#include <reg51.h>
#include <intrins.h>
typedef unsigned int u16;
typedef unsigned char u8;
sbit led=P2^2;
void UsartInit()
{
SCON=0x50;//串口工作方式1,REN=1:打開串口允許位
PCON=0x80;//倍頻
TMOD=0x20; //定時器T1,工作方式2
TH1=0xF3; //波特率為4800=(2*32)/T1溢出率,所以T1溢出率=76800
TL1=0xF3; //T1溢出率=12M/{12*(256-TH1)} TH1=243
EA=1;
ES=1;
// ET1=1; 不打開定時中斷
TR1=1;
}
void UsartSendbyte(u8 byte)
{
SBUF=byte;
while(!TI);
TI=0;
}
void UsartSendchar(u8 *c)
{
while(*c!='\0')
{
UsartSendbyte(*c);
c++;
}
}
void main()
{
UsartInit();
while(1);
}
void Usart() interrupt 4
{
u8 receiveData;
u8 a[]="I receive\r\n";
if(RI)
{
RI=0;
receiveData=SBUF;
if(receiveData=='1')
{
led=0;
}
else
{
led=1;
}
}
UsartSendchar(a);
if(TI)
{
TI=0;
}
}
|