UART Transmission with atmega328p

For general Flowcode discussion that does not belong in the other sections.
ChrisT66
Posts: 103
http://meble-kuchenne.info.pl
Joined: Mon Dec 07, 2020 12:47 pm
Been thanked: 1 time

UART Transmission with atmega328p

Post by ChrisT66 »

Hello, I need some help with reading data via the UART interface.
I want to receive a 32Byte data packet which is sent with 115200 bps in 16bit packets (little endian). The first 16 bits package is a header, the following ones are 14x 16bit date + 16bit checksum.
I would like to use the atmega328p with 8MHz clock frequency.
Do I do this via the internal UART interface or with the software UART? And how? Maybe someone has a tip for me.

Thank you very much!

Greetings Chris

mnfisher
Valued Contributor
Posts: 1628
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 761 times

Re: UART Transmission with atmega328p

Post by mnfisher »

Use the hardware UART (115200 should be pushing it for software uart at 8MHz)
You need to use an interrupt and receive characters (or bytes) in the interrupt handler - which can stuff then into a buffer (an array or a circular buffer)
The interrupt handler can set a flag after the message is complete (but should just receive one byte per call - keep it quick)

Martin

ChrisT66
Posts: 103
Joined: Mon Dec 07, 2020 12:47 pm
Been thanked: 1 time

Re: UART Transmission with atmega328p

Post by ChrisT66 »

Hello Martin,

thanks for your message.
I have tried that, unfortunately no data is read in or only occasionally something wrong...

ChrisT66
Posts: 103
Joined: Mon Dec 07, 2020 12:47 pm
Been thanked: 1 time

Re: UART Transmission with atmega328p

Post by ChrisT66 »

Made a mistake, I'm testing on an Arduino UNO that runs at 16MHz instead of 8MHz as specified in the project... now I receive something, I have to check if it is something useful :)

chipfryer27
Valued Contributor
Posts: 1685
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 374 times
Been thanked: 583 times

Re: UART Transmission with atmega328p

Post by chipfryer27 »

Hi

Your chip shouldn't have much trouble in doing as you want so I'm guessing something in your chart isn't correct if you are having issues

Many examples of using a Circular Buffer with the UART in the forum, but if you care to post your chart we can have a look.

Regards

ChrisT66
Posts: 103
Joined: Mon Dec 07, 2020 12:47 pm
Been thanked: 1 time

Re: UART Transmission with atmega328p

Post by ChrisT66 »

Thank you, that's super nice! Attached is my little test programme. I output the first 10 data bytes on a small display to see what arrives.
The header is always 0x2040 then the 14 data records come in two byte packets (little endian).
I don't yet know exactly how I can filter this out.

Thank you
Attachments
TestUART.fcfx
(24.21 KiB) Downloaded 160 times

chipfryer27
Valued Contributor
Posts: 1685
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 374 times
Been thanked: 583 times

Re: UART Transmission with atmega328p

Post by chipfryer27 »

Hi

You could use a variable (e.g. count) that increment (then resets) in your main loop to reduce your duplicated code in displaying the first ten contents.

I'm not 100% sure on what you are trying to achieve. My understanding from above and your chart is that you are sending 32 bytes, and the fist two bytes are always the same 0x2040.

In your chart you have set up an interrupt that branches to RXin

In this branch you receive a byte, then test to see if it equals 64, if so you then attempt to receive the next 30 bytes, storing them in your array.

Is this correct?

Remember though that arrays start at position 0 but you are reading back from 1.

Regards

mnfisher
Valued Contributor
Posts: 1628
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 761 times

Re: UART Transmission with atmega328p

Post by mnfisher »

A simple test - I use a 'state machine' - it's either looking for 0x20 (first byte of header), 0x40 (second byte), data or waiting whilst main deals with the received data. Note if the header is received as (0x4020 - little endian as well as data) then swap bytes in the interrupt handler.
I converted the 30 bytes of data to 15 words (LSB first - little endian) and then display them (very slowly) on the display.

The display takes ~15s so multiple data blocks may be missed - it's just a test... Main (or elsewhere) needs to reset state to 0 to start looking for the next data packet.

Martin
Attachments
Uart2.fcfx
(19.66 KiB) Downloaded 159 times

ChrisT66
Posts: 103
Joined: Mon Dec 07, 2020 12:47 pm
Been thanked: 1 time

Re: UART Transmission with atmega328p

Post by ChrisT66 »

Hello Martin,

many thanks for your demo chart. It works great so far, only the results of the data do not fit. The values should be between 1000 and 2000. Do I have a problem with the UART transmission?
Attached is an example chart of what I would like to receive.

Best regards Chris
Attachments
Bus.jpg
Bus.jpg (34.55 KiB) Viewed 4087 times

mnfisher
Valued Contributor
Posts: 1628
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 761 times

Re: UART Transmission with atmega328p

Post by mnfisher »

How do you mean 'values do not fit'?

I had a brief attempt to convert to 16 bit values (which should allow the data range):

.dataRcvd[.i] = payload[.i *2 +1]
.dataRcvd[.i] = (.dataRcvd[.i] << 8) + payload[.i *2]

Did I mess this up (did i make dataRcvd byte for example)?

Martin

Post Reply