89c52串口发送接收小示例
//串口发送
void sendChar(char *p)//调用前关中断,调用完成后关中断 { while(*p != '\0') { SBUF = *P while(!TI); TI = 0; p++; } } void sendChar(char *p)//调用前关中断,调用完成后关中断 { while(*p != '\0') { SBUF = *P while(!TI); TI = 0; p++; } }
//串口接收
//串口初始化
void series_init() { SCON = 0x50; //串口工作方式1,允许接受 TMOD = 0x20; //定时器T1工作方式2 TH1 = 0xfd;TL1 = 0xfd; //定时初值 PCON &= 0x00; //smod = 0 TR1 = 1; //开启定时器1 EA = 1;ES = 1; //开总中断可串行终端 } void series_init() { SCON = 0x50; //串口工作方式1,允许接受 TMOD = 0x20; //定时器T1工作方式2 TH1 = 0xfd;TL1 = 0xfd; //定时初值 PCON &= 0x00; //smod = 0 TR1 = 1; //开启定时器1 EA = 1;ES = 1; //开总中断可串行终端 }
unsigned char ReceiveBuf;
//串行中断服务函数
void series() interrupt 4
{
RI = 0;
ReceiveBuf = SBUF;
/*
do something;
*/
}
unsigned char ReceiveBuf;
//串行中断服务函数
void series() interrupt 4
{
RI = 0;
ReceiveBuf = SBUF;
/*
do something;
*/
}
补充:软件开发 , C++ ,