我尝试了一下。我使用的14.7456的晶振,想采用9600 8 1 n的方式通讯,其实是与另外的一个板子(s3c6410)那么
单片机的串口设置就的有一定的值。计算公式是这样的:
F = 14.7456 Baud = 9600(12T模式)
BELOAD = 256 - INT(14745600/9600/32/12 + 0.5)
= 256 - INT(5.5)
= 256 -4
= 251
Baud = 14745600 /(256 - 254)/ 32 / 12
= 9600
没有任何的误差
我试验过另外的一个,38400,8,N,1的格式此时
BELOAD = 0xFF,无误差;具体的使用时这样:
voiduart_init(void)
{
EA = 0;
SCON = 0x50;//采用方式1,REN=1,其他位为0 1位,8位,1位,N 38400
AUXR = 0x17;
//PCON = 0x7F;//不能有这个否则将会程序启动不了,
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();
}
}
正确程序应该是这样的,9600,8位,无奇偶校验,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++)//延时用
{;}
uart_1send(i);
i++;
}