cpu51 发表于 2014-6-7 12:20:07

M128的串口0发送数据不正确,请大神看看问题在哪.

本帖最后由 cpu51 于 2014-6-7 12:23 编辑

//问题:单片机发送0X00-0X17,计算机接收到的是 FD FB F9 F7 F5 F3 F1 EF ED EB E9 E7 E5 E3 E1 DF DD DB D9 D7 D5 D3 D1 00,计算机侧验证是好的,是发送的不对

void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV= 0x00; //xtal divider
XMCRA = 0x00; //external memory

port_init();

timer0_init();
uart0_init();
uart1_init();

MCUCR = 0x00;
MCUCSR=0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x00; //timer interrupt sourcestime0启用
ETIMSK = 0x00; //extended timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized

}
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x17;//?低?
UCSR0C = 0x06;//?低?8位数据1位停止位
UBRR0L = 0x47; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x98; //98
UCSR0B=(1<<TXEN0);

}
//*****************************************************************************
//发送字符
//使用通讯口0
//*****************************************************************************
void send_char_tx0(unsigned char d)
{   
    while (! ( UCSR0A & (1<<UDRE0) ) )   //等待TXC置位 /* 等待发送缓冲器为空 */
   ;          
   UDR0=d;                        //发送字符/* 将数据放入缓冲器,发送数据*/
      ;               
   while(!(UCSR0A & (1<<TXC0)));         //等待发送完毕
   UCSR0A|=(1<<TXC0);               
}

void main(void)
{
CLI();
init_devices();
while(1)
{
   delay_s(1);
send_char_tx0(0X00); send_char_tx0(0X01); send_char_tx0(0x02); send_char_tx0(0x03);   
send_char_tx0(0X04); send_char_tx0(0X05); send_char_tx0(0X06); send_char_tx0(0X07);
send_char_tx0(0X08); send_char_tx0(0X09); send_char_tx0(0x0A); send_char_tx0(0x0B);   
send_char_tx0(0X0C); send_char_tx0(0X0D); send_char_tx0(0X0E); send_char_tx0(0X0F);
send_char_tx0(0X10); send_char_tx0(0X11); send_char_tx0(0x12); send_char_tx0(0x13);   
send_char_tx0(0X14); send_char_tx0(0X15); send_char_tx0(0X16); send_char_tx0(0X17);
}
}
页: [1]
查看完整版本: M128的串口0发送数据不正确,请大神看看问题在哪.