我嘗試了一下。我使用的14.7456的晶振,想采用9600 8 1 n的方式通訊,其實(shí)是與另外的一個(gè)板子(s3c6410)那么單片機(jī)的串口設(shè)置就的有一定的值。計(jì)算公式是這樣的:F = 14.7456 Baud = 9600(12T模式)
本文引用地址:http://butianyuan.cn/article/201611/321320.htmBELOAD = 256 - INT(14745600/9600/32/12 + 0.5)
= 256 - INT(5.5)
= 256 -4
= 251
Baud = 14745600 /(256 - 254)/ 32 / 12
= 9600
沒有任何的誤差
我試驗(yàn)過另外的一個(gè),38400,8,N,1的格式此時(shí)
BELOAD = 0xFF,無誤差;具體的使用時(shí)這樣:
voiduart_init(void)
{
EA = 0;
SCON = 0x50;//采用方式1,REN=1,其他位為0 1位,8位,1位,N 38400
AUXR = 0x17;
//PCON = 0x7F;//不能有這個(gè)否則將會(huì)程序啟動(dòng)不了,
BRT = 0xff;//RELOD = 256 - 1; 誤差為0,9600是relod = 256 - 4;誤差為0
ES = 1;
EA = 1;
}
uchar uart_1send(unsigned char c)
{
ucharaa=0;
SBUF = c;
while(!TI) //若TI=0,在此等待
{
aa = 1;
return a;
}
TI = 0;
return a;
}
uchar uart_1read(void)
{
ucharaa=0;
aa = SBUF;
while(!RI) //若RI=0,在此等待
{
aa = 1;
return a;
}
RI = 0;
return a;
}
void uart1(void) interrupt 4
{
if(TI == 1)
{
uart_1send(send);
}
else
{
read = uart_1read();
}
}
正確程序應(yīng)該是這樣的,9600,8位,無奇偶校驗(yàn),1位停止
voiduart_init(void)
{
AUXR = 0x11;
SCON |= 0x50;
//PCON |= 0x80;
BRT = 0xfc;
ES = 1;
EA = 1;
}
void uart_1send(unsigned char c)
{
SBUF = c;
while(TI!=0) //若TI=0,在此等待
{
TI = 0;
}
}
uchar uart_1read(void)
{
uchar aa;
aa = SBUF;
while(RI!=0) //若RI=0,在此等待
{
RI = 0;
}
return aa;
}
int main(void)
{
unsigned char i;
unsigned int n;
alldata_init();
//uart_1send(i);
while(1)
{
//n++;
for(n=0;n<3000;n++)//延時(shí)用
{;}
uart_1send(i);
i++;
}
評(píng)論