Would you like to react to this message? Create an account in a few clicks or log in to continue.

You are not connected. Please login or register

Atmega8 USART RX/TX problem on pty

2 posters

Go down  Message [Page 1 of 1]

1Atmega8 USART RX/TX problem on pty Empty Atmega8 USART RX/TX problem on pty Thu Apr 27, 2023 8:16 pm

unbreakmyheart



Hello everyone!
I came across an unusual behavior of the USART - the data sent to the Atmega8 (via pty serial interface)is distorted. for example, data from 0x00 to 0x19 comes correctly, and everything above 0x19 already comes to the MC in the form of two bytes. Here is my diagram, can someone help me figure it out (its simple echo), thank you ?

My setup:
6.1.14-200.fc37.x86_64
SimulIDE 1.0.0-SR0 t Rev 1320

main.c:
Code:

// ATMEGA8-16PU USART echo
#define F_CPU 16000000UL
#define __AVR_ATmega8__

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

#define BAUD 1000000
#define BAUDRATE ((F_CPU + (BAUD * 4)) / (BAUD *  - 1)  // U2X == 1

void usart_init(unsigned int ubrr) {
    UBRRH = (ubrr >> 8);
    UBRRL = ubrr;
    UCSRB = (1 << TXEN) | (1 << RXEN);                  // enable receiver and transmitter
    UCSRA = (1 << U2X);                                 // enable 2x mode
    UCSRC = (1 << UCSZ0) | (1 << UCSZ1) | (1 << USBS);  // 8bit 2stop data format
}

int main() {
    usart_init(BAUDRATE);
    DDRB = 0b11111111;  // set all PortC Output
    DDRC = 0b11111111;  // set all PortC Output
    DDRD = 0b11111111;  // set all PortC Output

    unsigned char tmp;
    while (1) {
        while (!(UCSRA & (1 << RXC)))
            ;
        tmp = UDR;
        while (!(UCSRA & (1 << UDRE)))
            ;
        UDR = tmp;
    }
}


pty:
Code:

socat -d -d pty,rawer,echo=0,link=pty_in,user=LINUX_USERNAME_HERE pty,rawer,echo=0,link=pty_out,user=LINUX_USERNAME_HERE

event:
Code:

watch -n 1 'echo -n "1">pty_in'

Atmega8 USART RX/TX problem on pty Screenshot-from-2023-04-27-20-52-12

2Atmega8 USART RX/TX problem on pty Empty Re: Atmega8 USART RX/TX problem on pty Fri Apr 28, 2023 11:20 am

arcachofo

arcachofo

Hi.

Check that the baudrates, data bits and stop bits are the same everywhere and MCU frequency is correct.
And open the Serial Monitor of the MCU and also of the Serial Port to see what is really happening.

3Atmega8 USART RX/TX problem on pty Empty Re: Atmega8 USART RX/TX problem on pty Fri Apr 28, 2023 7:14 pm

unbreakmyheart



i think that the baud rate is finse because when AVR sends me incrementing counter full speed (1Mbps) its working, but when i sent some bytes, who's value >=0x20 it echoing me invalid data:

Atmega8 USART RX/TX problem on pty Screenshot-from-2023-04-28-19-56-52

Atmega8 USART RX/TX problem on pty Screenshot-from-2023-04-28-20-11-46

4Atmega8 USART RX/TX problem on pty Empty Re: Atmega8 USART RX/TX problem on pty Sun May 21, 2023 7:40 pm

arcachofo

arcachofo

I found an error in Atmega8 USART, it is probably related with this issue,
You can try the development version with this issue solved. Download here:
https://simulide.forumotion.com/t550-simulide-trunk-tester-builds#2961

In any case I think there are some problems in your code:

- Review your formula to calculate UBRR value, this one does not seem to make sense to me:
#define BAUDRATE ((F_CPU + (BAUD * 4)) / (BAUD *  - 1)  // U2X == 1

- To write to UCSRC you need to set URSEL high:
UCSRC = 1<<UCSZ0 | 1<<UCSZ1 | 1<<USBS | 1<<URSEL;   // 8bit 2stop data format

5Atmega8 USART RX/TX problem on pty Empty Re: Atmega8 USART RX/TX problem on pty Sun May 21, 2023 10:26 pm

unbreakmyheart



Problem solved in version you provided (R1671), thank you.
As to your comment about invalid flag (URSEL) it is indeed not valid in metal AVR and i must set it when uploading hex to my physical ATmega, but i found this flag is not working in simulator version 1.0.0-SR0_Lin64 furthermore it should not be set in order to make USART to work.
Those problems looks like are fixed now in version you provided above, thanks a lot)
Atmega8 USART RX/TX problem on pty Screenshot-from-2023-05-21-23-24-46

6Atmega8 USART RX/TX problem on pty Empty Re: Atmega8 USART RX/TX problem on pty Sun May 21, 2023 10:30 pm

arcachofo

arcachofo

Thank you for the feedback.

but i found this flag is not working in simulator version 1.0.0-SR0_Lin64 furthermore it should not be set in order to make USART to work.
Yes, this issue is already solved, keep an eye in next upload of 1.0.0 builds here:
https://simulide.forumotion.com/t390-simulide-1-0-0-tester-builds#1936

7Atmega8 USART RX/TX problem on pty Empty Re: Atmega8 USART RX/TX problem on pty Sun May 21, 2023 10:38 pm

unbreakmyheart



im also wanted to tell you that your simulator is really awesome!
one iteration - make some changes to C-code, compile, restart simulide and check inp/out on average is taking from me about 30 seconds.
the speed it allow me to develop is really incredible. i started making fun with atmega on my free time, just for fun, couple month ago and for today im happy with my new 200kHz oscilloscope for a buck) (gui is on DearPyGUI)
i tried multisim, proteus earlier and it was way harder and buggy

arcachofo and ilhan like this post

Sponsored content



Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum