Searching \ for '[PIC]: Checking the parity of a byte?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Checking the parity of a byte?'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Checking the parity of a byte?'
2000\10\10@102451 by Jamey Schroeder

flavicon
face
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.

Thanks, Jamey

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics




2000\10\10@115613 by Dan Michaels

flavicon
face
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.

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics




2000\10\10@122705 by Andrew Kunz

flavicon
face
>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:

UINT8 partbl[] = {
    0,   // 0000
    1,   // 0001
    1,   // 0010
    0,   // 0011
    1,   // 0100
...
    0};  // 1111


Now get the parity of a byte by adding the parities of the two nybbles:

    parity = partbl[(x >> 4) & 0x0f] + partbl[x & 0x0f];

The LSbit holds parity.

In assembly:

partbl    addwf     PCL,f
    retlw     0    ; 0000
    retlw     1    ; 0001
    ...
    retlw     0


    swapf     BYTE,w
    andlw     0fh
    call partbl
    movwf     parity
    movlw     0fh
    andwf     BYTE,w
    call partbl
    addwf     parity,f

Andy

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics




2000\10\10@123920 by Walter Banks

picon face
Attached is a simple parity calculation that is quicker than the shift and
add algorithms.


Walter Banks
----------
{Quote hidden}

/*
  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
*/

char parity(char ch)
 {
  registera ac;
  ch = (ch >> 1) ^ ch ;
  return (((ac = (((ch >> 2) ^ ch)) & 0x11) == 0) ? 1 : ((ac ^ 0x11) == 0)
);
  return 0;
 }

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics




2000\10\10@131619 by Dave Mumert
flavicon
face
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.

Dave Mumert


{Original Message removed}

2000\10\10@132042 by Bob Ammerman

picon face
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)


{Original Message removed}

2000\10\10@132918 by jamesnewton

face picon face
from
http:/http://www.piclist.com/../microchip/math/bit/parity


PIC   Microcontroller Bit   Math Method

Parity

From: John Payson

;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

P.S. Payson was a genius.

---
James Newton (PICList Admin #3)
.....jamesnewtonKILLspamspam@spam@piclist.com 1-619-652-0593
PIC/PICList FAQ: http://www.piclist.com or .org

{Original Message removed}

2000\10\10@151640 by Bob Blick

face
flavicon
face
> P.S. Payson was a genius.

Probably still is. Wonder who he's sharing his genius with now?

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics




2000\10\10@160737 by Olin Lathrop

flavicon
face
> 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, olinspamKILLspamcognivis.com, http://www.cognivis.com

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics




2000\10\10@164515 by Spehro Pefhany

picon face
At 04:04 PM 10/10/00 -0400, you wrote:

>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"
.....speffKILLspamspam.....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
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics




2000\10\10@172831 by Andrew Warren

flavicon
face
Olin Lathrop <EraseMEPICLISTspam_OUTspamTakeThisOuTMITVMA.MIT.EDU> wrote:

> 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 --- aiwspamspam_OUTcypress.com
=== Cypress Semiconductor Corporation
=== Interface Products Division, S.D.
===
=== The opinions expressed above do
=== not necessarily represent those of
=== Cypress Semiconductor Corporation.

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics




2000\10\10@174537 by jamesnewton

face picon face
I've update the page to reflected everyone's comments and given credit where
due. Thanks and well done.

---
James Newton (PICList Admin #3)
@spam@jamesnewtonKILLspamspampiclist.com 1-619-652-0593
PIC/PICList FAQ: http://www.piclist.com or .org

{Original Message removed}

2000\10\11@004611 by Jilles Oldenbeuving

flavicon
face
>>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...

My $0,02

Regards,

Jilles Oldenbeuving
KILLspamjillesKILLspamspamrendo.dekooi.nl

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.




2000\10\11@010346 by Dale Botkin

flavicon
face
On Wed, 11 Oct 2000, Jilles Oldenbeuving wrote:

> 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

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.




2000\10\11@012213 by Jilles Oldenbeuving

flavicon
face
>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)

Jilles Oldenbeuving

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.




2000\10\11@020344 by Dan Michaels

flavicon
face
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.

hope this makes some sense,
- dan michaels

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.




2000\10\11@082423 by Bob Ammerman

picon face
> 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)

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.




2000\10\11@175510 by me

picon face
>various solutions

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

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.




2000\10\11@180354 by Dave Mumert

flavicon
face
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

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.




More... (looser matching)
- Last day of these posts
- In 2000 , 2001 only
- Today
- New search...