终端仿真器通过 UART ATmega328p 从未知源接收字节

Terminal Emulator Receiving Bytes from Unknown Source via UART ATmega328p

我正在玩一个带有 ATmega328p MCU 的 DFRobot Romeo 开发板。

我编写了一个程序,将 4 个 DIP 开关的二进制和十进制表示形式作为输入传输到 PORTD(PD2 -> PD5)。 UART 连接到 PORTD、PD0 (Rx) 和 PD1 (Tx)。

程序按预期工作除了当程序第一次运行我在Mac上使用的终端仿真器似乎接收并显示以下内容binary/decimal 数字:00000011 3,然后出现 00000000 0,或 DIP 开关设置的任何数字。

如果我在保持 UART 连接的情况下重置控制器,我会一遍又一遍地得到相同的结果。我使用的终端模拟器是 CoolTerm。即使在重新连接之前刷新缓冲区,我也会得到相同的结果。如果我尝试另一个终端仿真器(即 Serial、SerialTools),也会出现相同的结果。

我检查了我的代码,没有发现任何明显的错误。我联系了一位同行,他说他也看到了这种行为,但除了清除终端缓冲区外,没有其他解决方案的建议。我还尝试了几种不同的物理配置,例如仅通过 USB 为电路板供电、在未连接 USB 编程电缆的情况下通过单独的电源为电路板供电以及连接 USB 电缆。我在终端上得到了相同的结果。

有人可以看看这个并就为什么会出现这种行为提出建议吗?

这是程序的 C 代码。

/* 
    Connection Diagram:

    Serial USART:
    Atmega328p      Romeo Board     FTDI Cable
    PD0 ->          D0 ->           Orange          Rx
    PD1 ->          D1 ->           Yellow          Tx
    GND ->          GND ->          Black           Ground

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Digital I/O:
    Atmega328p      Romeo Board     IO Board Jumper     Component
    PD2 ->          D2 ->           JP2_7               DS1             Dip Switch 1 - MSB
    PD3 ->          D3 ->           JP2_6               DS2             Dip Switch 2
    PD4 ->          D4 ->           JP3_2               DS3             Dip Switch 3
    PD5 ->          D5 ->           JP2_5               DS4             Dip Switch 4 - LSB
*/

#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <string.h>
#define F_CPU 16000000UL
#define BAUD 19200
#define DOUBLE_SPEED 0
#define DECIMAL 10

//functions
void initUART(unsigned int baud, unsigned int speed);
void initDIO(void);
void printDec(int sum);
void printByte(uint8_t num);
void printCR(void);
void delay_ms (uint16_t ms);

int main(void)
{
    //initialize UART
    initUART(BAUD, DOUBLE_SPEED);

    //initialize DIO (Digital I/O)
    initDIO();

    //value to be determined
    uint8_t value = 0;

    //previous value
    uint8_t previous = 0;

    while(1)
    { 
        previous = value;

        //Set or Clear LSB
        if((PIND & 0b00100000) == 0)
        {
            value |= (1 << 0);
        }
        else
        {
            value &= ~(1 << 0);
        }

        //Set or Clear 2nd bit
        if((PIND & 0b00010000) == 0)
        {
            value |= (1 << 1);
        }
        else
        {
            value &= ~(1 << 1);
        }

        //Set or Clear 3rd bit
        if((PIND & 0b00001000) == 0)
        {
            value |= (1 << 2);
        }
        else
        {
            value &= ~(1 << 2);
        }

        //Set or Clear MSB
        if((PIND & 0b00000100) == 0)
        {
            value |= (1 << 3);
        }
        else
        {
            value &= ~(1 << 3);
        }

        //if value has changed since previous, print the result
        if(value != previous)
        {
            printByte(value);
            printDec(value);
            printCR();
        }

        //add small delay to loop in attempt to stablize the button state (debounce buttons)
        delay_ms(80);
    }
}

void initUART(unsigned int baud, unsigned int speed)
{
    unsigned int ubrr;

    //double speed is OFF in this lab (it is 0)
    if(speed)
    {
        //double rate mode
        ubrr = F_CPU/8/baud-1;
        //set double speed mode
        UCSR0A = (speed << U2X0);
    }
    else
    {
        //normal rate mode
        ubrr = F_CPU/16/baud-1;
    }

    //set the baud rate
    UBRR0H = (unsigned char)(ubrr >> 8);
    UBRR0L = (unsigned char)(ubrr);

    //enable Tx and Rx pins on MCU
    UCSR0B = (1 << RXEN0) | (1 << TXEN0);

    //set control bits, 8 bit char, 0 stop, no parity
    UCSR0C = (1 <<UCSZ00) | (1 <<UCSZ01);
}

void initDIO(void)
{
    //set inputs for Port B
    //PD2 (D2) DS1
    DDRD &= ~(1 << PD2); 

    //PD3 (D3) DS2
    DDRD &= ~(1 << PD3);

    //PD4 (D4) DS3
    DDRD &= ~(1 << PD4);

    //PD5 (D5) DS4
    DDRD &= ~(1 << PD5);

    //Set PORTD PD2 & PD3 & PD4 & PD5 Pull-Up Resistors
    PORTD |= ((1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5));
}

void printDec(int sum)
{
    //character buffer for integer to string converstion
    char buffer[sizeof(int)*8+1];

    //convert integer to decimal represented string
    itoa(sum, buffer, DECIMAL);

    //transmit character string via UART
    for(int i = 0; i < strlen(buffer); i++)
    {
        // Wait for empty transmit buffer
        while( !(UCSR0A & (1 << UDRE0)) ) {};

        //start transmission of character string
        UDR0 = buffer[i];
    }
}

void printByte(uint8_t num)
{
    //transmit binary characters via UART
    for(int i = 7; i >= 0; i--)
    {
        // Wait for empty transmit buffer
        while( !(UCSR0A & (1 << UDRE0)) ) {};

        //start transmission of character string.
        //can add character 'O' or integer 48 to statement below, 
        //both result in the correct character being transmitted.
        UDR0 = ((num >> i) & 1) + '0';
    }

    //transmit one white space
    //Wait for empty transmit buffer
    while( !(UCSR0A & (1 << UDRE0)) ) {};
    UDR0 = 32;
}

void printCR(void)
{
    //transmit carriage return
    //Wait for empty transmit buffer
    while( !(UCSR0A & (1 << UDRE0)) ) {};
    UDR0 = 13;

    //transmit line feed
    //Wait for empty transmit buffer
    while( !(UCSR0A & (1 << UDRE0)) ) {};
    UDR0 = 10;
}

void delay_ms (uint16_t ms) 
{
    uint16_t i;
    for (i = 0; i < ms; i++)
        _delay_ms(1);
}

这是我在程序第一次扫描时看到的屏幕截图。

可能是启用弱上拉和第一次采样之间没有足够的时间。

尝试在循环之前加入延迟!