gpt4 book ai didi

c - ATMEGA 2560 uart 代码未在 minicom 上给出正确的输出

转载 作者:行者123 更新时间:2023-11-30 16:42:52 26 4
gpt4 key购买 nike

#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)

这是我的 fuse 设置 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 不起作用。

关于c - ATMEGA 2560 uart 代码未在 minicom 上给出正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45666084/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com