繁体   English   中英

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/rc7f0fhttps://prnt.sc/rc7fbj似乎是重要的屏幕。

提前致谢,我完全没有想法。

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

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

这最终修复了它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM