C语言 ATMEGA 2560 uart 代码在小通上未给出正确的输出


#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上看到它。但是我在小手机上收到"_"。我将小通信设置更改为 115200 7N1,但仍然收到"_"。然后我更改为 115200 6N1,然后我得到一个不同的二进制字符。我尝试更改迷你通信设置,但无济于事。知道我做错了什么吗?

这就是我在发送不同角色时看到的。

预期 (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;
;

这是我加载固件时发生的情况

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不起作用。

最新更新