PuTTY 不向 AVR 串口发送数据

PuTTY not sending data to AVR serial

在我的嵌入式编程课程的练习中,我们必须对 Atmega328p AVR 芯片进行编程以通过串口接收数据。为此,我们必须调用一个等待接收到字符的函数。接下来它应该在 LED 灯中显示该字符的 ascii 值,但我什至无法接收它。我已经做了很多调试,我想我把它缩小到 PuTTY 甚至没有发送数据,或者 AVR 没有正确接收它。我将把我的代码放在下面:

/*
From the PC should come a char, sent via the serial port to the USART data register. 
It will arrive in the RXB, which receives data sent to the USART data register. 
While running the loop, the program should encounter a function that is called and waits for the RXB to be filled. 
Then it will read the RXB and return it to the main loop.
The result will be stored and processed accordingly.
*/

#define F_CPU 15974400
#include <util/delay.h>
#include <avr/io.h>
#include <stdlib.h>
#include <avr/interrupt.h>

void writeChar(char x);
void initSerial();
char readChar();

int main(void)
{
    initSerial();
    while (1) 
    {
        char c = readChar(); //reads char, puts it in c
        _delay_ms(250); //waits

        writeChar(c); // spits that char back into the terminal
    }
}

void initSerial(){
UCSR0A = 0;
//UCSR0B = (1 << TXEN0); // Enable de USART Transmitter
UCSR0B = 0b00011000; //transmit and receive enable
//UCSR0C = (1 << UCSZ01) | (0 << UCSZ00); /* 8 data bits, 1 stop bit */
UCSR0C = 0b00100110; // Even parity, 8 data bits, 1 stop bit
UBRR0H=00;
UBRR0L=103; //baudrade 9600 bij
}

void writeChar(char x){
    while(!(UCSR0A & (1 << UDRE0))); // waits until it can send data
    UDR0 = x; // Puts x into the UDR0, outputting it
}

char readChar(){
    while (!(UCSR0A & (1 << RXC0))); //waits until it can send data
    return UDR0; // returns the contents of the UDR0 (the receiving part of course
}

问题是当我在 PuTTY 中输入任何内容时(我假设我设置正确。https://prnt.sc/rc7f0f and https://prnt.sc/rc7fbj 似乎是重要的屏幕。

在此先感谢,我完全没有想法。

我自己修好了。我把它带到楼下在另一台笔记本电脑上测试时弄明白了。我仍然将 LED 放入 PORTD 的所有引脚,全部处于输入模式(这是默认模式)。我快速查看 Atmega328p user guide(第 2.5.3 节),发现引脚 D0 实际上是 USART 的 RxD。通过在其上放置一个 LED 并将其有效接地,它始终被拉低,并且永远不会被 CPU 拉高,这将停止 readChar() 中的 while 循环检查 while (!(UCSR0A & (1 << RXC0))); //waits until it can send data

因此,只需移除那个 LED 灯,它就会再次工作。显然这意味着它是浮动的,所以我将 DDRD 设置为全部输出,因为无论如何都不需要输入。

这最终解决了它。