微机原理项目报告-双机串行通信(编辑修改稿)内容摘要:
发送一个字符串 */ void SendStr(unsigned char *s) { while(*s!=0x0d) // 回车表示字符串结束标志,通过检测是否字符串末尾 { SendByte(*s)。 s++。 } } void initial() { LCD_Init()。 LCD_Clear()。 //清屏 P1=0xff。 //关 LED 灯 TMOD=0x20。 TH1=0xfd。 TL1=0xfd。 TR1=1。 //启动定时器 1 ET1=0。 //关定时器 1 中断 SM0=0。 //串行工作方式 SM1=1。 REN=1。 //允许串行接受位 EA=1。 //开 cpu 总中断 ES=1。 //打开串口中断 TI=0。 RI=0。 PCON=0x00。 //波特率不加倍 i=j=0。 } /* 串行中断函数 */ void ser() interrupt 4 { if(RI==1) { P1=0XF0。 //进入中断后, 灯指示 if(m==32){m=1。 } rec_str[m]=SBUF。 //接受数据存入数组 RI=0。 if(rec_str[m]==0x0d) //收到结束位,然后进入显示函数 { receive(rec_str)。 m=1。 } m++。 } } /* 进入主函数 */ void main() { initial()。 for(k=0。 k29。 k++) //显示提示输入连接信号 { LCD_Write_Char(i,j,table[k])。 } while(1) { num=KeyScan()。 //扫描键盘 if(num!=0xff) { send_str[n]=num。 //读取键盘数据存入数组中 if(send_str[n]!=0x0d) //收到发送信号然后发送 LCD_Write_Char(i,j,num)。 //显示按键字符 if(n==32){n=1。 } if(send_str[n]==0x0d) //收到发送 enter 信号然后发送 { P1=0X0F。 //进入发送函数 灯指示 send(send_str)。 //进入发送函数 LCD_Write_Char(i,j,0x20)。 //显示空格表示一次通话结束 n=1。 } n++。 } } } /* 接受显示函数 */ void receive(uchar *s) { SendStr(s)。 SendByte(0x0d)。 while(*s!=0x0d) { while(*s!=0x0d) { LCD_Write_Char(i,j,*s)。 s++。 } LCD_Write_Char(i,j,0x20)。 } } /* 发送函数 */ void send(uchar *r) { SendStr(r)。 SendByte(0x0d)。 } /* 液晶显示调整函数 */ void yjxs(uchar Data) { i++。 if((i==17)amp。 amp。 (j==1)) { j=0。 i=0。 LCD_Write_Com(0x01)。 //清屏 delay(5)。 LCD_Write_Char(i,j,Data)。 delay(5)。 } if((i==16)amp。 amp。 (j==0)) { j++。 i=0。 } } /* 键盘扫描函数,使用行列逐级扫描法 */ unsigned char KeyScan(void) { unsigned char Val。 KeyPort0=KeyPort1=0。 KeyPort2=KeyPort3=KeyPort4=KeyPort5=1。 //高四位置高,低四位拉低 if(!((KeyPort0==0)amp。 amp。 (KeyPort1==0)amp。 amp。 (KeyPort2==1)amp。 amp。 (KeyPort3==1)amp。 amp。 (KeyPort4==1)amp。 amp。 (KeyPort5==1))) //表示有按键按下 { delay(10)。 //去抖 if(!((KeyPort0==0)amp。 amp。 (KeyPort1==0)amp。 amp。 (KeyPort2==1)amp。 amp。 (KeyPort3==1)amp。 amp。 (KeyPort4==1)amp。 amp。 (KeyPort5==1))) { //表示有按键按下 KeyPort0=0。 KeyPort1=KeyPort2=KeyPort3=KeyPort4=KeyPort5=1。 //检测第一行 if(!((KeyPort0==0)amp。 amp。 (KeyPort1==1)amp。 amp。 (KeyPort2==1)amp。 amp。 (KeyPort3==1)amp。 amp。 (KeyPort4==1)amp。 amp。 (KeyPort5==1))) { if(KeyPort2==0){Val=39。 339。 } if(KeyPort3==0){Val=39。 239。 } if(KeyPort4==0){Val=39。 139。 } if(KeyPort5==0){Val=0x0d。 } while(!((KeyPort0==0)amp。 amp。 (KeyPort1==1)amp。 amp。 (KeyPort2==1)amp。 amp。 (KeyPort3==1)amp。 amp。 (KeyPort4==1)amp。 amp。 (KeyPort5==1)))。 delay(10)。 //去抖 while(!((KeyPort0==0)amp。 amp。 (KeyPort1==1)amp。 amp。 (KeyPort2==1)amp。 amp。 (KeyPort3==1)amp。 amp。 (KeyPort4==1)amp。 amp。 (KeyPort5==1)))。 return Val。 } KeyPort1=0。 KeyPort0=KeyPort2=KeyPort3=KeyPort4=KeyPort5=1。 //检测第二行 if(!((KeyPort0==1)amp。 amp。 (KeyPort1==0)amp。 amp。 (KeyPort2==1)amp。 amp。 (KeyPort3==1)amp。 amp。 (KeyPort4==1)amp。 amp。 (KeyPort5==1))) { if(KeyPort2==0){Val=39。 739。 } if(KeyPort3==0){Val=39。 639。 } if(KeyPort4==0){Val=39。 539。 } if(KeyPort5==0){Val=39。 439。 } while(!((KeyPort0==1)amp。 amp。 (KeyPort1==0)amp。 amp。 (KeyPort2==1)amp。 amp。 (KeyPort3==1)amp。 amp。 (KeyPort4==1)amp。 amp。 (KeyPort5==1)))。 delay(10)。 //去抖 while(!((KeyPort0==1)amp。 amp。 (KeyPort1==0)amp。 amp。 (KeyPort2==1)amp。 amp。 (KeyPort3==1)amp。 amp。 (KeyPort4==1)amp。 amp。 (KeyPort5==1)))。 return Val。 } } } return 0xff。 } 上位机程序:我们有三个程序。 上位机程序 1:若单片机先要第一个发的数据是 7 就建立连接,可以开始通信,不是就不能通信,若 PC 机先按键盘,则 PC 机收到 7 后会显示出错 STACK SEGMENT DB 256 DUP(0) ZS DB 256 DUP (0) ;定义握手开始建立时的暂时数据缓冲区 STACK ENDS DATA SEGMENT SED DB 256 DUP (0) ;定义发送数据缓冲区 REC DB 256 DUP (0) ;定义接收数据缓冲区 DATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA,SS:STACK BEGIN PROC FAR PUSH DS ;保存 PSP 段地址 XOR AX,AX PUSH AX ;保存返回地址偏移值 START: ;初始化 8250 为: 8 位数据位, 1 位停止位,无校验,波特率 9600 MOV AX,DATA MOV DS,AX LEA SI,SED LEA DI,REC LEA BP,ZS。微机原理项目报告-双机串行通信(编辑修改稿)
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。
用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。