Page 1 of 1
Parity Check for SPI
Posted: Wed Aug 14, 2019 1:54 pm
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
Re: Parity Check for SPI
Posted: Wed Aug 14, 2019 2:04 pm
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
Re: Parity Check for SPI
Posted: Wed Aug 14, 2019 3:05 pm
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
Re: Parity Check for SPI
Posted: Wed Aug 14, 2019 5:59 pm
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....
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....
Re: Parity Check for SPI
Posted: Thu Aug 15, 2019 8:02 am
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.
Re: Parity Check for SPI
Posted: Fri Aug 16, 2019 11:30 am
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
Re: Parity Check for SPI
Posted: Fri Aug 16, 2019 3:59 pm
by mnf
No problem. Let me know if you have any questions...
Re: Parity Check for SPI
Posted: Fri Sep 13, 2019 2:44 pm
by Lord Grezington
Thanks Martin
Worked great, implemented well in the SPI