Page 1 of 1

Arduino Uart Parity

Posted: Wed Nov 22, 2023 8:36 pm
by Alan_37
Hi

I need to to use the UART with even parity enabled on an Atmega328p
anyone know what C code I need to add please ?

Found the following on the web but don't know how to implement for flowcode
seems that register UCSR0C is responsible for parity but don't know how to .

void serial_init ( unsigned short ubrr ) {
UBRR0 = ubrr ; // Set baud rate
UCSR0B |= (1 << TXEN0 ); // Turn on transmitter
UCSR0B |= (1 << RXEN0 ); // Turn on receiver
UCSR0C = (3 << UCSZ00 ); // Set for async . operation , no parity ,
// one stop bit , 8 data bits
}

Re: Arduino Uart Parity

Posted: Wed Nov 22, 2023 9:19 pm
by mnfisher
From the datasheet:
2023-11-22_21-12-56.jpg
2023-11-22_21-12-56.jpg (112.82 KiB) Viewed 4572 times
So you need to set bits 4 and 5 correctly,

UCSR0C |= 1 << UPM01; // Set UPM01 to 1
UCSR0C &= ~(1 << UPM00); // Clear UPM00


Martin

Re: Arduino Uart Parity

Posted: Wed Nov 22, 2023 9:26 pm
by Alan_37
Hi Martin ,

Thanks for your fast reply , so I put the 2 lines in a c code
and put it after INIT uart is that right ?

I try it and let you know

Thanks again

Re: Arduino Uart Parity

Posted: Wed Nov 22, 2023 9:29 pm
by mnfisher
Maybe - you'll probably need to check the UPE flag in UCSR0A to check for errors? If it works :-)

Re: Arduino Uart Parity

Posted: Wed Nov 22, 2023 9:32 pm
by Alan_37
Hello Again ,

It works perfectly !! :)

Thanks so much for your help

Re: Arduino Uart Parity

Posted: Wed Nov 22, 2023 9:37 pm
by mnfisher
That's great - thanks for the fast feedback too....