普中单片机–串口通信(2)
通过串口助手发送数据 点亮LED
软件部分
#include <reg52.h>
#define jingzhen 12000000UL
#define botelv 4800UL
typedef unsigned char uchar;
typedef unsigned int uint;
uchar buf;
sbit beep=P1^5;
void main(void)
{
EA=0;
TMOD&=0x0F;
TMOD|=0x20;
SCON=0x50;
TH1=256-jingzhen/(botelv*12*16);
TL1=256-jingzhen/(botelv*12*16);
PCON|=0x80;
ES=1;
TR1=1;
REN=1;
EA=1;
while(1);
}
void serial() interrupt 4
{
ES = 0;
RI = 0;
buf = SBUF;
switch(buf)
{
case 0x31: P2=0xfe;beep=1;break;
case 0x32: P2=0xfd;beep=1;break;
case 0x33: P2=0xfb;beep=1;break;
case 0x34: P2=0xf7;beep=1;break;
case 0x35: P2=0xef;beep=1;break;
case 0x36: P2=0xdf;beep=1;break;
case 0x37: P2=0xbf;beep=1;break;
case 0x38: P2=0x7f;beep=1;break;
default: beep=0;P2=0xff;break;
}
ES = 1;
}
参考资料
- 串口通信https://blog.csdn.net/weixin_43599390/article/details/105206530
- 串口通信基本原理(超基础、详细版) https://blog.csdn.net/qq_38410730/article/details/79887200