Page 1 of 1

9bit UART - Arduino Mega 2560

Posted: Sun Apr 28, 2024 10:14 am
by DStoyankov
Hello,
How to receive and send 9 bit data on the serial port - Arduino Mega 2560 ?

Re: 9bit UART - Arduino Mega 2560

Posted: Sun Apr 28, 2024 11:26 am
by chipfryer27
Hi

Add the UART / RS232 component found under Comms.

In the component properties you will find an option called Data Bits which can change bits being sent (I think it's 7 / 8 / 9).

Regards

Re: 9bit UART - Arduino Mega 2560

Posted: Sun Apr 28, 2024 12:30 pm
by mnfisher
... There is an issue - send and receive take a byte which is 8 bits, so some extra work needed..

- I might be wrong on this - looks like the functions now return an int.

Re: 9bit UART - Arduino Mega 2560

Posted: Sun Apr 28, 2024 4:11 pm
by DStoyankov
My idea is I get into a interrupt when receiving data, but how do I get to UCSRnB.

This is code from datasheet:

Code: Select all

C Code Example:
unsigned int USART_Receive( void )
{
unsigned char status, resh, resl;
/* Wait for data to be received */
while ( !(UCSRnA & (1<<RXCn)) )
;
/* Get status and 9th bit, then data */
/* from buffer */
status = UCSRnA;
resh = UCSRnB;
resl = UDRn;
/* If error, return -1 */
if ( status & (1<<FEn)|(1<<DORn)|(1<<UPEn) )
return -1;
/* Filter the 9th bit, then return */
resh = (resh >> 1) & 0x01;
return ((resh << 8) | resl);
}

Re: 9bit UART - Arduino Mega 2560

Posted: Sun Apr 28, 2024 9:43 pm
by mnfisher
A quick demo of 9 bit comms using 2 Arduino Megas.

One is the 'sender' (S) and one a 'receiver' (R) (I called it UART) (that echoes the received data to a PC via the USB comms UART)

I connected RX1 to TX1 and TX1 to RX1 on boards 'S' and 'R' - also GND (essential) and 5v as I have the sender powered by the receiver. The receiver is connected to the PC by USB and the PC is running a terminal program (in my case PuTTY on COM10)

The sender sends a constant stream of numbers (0.511 as it's 9bit) and the receiver spits them out to the PC every 10 numbers (note that it blocks further input while doing this so the results aren't quite contiguous blocks)
send.fcfx
(9.78 KiB) Downloaded 695 times
uart.fcfx
(17.15 KiB) Downloaded 667 times
Martin

NB - the interrupts are slightly confusing Rx1 is for Channel2 (software is channel 0 in FC) and the pins are marked as RX1 and TX1 (Channel 0 is hardware to USB in Arduino). To make matters worse - I set up the receive channel first so it is UART1 :-)

The two Megas communicate at 9600 baud - but the connection to the PC is 115200 to try and minimise characters 'dropped'

Re: 9bit UART - Arduino Mega 2560

Posted: Sun Apr 28, 2024 10:18 pm
by mnfisher
Note - could possibly have done the send and receive on the same Mega. I didn't think of it beforehand... I powered the second board from the first as I couldn't lay my hand on 2 of the Type A USB cables (though they are surely hiding somewhere)
Windows terminal programs don't support 9 bit data (the hardware doesn't). It is possible to produce 9 bit (using a parity bit) - but I couldn't find any terminal programs that support this (at least on a brief Google search)

My logic on blocks of 10 is a bit ropey too!

Re: 9bit UART - Arduino Mega 2560

Posted: Mon Apr 29, 2024 6:37 am
by DStoyankov
No, it isn't.
0.511 is a float type, 4 bytes.
A 9-bit symbol is of type byte + flag bit, that is, the maximum number is 511 - b111111111.
When making a buffer of 9-bit numbers with bytes - the odd ones are the 9th bit, that is, 0 or 1, and the even ones are the lower 8 bits, but the idea is that both bytes are sent in a packet at the same time - the odd one as the 9th bit flag, and the even one as byte with 8 bits.
In the UART component I can't access the 9th bit (or I don't know how).
The Arduino environment is also not set.

Re: 9bit UART - Arduino Mega 2560

Posted: Mon Apr 29, 2024 7:10 am
by mnfisher
Sorry - missed a '.' - I meant 0..511 (or 0 to 511)

Which version of Flowcode are you using - the components used to only return a byte (8bit) - in version 10 (free !) - the Send/Receive Char take an int (16bit) of which they send/receive the appropriate number of bits...

Did you manage to get my demo to work?

Re: 9bit UART - Arduino Mega 2560

Posted: Mon Apr 29, 2024 7:50 am
by DStoyankov
I am using version 10, with commercial license full package.

call_UART::RETURN - 16 bits : "16 bit data mode returns 0-255 for valid data, 256 for a timeout" - This is hint from combobox

Re: 9bit UART - Arduino Mega 2560

Posted: Mon Apr 29, 2024 7:59 am
by mnfisher
It does work - so I guess the 'hint' hasn't been updated. (As mentioned the component used to work with bytes)

Note - Use the Rx interrupt - data has been received (so it should never timeout?) I don't know what timeout would return in this case (but if you only receive single character in the interrupt (and you should) then it shouldn't occur anyway..

Looking at the UART code - it looks like it now returns 512 on timeout...