文档详情

单片机C语言双机通信(完整版).doc

发布:2017-07-07约2.82千字共6页下载文档
文本预览下载声明
单片机与PC机通信 mcu2pc(1) #include reg51.h #define uchar unsigned char #define uint unsigned int uchar temp,num; uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //串行口初始化函数 void spinit() { //串行口工作方式设定 SCON=0x50; //串行通信波特率设定 PCON=0x00; TMOD=0x20; TH1=253; TL1=253; TR1=1; //串行口中断允许 EA=1; ES=1; } //串行口中断服务函数 void spsv() interrupt 4 { if(RI==1) { RI=0; temp=SBUF; SBUF=temp; if(temp==1) num++; } if(TI==1) TI=0; } //显示函数 void display() { P0=table[num/10]; P2=table[num%10]; } //主函数 void main() { spinit(); P0=table[0]; while(1) { if(num==100) num=0; display(); } } (2)课本 #includereg51.h #define uchar unsigned char #define uint unsigned int uchar dat,num; sbit wei1=P2^0; sbit wei2=P2^1; uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; void delay(uint z) { uint x,y; for(x=z;x0;x--) for(y=110;y0;y--); } void init_serial() { SCON=0x50; TMOD=0X20; PCON=0X00; TH1=0xfd; TL1=0xfd; TR1=1; EA=1; ES=1; } void serial() interrupt 4 { if(RI==1) { RI=0; dat=SBUF; if(dat==1) { num++; } SBUF=dat; } else if(TI==1) TI=0; } void display() { P0=table[num/10]; wei1=0; delay(1); wei1=1; P0=table[num%10]; wei2=0; delay(1); wei2=1; } void main() { init_serial(); while(1) { if(num==100) num=0; display(); } } 单片机与单片机通信 双机通信-方式1 Rxd #include reg51.h #define uchar unsigned char #define uint unsigned int uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00}; void main() { TMOD=0x20; TH0=253; TL0=253; TR1=1; PCON=0x00; SCON=0x50; EA=1; ES=1; P0=table[10]; while(1); } void rxd() interrupt 4 { RI=0; P0=table[SBUF]; } Txd #include reg51.h #define uchar unsigned char #define uint unsigned int sbit key=P3^7; uchar keynum; void main() { TMOD=0x20; TH0=253; TL0=253; TR1=1; PCON=0x00; SCON=0x40; EA=1; ES=1; while(1) { if(key==0) { while(key==0); ke
显示全部
相似文档