Calculating Parity

retepsnikrep

Senior Member
Lets assume I have a value which can be loaded into a variable b0 for instance.

How can I work out if it has an odd or even number of bits?

I need to work this out as I'm experimenting with AUSART transmission and need an even parity bit for my particular project.

I'll be using the 9th bit transmisison mode in the 18X AUSART to send a parity bit and the data manualy with pokes etc.

I'm using my Eeprom to store my data to be transmitted which can change and don't really want to use a lookup table unless I really have too.

I would prefer a routine which can determine if the byte has an even/odd number of bits.

Hippy I', grateful for your earlier idea but want to avoid going to the 28X1 or 2 to keep cost down a bit and make the board smaller.

I suppose just use the bit command to step throught the byte and add up the number of bits set to 1.

Then use if then to say

if number of bits set to 1 = 1,3,5,7 then odd
if number of bits set to 1 = 0,2,4,6,8 then even

Set my bit 9 parity bit then accordingly

Sound right?

Ideas?
 
Last edited:

SilentScreamer

Senior Member
No idea if this works, but put the byte that you want to know if there is an even or odd number of bits in into b0 then use this code:

Code:
main:
if bit7 = 1 then even
if bit6 = 1 then odd
if bit5 = 1 then even
if bit4 = 1 then odd
if bit3 = 1 then even
if bit2 = 1 then odd
if bit1 = 1 then even
if bit0 = 1 then odd
goto main


odd:
b1 = 255
goto main


even:
b1 = 0
goto main
I'm sure the initial checking can be optimised but I don't know how so I'll leave that to someone else.

EDIT - Just changed the code to check bit7 first.
EDIT2 - Changed the code again, it works now. Sorry for the initial not working code, I forgot about the simulator :p.
 
Last edited:

Jeremy Leach

Senior Member
I'd use the bit variables. For instance if the number to calc parity on is in b0, then:
Code:
BitsSet = bit0 + bit1 + bit2 + bit3 + bit4 + bit5 + bit6 + bit7
Odd = BitsSet//2 'Modulus. e.g 3 //2 = 1, 5//2 = 1.
 

lbenson

Senior Member
If your number is in b0, then I think this will work:

b0 = bit0+bit1+bit2+bit3+bit4+bit5+bit6+bit7

Then the value of bit0 will be 1 if odd, 0 if even. Note that, as Hippy has pointed out in prior posts, the result value (in b0 in this case) is not modified until the arithmetic calculation is completed.
 

hippy

Ex-Staff (retired)
I'd go with Jeremy's or lbenson's scheme, but I'd personally modify that to ...

bit8 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^bit 1 ^ bit0

With a "^ 1" on the end if you need to invert the parity bit.

That probably has little advantage over using "+" so that's just my preference, but also gives only 0 or 1 if the assigned variable is a byte or word.

For speed, I'd populate Data Eeprom and simply do "READ b0, bit8".
 

retepsnikrep

Senior Member
Thanks for those ideas, great.

Which one do we think is faster?

I'm running an 18x at 8mhz and need to transmit three bytes back to back (ish) 9600 baud, 8 data bits, even parity.

There is then a 100ms delay before I transmit the next 3 bytes, this repeats ad infinitum.

Whilst i'm waiting during the 100ms I can use that time time to work out the parity for the next three bytes and store it in RAM along with the data to be sent.

The actual transmission routine then has little to do apart from poll the ASUART ready registers, and put the values to be sent inc parity into the reg/s etc using pokes.

The code for this is from one of Hippys earlier threads on ASUART

UartTx: ;Ausart Transmit

peek TXSTA, Status ;Wait for TX Buffer Empty
if TRMT_BIT = 0 then UartTx ;If TX Buffer not Empty then check again

poke TXREG, Char ;Send the byte

return
 
Last edited:

Jeremy Leach

Senior Member
If you've got 100ms to play with then I'd guess any of the methods would work - but as Hippy suggests, if you load up 255 EEPROM locations with data then just do READ b0, bit8, this will be the fastest. That's if you've got the EEPROM to spare ;).
 

hippy

Ex-Staff (retired)
It's not the time between transmission of your three bytes which is important, but the time between having the data to send available or determined and sending it. It shouldn't be a problem, At 8MHz I guestimate calculating each parity bit takes around 1ms, compared to a guesstimate of 200us for a READ.
 

Jeremy Leach

Senior Member
Not relevant for you, but I've just noticed the NOB command on the X2 Picaxe, which returns the number of bits that are set.
 

retepsnikrep

Senior Member
Might have to move to 28X2 later.

But for now does this code make sense?

Code:
Parity:    ;Calculate Parity for byte b0 to be transmitted

	bit8 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^bit1 ^ bit0	
	TXSTA_INIT = TXEN | TX9 | BIT8
 

retepsnikrep

Senior Member
Many Thanks to all on this thread I now have my 18X + rs485 line driver project up and running driving my Honda Insight's Soc/Assist/Regen dashboard gauges. I'll post the code when I've tidied it up a bit/lot!

Might have some more questions about the receive side later.
 
Top