#include
#include
#define uchar unsigned char
#define uint unsigned int
void Uart0_Init()
{
//9600波特率对应 FFD9H
RCAP2L = 0xD9;
RCAP2H = 0xFF;
//RCLK、TCLK置1
//TR2置1
T2CON = 0x34;
//串口工作模式1,接收使能
SCON = 0x50;
//允许串口中断
ES = 1;
return;
}
void Com_Init(void)
{
TMOD = 0x20;
PCON = 0x00;
SCON = 0x50;
TH1 = 0xfd; //设置波特率 9600 //fD
TL1 = 0xd3; //
TR1 = 1; //启动定时器1
ES = 1; //开串口中断
EA = 1; //开总中断
}
void Main()
{
uchar i = 0;
uchar code Buffer[] = "哈哈哈"; //所要发送的数据
uchar *p;
// Com_Init();
Uart0_Init();
P1 = 0xff;
p = Buffer;
while(1)
{
SBUF = *p;
while(!TI) //如果发送完毕,硬件会置位TI
{
_nop_();
}
p++;
if(*p == '\0')
break; //在每个字符串的最后,会有一个'\0'
TI = 0; //TI清零
}
while(1);
}
void Uart0_Ist() interrupt 4
{
if(RI)
{
RI = 0;
//Do whatever you want;
}
if(TI)
{
TI = 0;
//Do whatever you want;
}
}
看到几个STC12C5A60S2关于串口的例程,里面都有这个子函数,但是配置都不一样,不知道怎么用,有些配置根本不懂干什么的
void uart0_init(void)
{
PCON |= 0x80; //UART0 Double Rate Enable
SCON = 0x50; //UART0 set as 10bit , UART0 RX enable
AUXR |= 0x01; //UART0 使用BRT
AUXR |= 0x04; //BRT set as 1T mode
BRT = BRT_Reload;
AUXR |= 0x10; //start BRT
ES = 1;
EA = 1;
}