Hello,
How to receive and send 9 bit data on the serial port - Arduino Mega 2560 ?
9bit UART - Arduino Mega 2560
-
- Posts: 12
- http://meble-kuchenne.info.pl
- Joined: Sun Feb 26, 2023 12:44 pm
- Been thanked: 1 time
-
- Valued Contributor
- Posts: 1201
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 289 times
- Been thanked: 424 times
Re: 9bit UART - Arduino Mega 2560
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
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
-
- Valued Contributor
- Posts: 1008
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 106 times
- Been thanked: 520 times
Re: 9bit UART - Arduino Mega 2560
... 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.
- I might be wrong on this - looks like the functions now return an int.
-
- Posts: 12
- Joined: Sun Feb 26, 2023 12:44 pm
- Been thanked: 1 time
Re: 9bit UART - Arduino Mega 2560
My idea is I get into a interrupt when receiving data, but how do I get to UCSRnB.
This is code from datasheet:
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);
}
-
- Valued Contributor
- Posts: 1008
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 106 times
- Been thanked: 520 times
Re: 9bit UART - Arduino Mega 2560
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)
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'
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)
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'
-
- Valued Contributor
- Posts: 1008
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 106 times
- Been thanked: 520 times
Re: 9bit UART - Arduino Mega 2560
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!
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!
-
- Posts: 12
- Joined: Sun Feb 26, 2023 12:44 pm
- Been thanked: 1 time
Re: 9bit UART - Arduino Mega 2560
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.
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.
- Attachments
-
- 9bits.PNG (14.71 KiB) Viewed 1134 times
-
- Valued Contributor
- Posts: 1008
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 106 times
- Been thanked: 520 times
Re: 9bit UART - Arduino Mega 2560
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?
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?
-
- Posts: 12
- Joined: Sun Feb 26, 2023 12:44 pm
- Been thanked: 1 time
Re: 9bit UART - Arduino Mega 2560
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
call_UART::RETURN - 16 bits : "16 bit data mode returns 0-255 for valid data, 256 for a timeout" - This is hint from combobox
-
- Valued Contributor
- Posts: 1008
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 106 times
- Been thanked: 520 times
Re: 9bit UART - Arduino Mega 2560
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...
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...