ATMEGA 2560 uart 代码在 minicom 上没有给出正确的输出

ATMEGA 2560 uart code not giving out correct output on minicom

#include <avr/io.h>
#include <util/delay.h>

#define BAUDRATE 115200
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

//Declaration of our functions
void USART_init(void);
unsigned char USART_receive(void);
void USART_send( unsigned char data);

int main(void){
USART_init();        //Call the USART initialization code

while(1){        //Infinite loop
 USART_send('A');
 _delay_ms(1000);        //Delay for 5 seconds so it will re-send the string every 5 seconds
 }

return 0;
}

void USART_init(void){

 UBRR1H = (uint8_t)(BAUD_PRESCALLER>>8);
 UBRR1L = (uint8_t)(BAUD_PRESCALLER);
 UCSR1B = (1<<RXEN1)|(1<<TXEN1);
 UCSR1C = (3<<UCSZ10);
}

unsigned char USART_receive(void){

 while(!(UCSR1A & (1<<RXC1)));
 return UDR1;

}

void USART_send( unsigned char data){

 while(!(UCSR1A & (1<<UDRE1)));
 UDR1 = data;

}

Ubuntu 上的 minicom 设置为 115200 8N1

我正在使用 Elegoo ATMEGA2560、TX1 和 RX1 以及通信端口的 GND 引脚。 https://github.com/enthusiasticgeek/Elegoo_Mega_2560

我打算从 ATMEGA 发送 'A' 并希望在 PC 的 minicom 上看到它。但我在 minicom 上收到“_”。我将 minicom 设置更改为 115200 7N1,但仍然收到“_”。然后我改为 115200 6N1 然后我得到一个不同的二进制字符。我尝试更改 minicom 设置但无济于事。知道我哪里出错了吗?

这是我发送不同字符时看到的内容。

预期(AVR 发送) ASCII 0x56 [01010110] (V)

观察到(PC 接收) ASCII 0x2A [00101010] (*)

预期(AVR 发送) ASCII 0x41 [01000001] (A)

观察到(PC 接收) ASCII 0x5F [01011111] (_)

预期(AVR 发送) ASCII 0x42 [01000010] (B)

观察到(PC接收) ASCII 0x2F [00101111] (/)

预期(AVR 发送) ASCII 0x55 [01010101] (U)

观察到(PC 接收) ASCII 0x55 [01010101] (U)

这是我的保险丝设置https://github.com/enthusiasticgeek/Elegoo_Mega_2560/blob/master/avrdude.conf

    memory "lfuse"
    size            = 1;
    write           = "1 0 1 0  1 1 0 0  1 0 1 0  0 0 0 0",
                      "x x x x  x x x x  i i i i  i i i i";

    read            = "0 1 0 1  0 0 0 0  0 0 0 0  0 0 0 0",
                      "x x x x  x x x x  o o o o  o o o o";
    min_write_delay = 9000;
    max_write_delay = 9000;
      ;

    memory "hfuse"
    size            = 1;
    write           = "1 0 1 0  1 1 0 0  1 0 1 0  1 0 0 0",
                      "x x x x  x x x x  i i i i  i i i i";

    read            = "0 1 0 1  1 0 0 0  0 0 0 0  1 0 0 0",
                      "x x x x  x x x x  o o o o  o o o o";
    min_write_delay = 9000;
    max_write_delay = 9000;
      ;

    memory "efuse"
    size            = 1;
    write           = "1 0 1 0  1 1 0 0  1 0 1 0  0 1 0 0",
                      "x x x x  x x x x  x x x x  x i i i";

    read            = "0 1 0 1  0 0 0 0  0 0 0 0  1 0 0 0",
                      "x x x x  x x x x  o o o o  o o o o";
    min_write_delay = 9000;
    max_write_delay = 9000;
;

这是我加载 fw 时发生的情况

avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega2560 -c -o test.o test.c
avr-gcc -mmcu=atmega2560 test.o -o test
#EEPROM
#avr-objcopy -O ihex -R .eeprom test test.hex
#FLASH
avr-objcopy -O ihex -R .flash test test.hex
sudo avrdude -c wiring -p m2560 -P /dev/ttyACM0 -b 115200 -V -U flash:w:test.hex -D

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file "test.hex"
avrdude: input file test.hex auto detected as Intel Hex
avrdude: writing flash (366 bytes):

Writing | ################################################## | 100% 0.08s

avrdude: 366 bytes of flash written

avrdude: safemode: Fuses OK (E:FD, H:D8, L:FF)

avrdude done.  Thank you.

注意:我使用的是 CP-US-03 串口适配器,我认为它会有 FTD232 芯片。我也使用代码

从 Arduino 草图中得到相同的结果
void setup() {

// initialize both serial ports:
Serial1.begin(115200);

}

void loop() {

// read from port 1, send to port 0:
//if (Serial1.available()) {
//  int inByte = Serial1.read();
//  Serial.write(inByte);
//
 }

// read from port 0, send to port 1:
//if (Serial1.available()) {
  int inByte = 0x41;//Serial.read();
  Serial1.write(inByte);
  delay(1000);
//}
}

因此,现在我开始查看这是TTL还是逻辑电平转换问题。

原来问题与我的代码无关。我最终订购了以下 USB 2.0 到 TTL UART 6PIN CP2102 模块串行转换器,一切都很顺利。

https://www.amazon.com/gp/product/B01LRVQIFQ/ref=oh_aui_detailpage_o01_s00?ie=UTF8&psc=1

常规 USB 转串口 CP-US-03 不工作。