Serial-mcu

任务: PC按下1键, mcu连续发送a, 当PC按下2键, 终止发送

查询:

 #include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uint receive ; void tranData();
void delay(uint ms)
{
for( ;ms>;ms--); }
void main(void)
{
EA = ; //允许总中断(如不使用中断,可用//屏蔽)
ES = ; //允许UART串口的中断
TMOD = 0x20; //定时器T/C1工作方式2
SCON = 0x50; //串口工作方式1,允许串口接收(SCON = 0x40 时禁止串口接收)
TH1 = 0xF3; //定时器初值高8位设置 //12MHZ晶振,波特率为4800 0xf3
TL1 = 0xF3; //定时器初值低8位设置 //11.0592MHZ晶振,波特率为4800 0xf4 9600 0xfa 19200 0xfd
PCON = 0x80; //波特率倍频(屏蔽本句波特率为2400)
TR1 = ; //定时器启动 while()
{
receive=SBUF;
tranData();
delay() ;
}
} void tranData() //interrupt 4
{ RI=;
if((receive==0x31))
{
SBUF='a';
while(!TI);
TI=; }
else if((receive==0x32))
{
TI=;
} }

中断:

 #include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uint receive ; void tranData();
void delay(uint ms)
{
for( ;ms>;ms--); }
void main(void)
{
EA = ;
ES = ;
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xF3;
TL1 = 0xF3;
PCON = 0x80;
TR1 = ; while();
} void tranData() interrupt
{
receive=SBUF;
while()
{
if (receive==0x31)
{
SBUF='a';
while(!TI);
TI=;
}
else if (receive==0x32)
{
TI=;
} RI=;
receive=SBUF;
delay();
}
}
上一篇:SQL CREATE TABLE 语句


下一篇:NIOS II CPU复位异常的原因及解决方案