farfar 发表于 2013-7-2 17:40:13

ATmega64A USART1发送数据乱码的提问

最近做一个ATmega64A双串口实验,在使用像类似的初始化代码和测试代码,USART0工作正常,但是USART1端口现在发送数据时出现乱码。麻烦各位老大看看可能是哪里出了问题呢?谢谢大家!
测试代码:
//Initalization USART1
void USART1Init(void)
{
        //Set Baud Rate
        temp = 14745600UL / 16 / 9600 - 1;
        UBRR1H = (uint8_t)(temp >> 8);
        UBRR1L = (uint8_t)temp;
       
        UCSR1C |= ((1 << UCSZ1) | (1 << UCSZ0)); //Data is 8bit
        UCSR1C &= 0xcf; //Parity is NONE
        UCSR1C &= (~(1 << USBS)); //Stop bit is 1bit
        UCSR0B |= ((1 << RXEN) | (1 << TXEN)); //Enable RXD & TXD
}


int mian(void)
{
        uint8_t TestVariable;
       
        USART1Init(void);
       
        //Test start
        TestVariable = 0;
        TestVariable = 1;
        TestVariable = 2;
        TestVariable = 3;
        TestVariable = 4;
        TestVariable = 5;
        TestVariable = 6;
        TestVariable = 7;
        while (1)
        {
                temp = 0;
                do
                {
                        while (!(UCSR1A & (1 << UDRE))) ; //Waitting TXD registor empty
                        UDR1 = TestVariable; //Loading data
                }
                while(8 > ++temp);
        }
       
        return 0;
}
俘获的发送数据片段:

farfar 发表于 2013-7-2 17:52:22

编译环境:WINAVR

jim20090418 发表于 2013-7-2 18:04:03

2個串口都要做初始化,

你的程序看起來2個串口初始化都沒完成;

要使用USART0,要初始化UBRR0H:UBRR0L,UCSR0B,UCSR0C

要使用USART1,要初始化UBRR1H:UBRR1L,UCSR1B,UCSR1C

farfar 发表于 2013-7-2 18:09:38

jim20090418 发表于 2013-7-2 18:04 static/image/common/back.gif
2個串口都要做初始化,

你的程序看起來2個串口初始化都沒完成;


感谢jim20090418的热心回答,USART0的初始化部分没有贴上来,USART1和USART0使用像类似的初始化代码,USART0已通过测试,完全没有问题。但是USART1却出现发送数据时出现乱码的问题,现在从代码上看不出哪里还有问题,所以请大家帮忙看看。

jim20090418 发表于 2013-7-2 18:13:18

      UCSR1C |= ((1 << UCSZ1) | (1 << UCSZ0)); //Data is 8bit
      UCSR1C &= 0xcf; //Parity is NONE
      UCSR1C &= (~(1 << USBS)); //Stop bit is 1bit
      UCSR0B |= ((1 << RXEN) | (1 << TXEN)); //Enable RXD & TXD<-----這個暫存器是USART0的

farfar 发表于 2013-7-2 18:45:53

jim20090418 发表于 2013-7-2 18:13 static/image/common/back.gif
UCSR1C |= ((1

是的,是我弄错了。其实是在去除一些无关代码时弄错了。
正确的应为“UCSR1B |= ((1 << RXEN) | (1 << TXEN)); //Enable RXD & TXD”
重新检查后故障依然存在。正在查找原因。
感谢jim20090418

farfar 发表于 2013-7-2 19:16:40

修改了一下测试代码:
从俘获的数据可以看出,出错的都是在头几个字节里。

//Initalization USART1
void USART1Init(void)
{
        //Set Baud Rate
        temp = 14745600UL / 16 / 9600 - 1;
        UBRR1H = (uint8_t)(temp >> 8);
        UBRR1L = (uint8_t)temp;
       
        UCSR1C |= ((1 << UCSZ1) | (1 << UCSZ0)); //Data is 8bit
        UCSR1C &= 0xcf; //Parity is NONE
        UCSR1C &= (~(1 << USBS)); //Stop bit is 1bit
        UCSR1B |= ((1 << RXEN) | (1 << TXEN)); //Enable RXD & TXD
}


int mian(void)
{
        uint8_t TestVariable;
       
        USART1Init(void);
       
        //Test start
        TestVariable = 0;
        TestVariable = 1;
        TestVariable = 2;
        TestVariable = 3;
        TestVariable = 4;
        TestVariable = 5;
        TestVariable = 6;
        TestVariable = 7;
        TestVariable = 8;
        TestVariable = 9;
        TestVariable = 10;
        TestVariable = 11;
        TestVariable = 12;
        TestVariable = 13;
        TestVariable = 14;
        TestVariable = 15;
        while (1)
        {
                temp = 0;
                do
                {
                        while (!(UCSR1A & (1 << UDRE))) ; //Waitting TXD registor empty
                        UDR1 = TestVariable; //Loading data
                }
                while(16 > ++temp);
        }
       
        return 0;
}

俘获的发送数据片段:

farfar 发表于 2013-7-2 22:19:55

好安靜啊...
页: [1]
查看完整版本: ATmega64A USART1发送数据乱码的提问