单片机与pc串口通信-----下位机程序
发布时间:2016-8-9 10:00
发布者:designapp
//MCS-51通过中断方式接收pc机发来的字符,并回送给主机 #include #include #define uchar unsigned char uchar xdata rt_buf[32]; uchar r_in,t_out; bit r_full,t_empty; serial() interrupt 4 //串口中断程序 { if(RI && r_full) { rt_buf[r_in]=SBUF; RI=0; if(rt_buf[r_in]==0x24) { r_full=1; SBUF=rt_buf[t_out]; t_empty=0; } /*接收字符为$,则接收结束;设置接收结束标志,开始发送*/ r_in=++r_in; } else if(TI && t_empty) { TI=0; t_out=++t_out; SBUF=rt_buf[t_out]; if(t_out==r_in) t_empty=1; /*t_out=r_in则发送完,设发送完标志t_empty*/ } } main() { /*设置定时器T1工作于方式2,计数常数为0xfdH */ TMOD=0x20; //TMOD:GATE C/T1 M1 M0 GATE C/T0 M1 M0 0010 0000 以TR启动定时 TL1=0xfd; //定时3us TH1=0xfd; SCON=0x50; //在11.0592MHz下,设置串行口波特率为9600,方式1,允许接收 01010000 PCON=0xD0; // 11010000 波特率加倍 IE=0x10; //00010000 允许串行中断ES=1 TR1=1; EA=1; r_in=t_out=0; t_empty =1; r_full=0; for(;;) { printf ("%s","serial communication\n"); //添加自己的程序处理代码 } } 附上: proteus下单片机和pc机串口的连接图 |
网友评论