UART CRC Check

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Lord Grezington
Flowcode V4 User
Posts: 288
Joined: Wed Nov 02, 2011 11:15 pm
Has thanked: 29 times
Been thanked: 30 times
Contact:

UART CRC Check

Post by Lord Grezington »

Hello Everyone...

I have this project where I am trying to communicate with an IC that has a difficult CRC check. Anyone have any idea how I would implement this with Flowcode?

Thanks
CRC.jpg
CRC.jpg (90.67 KiB) Viewed 4605 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: UART CRC Check

Post by Benj »

Hello,

Yes that should be fine.

First create a byte variable named CRC and initialise it to 0.

Next create a loop icon that will run 8 times, you need to run this loop for every byte you send or receive so it might make sense to put the loop in a macro you can call when sending or receiving bytes. Next create a variable named data you might want to make it local to the macro if you created one.

You can simply do the following using a decision and calculation icons inside the loop. Remember to load the .Data variable with the byte value that's been received or ready to transmit before the loop.

Code: Select all

Decision:  (CRC >> 7) ^ (.Data & 0x01)
Yes: Calculation: CRC = (CRC << 1) ^ 0x07
No: Calculation: CRC = (CRC << 1)

Calculation: .Data = .Data >> 1

Lord Grezington
Flowcode V4 User
Posts: 288
Joined: Wed Nov 02, 2011 11:15 pm
Has thanked: 29 times
Been thanked: 30 times
Contact:

Re: UART CRC Check

Post by Lord Grezington »

Hi Ben

Works great, thanks again...

Brendan
Posts: 243
Joined: Tue Nov 27, 2012 12:53 pm
Location: Cambridge, UK
Has thanked: 140 times
Been thanked: 118 times
Contact:

Re: UART CRC Check

Post by Brendan »

As I use CRCs with Flowcode a lot (particularly Maxim 1-wire, etc), and assuming you've got 256 Bytes of program memory to spare for an 8-bit CRC, I'd like to chip in with a popular alternative of pasting a 256-byte table (relevant to your CRC polynomial) to the Flowcode LUT component.

Such CRC tables may be published by manufacturers in application notes, or can be found on the internet - though always wise to test/debug with an arbitrary Byte string and see if your result concurs with an online calculator.

The primary benefit to this approach is speed, particularly when streaming out/in indeterminate amounts of data with limited PIC speed...

CurrentCRC XOR DataByte = LUTindex, the returned Byte from that location being the new CRC.

Such an approach also applies to 16-bit CRCs and higher, with code space trade-off and appropriate Byte-shifting, but where speed gain becomes even more noticeable.

All the best,
Brendan

Post Reply