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

时间:2017-08-14 00:37:02

标签: c microcontroller avr uart atmega

#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

我打算发送&#39; A&#39;来自ATMEGA并希望在PC上的minicom上看到它。但我收到了#39; _&#39;在minicom。我将minicom设置更改为115200 7N1并仍然接收&#39; _&#39;。然后我改为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还是逻辑电平转换问题。

1 个答案:

答案 0 :(得分:0)

原来问题与我的代码无关。我最终订购了以下USB 2.0到TTL UART 6PIN CP2102模块串行转换器,所有东西都是hunky dory。

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

常规USB转串口CP-US-03无法正常工作。