Parity Check for SPI

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:

Parity Check for SPI

Post by Lord Grezington »

Hello All

If it have an int, say 113, this gives a binary value of 0b01110001. If we count the 1's we get an even number - but how do we impliment this in code?

I currently have an address of 5bits (say 10110), a data length of 10bits (0b0001110001) and the last bit needs to be the parity (needs to be Odd).

I have managed to combine the address bits and the data bits and put them in the correct position, but I am just stump on how to implement the last parity bit efficienty within the code.

Ie
0b1011000011100010

Parity is 0 added to the end.

Thanks

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Parity Check for SPI

Post by mnf »

Quickest is a lookup table - but depends on how much free memory you have.... (2 x 8 bit look ups?) (The value retrieved from table can even be byte with parity bit set - rather than count of 1s)

Shift and add (count = count + (n & 1); n = n >> 1 for no. of bits)?

Or with a bit of fancy math: - see https://stackoverflow.com/questions/381 ... -operators


And then just set bit 0 to 0 or 1 depending on result...

If (count & 1) result = value | 1 else result = value & 0xFE

Martin

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: Parity Check for SPI

Post by Lord Grezington »

Hi Martin

Thanks for the responce.

I have gone through all the options, I think the best option would be the shift and add method. I am still confused however how this coule be implemented in flowcode - is it possible you could do a very quick example?

Thanks

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Parity Check for SPI

Post by mnf »

Ok

A simple bit count macro - and a test 'main' that just counts the bits in random numbers and spits the result to UART (with a 1s delay to give you a chance to check the result :-) )

Note that I'm counting 16 bits (of a WORD) here - you can modify if needed to count 12 or ignore bit 0 as required....
BitCount.fcfx
(10.2 KiB) Downloaded 223 times
Martin
PS - I'd be tempted to try a lookup table that uses 16 entries (and lookup 4 bit nibbles) and compare speed over a large number of counts.
- and for fun, try a 256 byte lookup table as well.... - I'll have a play if I have some spare time later....

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Parity Check for SPI

Post by mnf »

I tried a small (16) byte lookup and depending on the loop type used it was either slightly faster or slightly slower than the rotate and count used above.

Using a 'for' loop was much quicker than a while or until at least on an Arduino mega2560...

Took approx 90s for 1million random number bit counts but generating the random numbers took approx 90s of this... (Bogosecs using a 0.95hz timer interrupt)

Using a 4bit lookup took 92s and above algorithm 95s using a while(n>0) test.

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: Parity Check for SPI

Post by Lord Grezington »

Hello Martin

I apologise for not responding sooner - I was side tracked onto a different project.

I will take a look at your example later today

Thanks

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Parity Check for SPI

Post by mnf »

No problem. Let me know if you have any questions...

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: Parity Check for SPI

Post by Lord Grezington »

Thanks Martin
Worked great, implemented well in the SPI

Post Reply