1

When I try to receive the USART signal with my Silicon Labs CP210x USB to UART Bridge. The only thing I receive is:

<0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0><0> etc

I got the right settings selected: baud rate: 9600 data bits: 8 parity: none stop bits: 1

I use a ATmega128A3U. The test_LED (see code below) on port E pin 0 is working. I used a oscilloscope to check the uart signal. See picture here: https://i.stack.imgur.com/MX0O0.jpg

Does someone know a solution for this?

Does anyone know how to fix a framing error? (My UART software is giving this error)

CODE:

#define F_CPU (32000000UL) // CPU clock speed

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

void USARTF0_init() {
    USARTF0_BAUDCTRLB = 0; //BSCALE is 0
    USARTF0_BAUDCTRLA = 0xCF; //BSEL is 207

    USARTF0_CTRLA = 0; //Disable interrupts
    USARTF0_CTRLC = USART_CHSIZE_8BIT_gc; //8 data bits, no parity and 1 stop bit

    USARTF0_CTRLB = USART_TXEN_bm | USART_RXEN_bm; // //Enable receive,transmit and high speed mode

    PORTF_OUTSET = PIN3_bm; // Port F pin 3 as TX
    PORTF_DIRSET = PIN3_bm; // TX pin as output

    PORTF_OUTCLR = PIN2_bm; // Port F pin 2 as RX
    PORTF_DIRCLR = PIN2_bm; // RX pin as input
}

void sendChar(char c) {

    while( !(USARTF0_STATUS & USART_DREIF_bm) ); //Wait until DATA buffer is empty

    USARTF0_DATA = c;
}

void sendString(char *text) {
    while(*text) {
        sendChar(*text++);
    }
}

int main(void) {
    USARTF0_init();

    PORTE.DIRSET = PIN0_bm; // make port E pin 0 output (test_LED)

    while(1)
    {
        _delay_ms(10);
        sendString("Hello World!\n\r");
        PORTE.OUTTGL = PIN0_bm; // test_LED Toggle
    }
} 

Code source: http://morf.lv/modules.php?name=tutorials&lasit=29

DrOctorooi
  • 11
  • 3
  • Well, you can see some signal is sent to UART line. Check the scope trace. You should be able to figure out the clock rate quickly (and it will confirm if it really is the 9600) and also decode data to ASCII. On the other hand, maybe you've just mixed up RX/TX pins on USB UART bridge. – domen Oct 26 '15 at 13:40
  • Thanks for your reply. I've change the string to "@~00" so the signal is shorter. This characters in ASCII: @ = 01000000 ~ = 01111110 0 = 00110000 http://imgur.com/vAQH85M In this picture you can see that every character gets "10" behind it except the last one. Is this the stop bit? Why doesn't the last character get a stop bit? Also I've check the bittime this should be 104 μs but when I calculate the bittime it's 608 μs. How can I fix this? Please reply – DrOctorooi Oct 27 '15 at 10:13
  • Nice, good work on this last picture. The bits look good. 1 start bit (low), bits LSB first, 1 stop bit (high). – domen Oct 27 '15 at 10:15
  • I'm not sure how you calculated the bit length. If all 4 bytes are 62.5ms, then 1 bit should be around 62.5/40.0 = 1.56. This means the baudrate is 1/0.00156 = 641 baud. If I go the other way, 608us for 1 bit, that means baudrate is 1/0.000608 = 1644 baud. Either way, your baudrate seems incorrect - check F_CPU and baud rate dividers. – domen Oct 27 '15 at 10:19
  • Thanks for your reply. You are right. My calculations were wrong. So It should be like this: http://imgur.com/eXftd43 green: start bit; blue: stop bit. My F_CPU should be 32 MHz cause I use a ATxmega128A3U and can't see anything wrong with the rate dividers. :( – DrOctorooi Oct 27 '15 at 10:48
  • Are you sure about 32MHz? Quick glance at datasheet says 2MHz is the default clock after reset. And 9600/615 is 15.6, which means your clock (or dividers) are about 16 times too slow. – domen Oct 27 '15 at 11:02
  • Thank you very much domen! You solved my problem. I also had to recalculate the values of BSCALE and BSEL with the right clock frequency. Now it works fine. :) – DrOctorooi Oct 27 '15 at 14:53
  • I've tried to create an answer from this, so it hopefully helps someone in the future. I took your oscilloscope photo, I hope you don't mind. – domen Oct 27 '15 at 15:05

1 Answers1

4

To recap the discussion, so it can useful to someone later.

If you have an oscilloscope, capture a trace for a simple test case, as @DrOctorooi has done with "@~00": enter image description here

He has marked start and stop bits, and also the data bits (for serial/UART they're least-significant first)

This confirms that UART somehow works.

One should also confirm the voltage levels. 0-3.2V looks about right.

And the time base. On the captured trace it appears 1 bit takes 1.625ms, which means a baudrate of around 615. That's quite far from expected 9600. In fact, that's around 16 times slower.

Since the error has to do with time, the immediate questions are the MCU clock and UART clock dividers (and on more complex devices, and intermediate dividers and clocks).

It turns out the dividers were meant for the 32MHz clock, but this MCU has a clock of 2MHz when it comes out of reset (16 times slower, as we have seen above). Solution was to recalculate the dividers.

domen
  • 1,819
  • 12
  • 19