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

标签 c terminal avr uart

我正在玩一个 DFRobot Romeo 开发板,上面有一个 ATmega328p MCU。

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

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

如果我在保持 UART 连接的情况下重置 Controller ,我会一遍又一遍地得到相同的结果。我使用的终端模拟器是 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);
}

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

CoolTerm display

最佳答案

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

尝试在循环之前放置一个延迟!

关于c - 终端仿真器通过 UART ATmega328p 从未知源接收字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58659776/

相关文章:

C 并发进程和管道

c - 终端无法识别更改,除非我写 "make programm"

assembly - 为什么分支指令比跳转指令更快?

c - sizeof(<字符串数组>) 始终返回 8

c - C语言工资单生成程序

c - 从 dev/urandom 获取真正随机的数据

android - 如何更改终端的默认目录?

javascript - 引用错误: require is not defined at gulpfile. js

c - AVR C 函数可以在 Main 中运行,但不能在其他函数中运行

c - GCC 编译时浮点优化