PuTTY不向AVR串行发送数据

问题描述 投票:1回答:1

在我的嵌入式编程课程的练习中,我们必须对Atmega328p AVR芯片进行编程,以通过串行端口接收数据。为此,我们必须调用一个等待接收到字符的函数。接下来,它应该在led灯中显示该char的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似乎是重要的屏幕。

谢谢,我完全没主意了。

c avr atmega
1个回答
0
投票

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

因此,只需删除导致其将再次起作用。显然这意味着它是浮动的,所以我将DDRD设置为全部输出,因为无论如何都不需要输入。

最终解决了它。

© www.soinside.com 2019 - 2024. All rights reserved.