I am trying to write a software uart program where i have to send the
parity bit. The problem is I don't know how to check the parity of a
byte. If anyone could help I would greatly aprreciate it. I don't know
and c code so if someone could give me an example in assembly for pic or
for avr.
Jamey Schroeder wrote:
>Hello,
>
>I am trying to write a software uart program where i have to send the
>parity bit. The problem is I don't know how to check the parity of a
>byte. If anyone could help I would greatly aprreciate it. I don't know
>and c code so if someone could give me an example in assembly for pic or
>for avr.
>
Use the rotate command to shift the byte into the carry flag one
bit at a time, and count the number of 1s or 0s as you go.
>I am trying to write a software uart program where i have to send the
>parity bit. The problem is I don't know how to check the parity of a
>byte. If anyone could help I would greatly aprreciate it. I don't know
>and c code so if someone could give me an example in assembly for pic or
>for avr.
Make a lookup table of 16 entries, 0-15. Each entry contains the parity in that
index:
> From: Dan Michaels <spam_OUToricomTakeThisOuTUSWEST.NET>
> >
> >I am trying to write a software uart program where i have to send the
> >parity bit. The problem is I don't know how to check the parity of a
> >byte. If anyone could help I would greatly aprreciate it. I don't know
> >and c code so if someone could give me an example in assembly for pic or
> >for avr.
> >
>
> Use the rotate command to shift the byte into the carry flag one
> bit at a time, and count the number of 1s or 0s as you go.
/*
Byte parity calculation based on algorithm
published in Electronics Design Aug 22, 1991
by,
Noor Singh Khalsa (505) 667-0200
EG&G
P.O Box 809,
MS E-1, Los Alamos, NM 87544
Implementation copyright (c) 1991 Byte Craft Limited
421 King Street North, Waterloo, Ontario N2J 4E4
(519) 888 6911
*/
Wouldn't it be easier to count the number of 1's as they are transmitted?
The area of code that sets the output to send a '1' could also increment the
parity byte.
Clear ParityByte
Clear BitCount
Loop:
If BitCount = Parity (8) (bit 3 = 1) (btfsc BitCount,3)
goto SendParity
Rotate each bit out of the byte to be sent
If bit is '0'
call Send0Code
else
call Send1Code
increment BitCount
goto Loop
SendParity:
Test Bit 0 of ParityByte (btfss ParityByte,0)
If bit is '0'
goto Send0Code (and return from there)
else
goto Send1Code (and return from there)
Send1Code:
output a 1
increment parity byte
return
Send0Code:
output a 0
return
This is a bit garbled but hopefully it gives you the idea. AN555 from
Mcirochip also has a software Uart complete with parity using a seperate
parity generation routine.
With a software UART the easiest way to generate / check parity is to
maintain a flag bit with the current parity and 'xorwf' it with each data
bit as it is received/transmitted.
Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)
;This routine will leave the parity of X in X.0
;while blenderizing most of the rest of X
swapf X, W
xorwf X, F
rrf X, W
xorwf X, F
btfsc X, 2
incf X, F
> I am trying to write a software uart program where i have to send the
> parity bit. The problem is I don't know how to check the parity of a
> byte. If anyone could help I would greatly aprreciate it. I don't know
> and c code so if someone could give me an example in assembly for pic or
> for avr.
I see you've gotten some good responses to this question that answer how to
calculate the parity of a byte. I think you wanted this, but are you also
asking what parity is in the first place? I also noticed that most of the
responses only said "here, do this" whithout any explanation, so I'll try to
demystify things a bit.
Parity is a one-bit checksum for a string of bits. There are two kinds of
parity, even and odd. Even parity is the one-bit sum of all the bits, and
odd parity is the complement of that. A one-bit sum means the addition is
carried out in a one-bit word, which is the same as the least significant
bit of an addition using a larger word. This low bit therefore only
indicates whether the sum was odd (1) or even (0). Therefore the even
parity is 0 iff there are an even number of 1 bits in the string of bits
being tested.
Note that one-bit addition is the same as XOR (make a truth table for
addition and XOR if you're not conviced), so even parity can also be thought
of as the XOR of all the bits being tested. That is how the compact example
apparently from John Payson worked. First the high and low nibbles were
XORed. Each bit in each nibble is now the parity of itself and the same bit
in the other nibble. In other words, this reduces the problem to finding
the parity of one of the nibbles. This value is XORed again with itself
shifted right one bit. Bits 0 and 2 are now the parity of the upper and
lower half of the nibbles. Bit 0 is then incremented if bit 2 is set,
creating the combined parity in bit 0.
ODD parity is most useful for serial transmission like RS-232. This
guarantees that each 9 bit word (8 data bits, 1 parity bit) sent contains at
least one 0 bit and at least one 1 bit, which means all zeros or all ones is
definitely an error condition. Parity isn't used as much today because it
can only detect single bit errors. Most communications systems tend to use
checksums for blocks of data with some sort of ACK/NACK higher level
protocol.
[Start soapbox]
I was dissappointed by some responses that showed code snippets without any
comments or other explanation as to how they worked. The comment about
someone being a "genius" right after showing his undocumented code bothered
me most and prompted this diatribe. Yes, his algorithm was very clever, but
perpetuating bad code (undocumented code IS bad code, even more so since any
novice can easily avoid it) only serves to perpetuate it and give it an
implicit stamp of approval.
[end soapbox]
*****************************************************************
Olin Lathrop, embedded systems consultant in Devens Massachusetts
(978) 772-3129, olinKILLspamcognivis.com,http://www.cognivis.com
>I was dissappointed by some responses that showed code snippets without any
>comments or other explanation as to how they worked. The comment about
>someone being a "genius" right after showing his undocumented code bothered
>me most and prompted this diatribe. Yes, his algorithm was very clever, but
>perpetuating bad code (undocumented code IS bad code, even more so since any
>novice can easily avoid it) only serves to perpetuate it and give it an
>implicit stamp of approval.
In all fairness, though, micro-commenting can do more harm than good.
I'd be reasonably satisfied with the two lines of comments that *were*
supplied,
and I can figure the rest out in my head if need be.
My code typically has more comments than that, but mostly because I'm figuring
it out as I go.
Best regards,
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Spehro Pefhany --"it's the network..." "The Journey is the reward" .....speffKILLspam.....interlog.com Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog Info for designers: http://www.speff.com
Contributions invited->The AVR-gcc FAQ is at: http://www.bluecollarlinux.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> I was dissappointed by some responses that showed code snippets
> without any comments or other explanation as to how they worked.
> The comment about someone being a "genius" right after showing his
> undocumented code bothered me most and prompted this diatribe.
> Yes, his algorithm was very clever, but perpetuating bad code
> (undocumented code IS bad code, even more so since any novice can
> easily avoid it) only serves to perpetuate it and give it an
> implicit stamp of approval.
Olin:
;This routine will leave the parity of X in X.0
;while blenderizing most of the rest of X
swapf X, W ;x = abcdefgh w = efghabcd
xorwf X, F ;x = abcdefgh w = efghabcd
; xor efghabcd
rrf X, W ;x = abcdefgh w = -abcdefg
; xor efghabcd xor -efghabc
xorwf X, F ;x = abcdefgh w = -abcdefg
; xor efghabcd xor -efghabc
; xor -abcdefg
; xor -efghabc
; at this point, the parity for half the bits
; (a, b, e, and f) is in bit 2 of X, and the
; parity for the other half (bits c, d, g, and h)
; is in bit 0 of X.
btfsc X, 2 ; if the parity of (a,b,e,f) is 0,
; then the parity of (a,b,c,d,e,f,g,h)
; is equal to the parity of (c,d,g,h)...
; which is already in bit 0, so skip ahead.
incf X, F ; otherwise, the parity of (a,b,e,f) is 1,
; so the parity of (a,b,c,d,e,f,g,h) is
; NOT equal to the parity of (c,d,g,h).
; invert bit 0.
; at this point, bit 0 contains the parity of
; (a,b,c,d,e,f,g,h).
-Andy
=== Andrew Warren --- aiwspam_OUTcypress.com
=== Cypress Semiconductor Corporation
=== Interface Products Division, S.D.
===
=== The opinions expressed above do
=== not necessarily represent those of
=== Cypress Semiconductor Corporation.
>>I was dissappointed by some responses that showed code snippets without
any
>>comments or other explanation as to how they worked. The comment about
>>someone being a "genius" right after showing his undocumented code
bothered
>>me most and prompted this diatribe. Yes, his algorithm was very clever,
but
>>perpetuating bad code (undocumented code IS bad code, even more so since
any
>>novice can easily avoid it) only serves to perpetuate it and give it an
>>implicit stamp of approval.
>
>In all fairness, though, micro-commenting can do more harm than good.
>I'd be reasonably satisfied with the two lines of comments that *were*
>supplied, and I can figure the rest out in my head if need be.
>
>My code typically has more comments than that, but mostly because I'm
figuring
>it out as I go.
The code-writer himself might not require any comments because he know's
what
he is doing. But if you don't comment your code, please don't share it with
others
because someone else doesn't understand it (at least, you must assume this
otherwise the question wouldn't be ask in the first place).
Only share your code with someone you know is capable of reading it...
> The code-writer himself might not require any comments because he know's
> what
> he is doing. But if you don't comment your code, please don't share it with
> others
> because someone else doesn't understand it (at least, you must assume this
> otherwise the question wouldn't be ask in the first place).
> Only share your code with someone you know is capable of reading it...
Like people subscribed to PIClist...
Send more code, I learn more every time I try to decipher what someone
else has done and why. If I'm paying for code, I want it documented. If
someone else is sharing it just to help out, share the love of the hobby,
or whatever -- I don't care if it's commented or not. I certainly won't
complain about someone doing me a favor but not doing it to MY
specifications!
Dale
---
The most exciting phrase to hear in science, the one that heralds new
discoveries, is not "Eureka!" (I found it!) but "That's funny ..."
-- Isaac Asimov
>Like people subscribed to PIClist...
>
>Send more code, I learn more every time I try to decipher what someone
>else has done and why. If I'm paying for code, I want it documented. If
>someone else is sharing it just to help out, share the love of the hobby,
>or whatever -- I don't care if it's commented or not. I certainly won't
>complain about someone doing me a favor but not doing it to MY
>specifications!
I can certainly relate to how you feel about posted code. I too learn
everyday
in deciphering some-elses code. But you've (I assume this) got a
reasonable know-how about PICmicro's. Other's new to the list, or
new to microcontrollers, or new to programming, or whatever
might not be able to decipher it.
I certainly agree with you that if someone is doing you a favor that you
can't set specificatoins for it.
Here in Holland we've got a saying: "Je mag een gegeven paard niet in de bek
kijken".
Wich means that you may not complain about a present you've received :o)
This comment is directly mainly to "new" coders listening in
to this thread - to give some perspective.
The sort of thing going on on this thread is great fun, and it
helps you learn coding, expands your horizons, and helps make
your mind sharp
-- BUT --
from the viewpoint of someone who might "employ" others to do
programming, I would "not" want to employ someone who wrote really
clever code and never commented anything, or only minimally
commented it, and then ran off to his or her next job. Ultimately,
this would be a disaster.
The other half of coding is maintenance - trying to fix, modify,
append, or extend your own or someone else's code sometime down the
road - boo, hiss, about as much fun as cleaning the bathroom on a
saturday nite - but big companies spend a lot of time and $$$$
on this.
3 months after writing something "really clever", and leaving no
comments, just try coming back to it and try figuring out what it
does. [anyone who used to write in Forth according to the "accepted"
standard - ie, "you're supposed to do everything possible using
stack operations only" - will know what I mean].
The more clever and less straight-forward [ie, obvious] the code,
the more necessary the comments.
The corollary to this is that, code that can be read by only the
original coder gets thrown into the wastebasket the first time
someone else has to modify or extend it.
Otherwise, have fun, learn something new and clever - and look
at it from every perspective.
> I certainly agree with you that if someone is doing you a favor that you
> can't set specificatoins for it.
> Here in Holland we've got a saying: "Je mag een gegeven paard niet in de
bek
> kijken".
> Wich means that you may not complain about a present you've received :o)
>
> Jilles Oldenbeuving
The English (or at least American) version of this is:
"Don't look a gift horse in the mouth".
(ie: to check the condition of the teeth, and thus the age).
[not to imply that an old horse is worthless :-)]
Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)
If you just want the parity then count the number of 1's. There is a very fast
algorythm for this in the archives I think ...
If you want the parity while transmitting, just increment a counter every time
you send a '1' inside your serial routines. The 0 bit of the counter is the
parity (even). You invert it (XOR 1) to get odd parity, and then you send it.
You can get the RX parity the same way. Increment Count every time you
receive a '1'. After receiving all nine bits you should have a 0 in the bit
0 location of Count for even parity and a 1 for odd parity.
Dave Mumert
>
> If you just want the parity then count the number of 1's. There is a very
fast
> algorythm for this in the archives I think ...
>
> If you want the parity while transmitting, just increment a counter every
time
> you send a '1' inside your serial routines. The 0 bit of the counter is
the
> parity (even). You invert it (XOR 1) to get odd parity, and then you send
it.
>
> Peter