如何写51单片机二个串口收发程序
时间: 2023-03-24 09:03:02 浏览: 79
你可以参考以下的代码:
#include <reg51.h>
#define FOSC 11059200L
#define BAUD 960
#define TH1Value (256 - FOSC / 12 / BAUD)
sbit LED = P1^;
void InitUART()
{
SCON = x50; // 8-bit data, no parity, 1 stop bit
TMOD &= xF; // clear T1 mode bits
TMOD |= x20; // set T1 mode to 8-bit auto-reload
TH1 = TH1Value;
TL1 = TH1Value;
TR1 = 1; // start timer 1
ES = 1; // enable serial interrupt
EA = 1; // enable global interrupt
}
void SendByte(unsigned char dat)
{
SBUF = dat;
while (!TI);
TI = ;
}
void SendString(unsigned char *s)
{
while (*s)
{
SendByte(*s++);
}
}
void UARTInterrupt() interrupt 4
{
if (RI)
{
RI = ;
LED = !LED;
SendByte(SBUF);
}
if (TI)
{
TI = ;
}
}
void main()
{
InitUART();
while (1);
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)