资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
一单片机与 PC 机通信(1) mcu2pc(1)#include #define uchar unsigned char#define uint unsigned intuchar 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 4if(RI=1)RI=0;temp=SBUF;SBUF=temp;if(temp=1) num+;if(TI=1) TI=0;/显示函数void display()P0=tablenum/10;P2=tablenum%10;/主函数void main()spinit();P0=table0;while(1)if(num=100) num=0;display();(2)课本#include#define uchar unsigned char#define uint unsigned intuchar dat,num;sbit wei1=P20;sbit wei2=P21;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=tablenum/10;wei1=0;delay(1); wei1=1;P0=tablenum%10;wei2=0;delay(1); wei2=1;void main()init_serial();while(1) if(num=100) num=0;display(); 二单片机与单片机通信(1) 双机通信-方式 1Rxd#include #define uchar unsigned char#define uint unsigned intuchar 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=table10;while(1);void rxd() interrupt 4RI=0;P0=tableSBUF;Txd#include #define uchar unsigned char#define uint unsigned intsbit key=P37;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);keynum+;if(keynum=10) keynum=0;SBUF=keynum;void txd() interrupt 4TI=0;(2) 双击通信-方式 2&3RXD#include #define uchar unsigned char#define uint unsigned intuchar 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=0xb0;/10110000EA=1;ES=1;P0=table10;while(1);void rxd() interrupt 4RI=0;P0=tableSBUF;TXD#include #define uchar unsigned char#define uint unsigned intsbit key=P37;uchar keynum;void main()TMOD=0x20;TH0=253;TL0=253;TR1=1;PCON=0x00;SCON=0x90;EA=1;ES=1;while(1)if(key=0)while(key=0);keynum+;if(keynum=10) keynum=0;ACC=keynum;TB8=P;SBUF=ACC;void txd() interrupt 4TI=0;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号