单片机串行通信[互联网+].doc

上传人:rrsccc 文档编号:9363529 上传时间:2021-02-21 格式:DOC 页数:10 大小:162.50KB
返回 下载 相关 举报
单片机串行通信[互联网+].doc_第1页
第1页 / 共10页
单片机串行通信[互联网+].doc_第2页
第2页 / 共10页
单片机串行通信[互联网+].doc_第3页
第3页 / 共10页
单片机串行通信[互联网+].doc_第4页
第4页 / 共10页
亲,该文档总共10页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《单片机串行通信[互联网+].doc》由会员分享,可在线阅读,更多相关《单片机串行通信[互联网+].doc(10页珍藏版)》请在三一文库上搜索。

1、45 甲机通过串口控制乙机LED/*名称:甲机发送控制命令字符说明:甲单片机负责向外发送控制命令字符“A”、“B”、“C”,或者停止发送,乙机根据所接收到的字符完成LED1闪烁、LED2闪烁、双闪烁、或停止闪烁。*/#include#define uchar unsigned char#define uint unsigned intsbit LED1=P00;sbit LED2=P03;sbit K1=P10;/延时void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i120;i+);/向串口发送字符void Putc_to_SerialPort(

2、uchar c)SBUF=c;while(TI=0);TI=0;/主程序void main()uchar Operation_No=0;SCON=0x40;/串口模式1TMOD=0x20;/T1工作模式2PCON=0x00;/波特率不倍增TH1=0xfd;TL1=0xfd;TI=0;TR1=1;while(1)if(K1=0)/按下K1时选择操作代码0,1,2,3while(K1=0);Operation_No=(Operation_No+1)%4;switch(Operation_No)/根据操作代码发送A/B/C或停止发送case 0:LED1=LED2=1;break;case 1:Pu

3、tc_to_SerialPort(A);LED1=LED1;LED2=1;break;case 2:Putc_to_SerialPort(B);LED2=LED2;LED1=1;break;case 3:Putc_to_SerialPort(C);LED1=LED1;LED2=LED1;break;DelayMS(100);/*名称:乙机程序接收甲机发送字符并完成相应动作说明:乙机接收到甲机发送的信号后,根据相应信号控制LED完成不同闪烁动作。*/#include#define uchar unsigned char#define uint unsigned intsbit LED1=P00;

4、sbit LED2=P03;/延时void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i120;i+);/主程序void main()SCON=0x50;/串口模式1,允许接收TMOD=0x20;/T1工作模式2PCON=0x00;/波特率不倍增TH1=0xfd;/波特率9600TL1=0xfd;RI=0;TR1=1;LED1=LED2=1;while(1)if(RI)/如收到则LED闪烁RI=0;switch(SBUF)/根据所收到的不同命令字符完成不同动作case A:LED1=LED1;LED2=1;break;/LED1闪烁case B:LE

5、D2=LED2;LED1=1;break;/LED2闪烁case C:LED1=LED1;LED2=LED1;/双闪烁else LED1=LED2=1;/关闭LEDDelayMS(100);46 单片机之间双向通信/*名称:甲机串口程序说明:甲机向乙机发送控制命令字符,甲机同时接收乙机发送的数字,并显示在数码管上。*/#include#define uchar unsigned char#define uint unsigned intsbit LED1=P10;sbit LED2=P13;sbit K1=P17;uchar Operation_No=0;/操作代码/数码管代码uchar co

6、de DSY_CODE=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f;/延时void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i=0&SBUF=9) P0=DSY_CODESBUF;else P0=0x00;/*名称:乙机程序接收甲机发送字符并完成相应动作说明:乙机接收到甲机发送的信号后,根据相应信号控制LED完成不同闪烁动作。*/#include#define uchar unsigned char#define uint unsigned intsbit LED1=P10;sbit LED2

7、=P13;sbit K2=P17;uchar NumX=-1;/延时void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i120;i+);/主程序void main()LED1=LED2=1;SCON=0x50;/串口模式1,允许接收TMOD=0x20;/T1工作模式2TH1=0xfd;/波特率9600TL1=0xfd;PCON=0x00;/波特率不倍增RI=TI=0;TR1=1;IE=0x90;while(1)DelayMS(100);if(K2=0)while(K2=0);NumX=+NumX%11;/产生010范围内的数字,其中10表示关闭SB

8、UF=NumX;while(TI=0);TI=0;void Serial_INT() interrupt 4if(RI)/如收到则LED则动作RI=0;switch(SBUF)/根据所收到的不同命令字符完成不同动作case X:LED1=LED2=1;break;/全灭case A:LED1=0;LED2=1;break;/LED1亮case B:LED2=0;LED1=1;break;/LED2亮case C:LED1=LED2=0;/全亮47 单片机向主机发送字符串/*名称:单片机向主机发送字符串说明:单片机按一定的时间间隔向主机发送字符串,发送内容在虚拟终端显示。*/#include#d

9、efine uchar unsigned char#define uint unsigned int/延时void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i120;i+);/向串口发送字符void Putc_to_SerialPort(uchar c)SBUF=c;while(TI=0);TI=0;/向串口发送字符串void Puts_to_SerialPort(uchar *s)while(*s!=0)Putc_to_SerialPort(*s);s+;DelayMS(5);/主程序void main()uchar c=0;SCON=0x40;

10、/串口模式1 TMOD=0x20;/T1工作模式2TH1=0xfd;/波特率9600TL1=0xfd;PCON=0x00;/波特率不倍增TI=0;TR1=1;DelayMS(200);/向主机发送数据Puts_to_SerialPort(Receiving From 8051.rn);Puts_to_SerialPort(-rn);DelayMS(50);while(1)Putc_to_SerialPort(c+A);DelayMS(100);Putc_to_SerialPort( );DelayMS(100);if(c=25)/每输出一遍后加横线Puts_to_SerialPort(rn-r

11、n);DelayMS(100);c=(c+1)%26;if(c%10=0)/每输出10个字符后换行Puts_to_SerialPort(rn);DelayMS(100);48 单片机与PC通信/*名称:单片机与PC通信说明:单片机可接收PC发送的数字字符,按下单片机的K1键后,单片机可向PC发送字符串。在Proteus环境下完成本实验时,需要安装Virtual Serial Port Driver和串口调试助手。本例缓冲100个数字字符,缓冲满后新数字从前面开始存放(环形缓冲)。*/#include#define uchar unsigned char#define uint unsigned

12、 intuchar Receive_Buffer101;/接收缓冲uchar Buf_Index=0;/缓冲空间索引/数码管编码uchar code DSY_CODE=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00;/延时void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i120;i+);/主程序void main()uchar i;P0=0x00;Receive_Buffer0=-1;SCON=0x50;/串口模式1,允许接收TMOD=0x20;/T1工作模式2TH1=0xfd;/波特

13、率9600TL1=0xfd;PCON=0x00;/波特率不倍增EA=1;EX0=1;IT0=1;ES=1;IP=0x01;TR1=1;while(1)for(i=0;i=0&c=9)/缓存新接收的每个字符,并在其后放-1为结束标志Receive_BufferBuf_Index=c-0;Receive_BufferBuf_Index+1=-1;Buf_Index=(Buf_Index+1)%100;ES=1;void EX_INT0() interrupt 0/外部中断0uchar *s=这是由8051发送的字符串!rn;uchar i=0;while(si!=0)SBUF=si;while(TI=0);TI=0;i+;10互联网络

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1