/* file name: uart.c
* author: yilonglucky#gmail.com
* description: basic usage of uart for 51
* NOTE: 波特率设置为9600. 无奇偶校验. 晶振11.0592MHz
*/
#include<reg52.h>
void SendByte(unsigned char c)
{
SBUF = c;
while(!TI)
{
/* do nothing */;
}
TI = 0;
}
void SendStr(unsigned char *s)
{
while('\0' != *s)
{
SendByte(*s);
s++;
}
/* add a new line after the string */
SendByte(13); /* 0x0D: CR (carriage return) */
SendByte(10); /* 0x0A: LF (NL linefeed, new line) */
}
void InitUART(void)
{
SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重装
TH1 = 0xFD; // TH1: 重装值 9600 波特率 晶振 11.0592MHz
TR1 = 1; // TR1: timer 1 打开
EA = 1; //打开总中断
//ES = 1; //打开串口中断
}
void main(void)
{
unsigned int i;
char str[] = "This is UART test!";
InitUART();
while(1)
{
SendStr(str);
for(i=0; i< 60000; i++)
{
/* do nothing */;
}
}
}
51之串口的基本使用
最新推荐文章于 2023-04-02 17:53:27 发布