Searching \ for '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/index.htm?key=byte
Search entire site for: 'byte?'.

No exact or substring matches. trying for part
PICList Thread
'Efficient way to reverse a byte?'
1999\10\29@095055 by Jim Main

flavicon
face
I'm sure I saw an easy way to reverse a byte some time ago in this list, but
I've forgotten it..

I want to change   b7 b6 b5 b4 b3 b2 b1 b0   to  b0 b1 b2 b3 b4 b5 b6 b7.

Quickest & most code efficient??

Jim
---

1999\10\29@100127 by Wagner Lipnharski

picon face
A nibble table look ahead, just 16 bytes, or 8 if you accept to deal
with 4 bits tables.
For top possible speed at cost of some memory, a 256 bytes look ahead
table.
Wagner

Jim Main wrote:
{Quote hidden}

1999\10\29@110228 by Adam Bryant

picon face
Jim,
Your choices are likely to be fast execution and big code usage OR slow
execution and small code usage.

As Wagner suggested, fastest would be a 256 byte lookup table (but it
uses up 256 bytes for  the lookup table).

A small, slower approach is to rotate the source byte right one bit
(putting the rightmost bit in the carry flag), then rotate the
destination byte left one bit (putting the carry flag in the rightmost
bit of your destination register).  Repeat this cycle 8 times.  Your
destination byte will have to be a temp register, then when done move the
temp register back to the original source register.  I would estimate
this could be done in 10 to 15 bytes of code and 50 to 70 instruction
cycles.

Obviously I am thinking like the C programmer I am and someone like
Dimitry could probably give you a method for accomplishing the same thing
in 5 bytes of code and 10 cycles. :-)

Hope this helps,
Adam

On Fri, 29 Oct 1999 14:48:54 +0100 Jim Main <spam_OUTj.mainTakeThisOuTspamNET.NTL.COM> writes:
{Quote hidden}

Adam Bryant (age 0x23)
.....abryantKILLspamspam@spam@peaktech.com (work)
adamdbspamKILLspamjuno.com (home)
Parker, CO, USA
Robotics, RC Airplanes, anything using a PIC

___________________________________________________________________
Get the Internet just the way you want it.
Free software, free e-mail, and free Internet access for a month!
Try Juno Web: dl.http://www.juno.com/dynoget/tagj.

1999\10\29@111304 by Douglas Burkett

flavicon
face
If I'm not mistaken you could use a 16 byte lookup for the nybbles and then
use the efficency of the swap instruction, quicker I believe than rotating
all 8 bits and less code space than the full lookup table.

Doug

{Original Message removed}

1999\10\29@112521 by Dan Larson

flavicon
face
I colleague of mine has extoled the virtues of
using lookup tables as a means of improving
speed for as long as I can remember.  Just about
every time I have ever asked him, "How can I make
this faster", he usually replies: "use a table".

Ever wonder how they got so many of those video
games on the 1Mhz "pre-Nintendo-era" game units
to animate smoothly?

I tend to agree that this technique is often
underused.  But, Of course, there are plenty of
situations where the use of a table may not be
practical.

Just my $.02

Dan

On Fri, 29 Oct 1999 17:12:13 +0200, Douglas Burkett wrote:

>If I'm not mistaken you could use a 16 byte lookup for the nybbles and then
>use the efficency of the swap instruction, quicker I believe than rotating
>all 8 bits and less code space than the full lookup table.
>
>Doug
>
>{Original Message removed}

1999\10\29@113138 by Wagner Lipnharski

picon face
Adam Bryant wrote:
> A small, slower approach is to rotate the source byte right one bit
> (putting the rightmost bit in the carry flag), then rotate the
> destination byte left one bit (putting the carry flag in the rightmost
> bit of your destination register).  Repeat this cycle 8 times.  Your
> destination byte will have to be a temp register, then when done move the
> temp register back to the original source register.  I would estimate
> this could be done in 10 to 15 bytes of code and 50 to 70 instruction
> cycles.
>
> Obviously I am thinking like the C programmer I am and someone like
> Dimitry could probably give you a method for accomplishing the same thing
> in 5 bytes of code and 10 cycles. :-)

Adam is correct about the options, but even in assembler:

1) A direct flow routine:
   8 instructions shifting bits out
   8 instructions shifting bits in
   1 instruction swapping bytes
       Total Program Instructions:  17
       Total Instructions Executed: 17

2) A loop routine:
   1 instruction setting loop count = 8
   1 instruction shifting bit out
   1 instruction shifting bit in
   1 instruction decrementing count and looping back
   1 instruction to swap bytes
       Total Program Instructions:  5
       Total Instructions Executed: 26

3) Lookahead table (16 bytes of nibbles):
   1 instruction to set the lookahead base address
   1 instruction to save the original byte
   1 instruction to AND the original byte lower nibble
   1 instruction to get the table nibble
   1 instruction to save this table byte
   1 instruction to retrieve the original byte
   1 instruction to swap original byte nibbles
   1 instruction to AND the original byte lower nibble
   1 instruction to get the table nibble
   1 instruction to OR with previous table nibble
   1 instruction to swap final byte nibbles
        Total Program Instructions: 11 + 16 (table)
        Total Instructions Executed: 11

4) Lookahead table (256 bytes):
   1 instruction to set the lookahead base address
   1 instruction to get the table byte
        Total Program Instructions: 2 + 256
        Total Instructions Executed: 2

As you can see, execution speed cost a lot, always.

1999\10\29@115303 by Jim Main

flavicon
face
> 3) Lookahead table (16 bytes of nibbles):
>     1 instruction to set the lookahead base address
>     1 instruction to save the original byte
>     1 instruction to AND the original byte lower nibble
>     1 instruction to get the table nibble
>     1 instruction to save this table byte
>     1 instruction to retrieve the original byte
>     1 instruction to swap original byte nibbles
>     1 instruction to AND the original byte lower nibble
>     1 instruction to get the table nibble
>     1 instruction to OR with previous table nibble
>     1 instruction to swap final byte nibbles
>          Total Program Instructions: 11 + 16 (table)
>          Total Instructions Executed: 11

I made it  Total Program Instructions: 7 + 19 (table)
             &Total Instructions executed: 7

eg.

       movf    original,w
       call      rev_nibble
       movwf result
       swapf     result,f
       swapf     original,w
       call      rev_nibble
       iorwf    result,f

 rev_nibble
       andlw    0x0F
       addwf    pcl,f
       retlw       00
       retlw       08

           ....etc...

Thanks for all the suggestions..

jim

1999\10\29@135151 by Dwayne Reid

flavicon
face
>I'm sure I saw an easy way to reverse a byte some time ago in this list, but
>I've forgotten it..
>
>I want to change   b7 b6 b5 b4 b3 b2 b1 b0   to  b0 b1 b2 b3 b4 b5 b6 b7.

I've seen a lot of replies, but to me, the simple 'brute force and
ignorance' technique seems to make the most sense.

   clrf    dest
   btfsc   source,0
     bsf   dest,7
   btfsc   source,1
     bsf   dest,6
   btfsc   source,2
     bsf   dest,5
   btfsc   source,3
     bsf   dest,4
   btfsc   source,4
     bsf   dest,3
   btfsc   source,5
     bsf   dest,2
   btfsc   source,6
     bsf   dest,1
   btfsc   source,7
     bsf   dest,0

17 instructions, 17 cycles

dwayne


Dwayne Reid   <.....dwaynerKILLspamspam.....planet.eon.net>
Trinity Electronics Systems Ltd    Edmonton, AB, CANADA
(780) 489-3199 voice          (780) 487-6397 fax

Celebrating 15 years of Engineering Innovation (1984 - 1999)

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Do NOT send unsolicited commercial email to this email address.
My posting messages to Usenet neither grants consent to receive
unsolicited commercial email nor is intended to solicit commercial
email.

1999\10\29@142252 by Terry A. Steen

flavicon
face
How about:

    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in
    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in
    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in
    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in
    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in
    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in
    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in
    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in

Kind of long, but this should reverge arg1 and store it in arg2. At the
same time, it will also reverse arg2 and store it in arg1.

this is untried... give it a go egh?

Terry

{Quote hidden}

-----------------------------------------------------------------
Sent from the desk of:
Terry Allen Steen, EE              engineeringspamspam_OUTmarinapower.com
332 McLaws Circle, Ste 111         757-258-8800 (Voice)
Williamsburg, Va   23185           757-258-8805 (FAX)
-----------------------------------------------------------------
!I AM A WHALE MAIL USER! If you have a large file to send, goto
   http://www.whalemail.com    send them to:   mplengineer
       Give me your account and I will use it also
-----------------------------------------------------------------
Visit our web-site: http://www.marinapower.com
-----------------------------------------------------------------

1999\10\29@150614 by Tracy Smith

picon face
>I want to change   b7 b6 b5 b4 b3 b2 b1 b0   to  b0
b1 b2 b3 b4 b5 b6 b7.
>
>Quickest & most code efficient??

How about this... (John Payson)

       movwf   source
       btfsc   source,0
        xorlw  h'41'
       btfsc   source,6
        xorlw  h'41'
       btfsc   source,1
        xorlw  h'22'
       btfsc   source,5
        xorlw  h'22'
       btfsc   source,2
        xorlw  h'14'
       btfsc   source,4
        xorlw  h'14'

Why waste time on lookup tables? Each access is a 6
cycle hit.

.lo

=====

__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com

1999\10\29@171106 by paulb

flavicon
face
Tracy Smith wrote:

> How about this... (John Payson)

       movwf   source      a   b   c   d   e   f   g   h
       btfsc   source,0
        xorlw  h'41'       a  b+h  c   d   e   f   g   0
       btfsc   source,6
        xorlw  h'41'       a   h   c   d   e   f   g   b
       btfsc   source,1
        xorlw  h'22'       a   h  c+g  d   e   f   0   b
       btfsc   source,5
        xorlw  h'22'       a   h   g   d   e   f   c   b
       btfsc   source,2
        xorlw  h'14'       a   h   g  d+f  e   0   c   b
       btfsc   source,4
        xorlw  h'14'       a   h   g   f   e   d   c   b

 Cute, but you haven't completed the reverse - a rotate *without*
carry is required.  That's going to take two more cycles at least.
--
 Cheers,
       Paul B.

1999\10\29@174236 by Edson Brusque

face
flavicon
face
John Payson,

   Are you sure this works? I've tried it with CC5X and haven't sucess.

       uns8 source;
       MOVLW (0b.1101.1001); // this value is just for test
       MOVWF (source);
       BTFSC (source.0);
       XORLW (0x41); // 0100.0001
       BTFSC (source.6);
       XORLW (0x41); // 0100.0001
       BTFSC (source.1);
       XORLW (0x22); // 0010.0001
       BTFSC (source.5);
       XORLW (0x22); // 0010.0001
       BTFSC (source.2);
       XORLW (0x14); // 0001.0100
       BTFSC (source.4);
       XORLW (0x14); // 0001.0100

   It is very ingenious, but seens to have a bug somewhere.

   Regards,

   Brusque
___________________________________________________________________________
| | || |\| | || || |\|\ Edson Brusque :-^= (@spam@brusqueKILLspamspamflynet.com.br)
| | || ||| | || || |||| Musician, Tech Consultant, Programmer, Developer
| |_||_||| |_||_||_|||| Blumenau / SC / Brazil / Earth / Solar Syst /
Milk...
| \_\\_\|| \_\\_\\_\||| Giro In'Italia homepage: http://www.flynet.com.br/giro
|  |  |  |  |  |  |  || C.I.Tronics Lighting Designers: http://www.citronics.com.br
|__|__|__|__|__|__|__||----------------- ICQ# 15937748 ---------------------
\__\__\__\__\__\__\__\|    The SoundFont Users Group Mailing List is at
----------------------|  www.geocities.com/SiliconValley/Port/6619/
----------------------------------------------------------------------------

1999\10\29@181113 by Keith Causey

picon face
This would probably waste too much port space but you could reverse the 'w'
register in two instructions if you wired two ports together like this:
RA0->RB7
RA1->RB6
RA2->RB5
RA3->RB4
RA4->RB3
RA5->RB2
RA6->RB1
RA7->RB0
and executed the code sequence
mov  RA,W
mov  w,RB

1999\10\29@185302 by Russell McMahon

picon face
The piece of code that Tracy posted (from John Payson) does what it was
originally meant to do BUT is NOT correct for the present problem.

Here we require
76543210 --> 01234567

John's solution is for a 7 bit swap ignoring B7.
ie x6543210 --> x0123456

As a consequence B7 & B3 is untouched and there are only 3 swaps needed
making the code shorter.
Rewriting it for an 8 bit swap as required here, produces -

       movwf   source

       btfsc   source,0
        xorlw  h'81'
       btfsc   source,7
        xorlw  h'81'
       btfsc   source,1
        xorlw  h'42'
       btfsc   source,6
        xorlw  h'42'
       btfsc   source,2
        xorlw  h'24'
       btfsc   source,5
        xorlw  h'24'
       btfsc   source,3
        xorlw  h'18'
       btfsc   source,4
        xorlw  h'18'

17 instructions, time varies depending on data but fairly quick.

Operation may not be initially obvious.
It implements
   For N = 0 to 3
       If bN = 1 then invert bits bN and b(7-N)
       If b(7-N) = 1 then invert bits bN and b(7-N)
   Next N

Worst case, if both bits in a pair are set it inverts them both twice.
Best case, if neither is set it takes no action.
This suggests that a more cunning test for "both bits set" may allow a small
reduction in run time but intuition suggests that the testing overhead would
be excessive..

John's solution is elegant but a  little hard on the brain.
I prefer Dwayne Reids more brute force -

{Quote hidden}

because it's much easier for trhe average person to see what is happening at
a glance (or a few glances :-) - YMMV).

A full 8 bit lookup table is much quicker and much much less code space
efficient.


A dual nibble lookup table is not much quicker on average and rather less
code efficient.


Lookup tables have the possible advantage of having a constant
implementation time.


     Russell McMahon
_____________________________

>From another world - http://www.easttimor.com

What can one man* do?
Help the hungry at no cost to yourself!
at  http://www.thehungersite.com/

(* - or woman, child or internet enabled intelligent entity :-))

From: Tracy Smith <KILLspamdot_loKILLspamspamYAHOO.COM>

{Quote hidden}

1999\10\29@191917 by Tracy Smith

picon face
> John Payson,

<blush>
I'm NOT John Payson
</blush>


{Quote hidden}

Well it doesn't have a bug, it was intended to reverse
seven bits and not eight. I'm no Payson, but it looks
like for one more cycle you could do this:

       rrf     source,w
       rrf     source,w
       btfsc   source,0
        xorlw  h'41'
       btfsc   source,6
        xorlw  h'41'
       btfsc   source,1
        xorlw  h'22'
       btfsc   source,5
        xorlw  h'22'
       btfsc   source,2
        xorlw  h'14'
       btfsc   source,4
        xorlw  h'14'

.lo


=====

__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com

1999\10\29@194305 by Harold M Hallikainen

picon face
On Fri, 29 Oct 1999 15:10:14 -0700 Keith Causey <RemoveMEffightTakeThisOuTspamGEOCITIES.COM>
writes:
>This would probably waste too much port space but you could reverse
>the 'w'
>register in two instructions if you wired two ports together like
>this:
>RA0->RB7
>RA1->RB6
>RA2->RB5
>RA3->RB4
>RA4->RB3
>RA5->RB2
>RA6->RB1
>RA7->RB0
>and executed the code sequence
>mov  RA,W
>mov  w,RB

       Aha!  A hardware solution!  Just like adding a multiplier to the
chip!  Here's bit reverser hardware...  I normally am squeezing
everything I can out of I/O though...

Harold


Harold Hallikainen
spamBeGoneharoldspamBeGonespamhallikainen.com
Hallikainen & Friends, Inc.
See the FCC Rules at http://hallikainen.com/FccRules and comments filed
in LPFM proceeding at http://hallikainen.com/lpfm

___________________________________________________________________
Get the Internet just the way you want it.
Free software, free e-mail, and free Internet access for a month!
Try Juno Web: dl.http://www.juno.com/dynoget/tagj.

1999\10\29@194944 by bill

flavicon
face
> John's solution is elegant but a  little hard on the brain.
> I prefer Dwayne Reids more brute force -

> because it's much easier for trhe average person to see what is happening at
> a glance (or a few glances :-) - YMMV).

The shift-out-shift-in method is also pretty easy to follow.  It is also 1
instruction shorter because you don't have to clear the destination register
at the beginning.

> Lookup tables have the possible advantage of having a constant
> implementation time.

The test-each-bit method and the shift-out-shift-in method also have constant
execute times.


---
                                       Peace,
                                       William Kitchen
                                       TakeThisOuTbillEraseMEspamspam_OUTiglobal.net

The future is ours to create.

1999\10\29@223238 by Dmitry Kiryashov

flavicon
face
Greetings everybody. ;-)

Tracy Smith wrote:
>
> >I want to change   b7 b6 b5 b4 b3 b2 b1 b0   to  b0
> b1 b2 b3 b4 b5 b6 b7.
> >
> >Quickest & most code efficient??
>
> How about this... (John Payson)
>
>         movwf   source

       movfw   source  ;otherwise you put W to source

>         btfsc   source,0
>          xorlw  h'41'
>         btfsc   source,6
>          xorlw  h'41'

ok. swap 0 with 6

>         btfsc   source,1
>          xorlw  h'22'
>         btfsc   source,5
>          xorlw  h'22'

ok. swap 1 with 5

>         btfsc   source,2
>          xorlw  h'14'
>         btfsc   source,4
>          xorlw  h'14'

ok. swap 2 with 4

bit 3 is already on right place.

Here  is another reduced version:

       swapf   source,w        ;3210.654
       btfsc   source,3        ;swap bit 3 back
        xorlw  0x44            ;bit 7 is expected = 0
       btfsc   source,6
        xorlw  0x05
       btfsc   source,4        ;
        xorlw  0x05
       btfsc   source,2
        xorlw  0x50
       btfsc   source,0
        xorlw  0x50

I believe that there is more quick and compact version of this
(without ever table lookups) but I've no time to discover that.

WBR Dmitry.

1999\10\30@065447 by gdaniel

flavicon
face
Keith Causey wrote:

> This would probably waste too much port space but you could reverse the 'w'
> register in two instructions if you wired two ports together like this:

Alternately, wait for this beast:
http://www.atmel.com/atmel/products/prod39.htm
(it's an AVR with dual UART, no ADC, FPGA on chip) looks good for custom
protocol interfacing at high speed, multiple channel, Video processing ....
I want one to play with now, it would make a beaut logic analyser, just add
SIMM & graphics LCD
regards,
Graham Daniel.

1999\10\31@073516 by w. v. ooijen / f. hanneman

picon face
Just out of my head, untested:

clrf Y
bsf Y,0
loop:
rrf X,f
rlf Y,f
skpnc
 goto loop

6 instructions, does not affect W, isosynchronous, not very fast

Wouter


'Efficient way to reverse a byte?'
1999\11\01@072955 by Andy David
flavicon
face
part 0 1626 bytes
- 16 cycles
- 16 words
- uses 1 addition byte or RAM.
- Enter with byte to be reversed in WREG, ends with reversed byte in
WREG.
- Isochronous.

               rlcf    WREG,f          ; W   = d6 d5 d4 d3 d2 d1 d0 C
{C = d7}
               rrcf    Reverse,f       ; Rev = d7 R7 R6 R5 R4 R3 R2 R1
{C = R0}
               rlcf    WREG,f          ; W   = d5 d4 d3 d2 d1 d0 C  R0
{C = d6}
               rrcf    Reverse,f       ; Rev = d6 d7 R7 R6 R5 R4 R3 R2
{C = R1}
               rlcf    WREG,f          ; W   = d4 d3 d2 d1 d0 C  R0 R1
{C = d5}
               rrcf    Reverse,f       ; Rev = d5 d6 d7 R7 R6 R5 R4 R3
{C = R2}
               rlcf    WREG,f          ; W   = d3 d2 d1 d0 C  R0 R1 R2
{C = d4}
               rrcf    Reverse,f       ; Rev = d4 d5 d6 d7 R7 R6 R5 R4
{C = R3}
               rlcf    WREG,f          ; W   = d2 d1 d0 C  R0 R1 R2 R3
{C = d3}
               rrcf    Reverse,f       ; Rev = d3 d4 d5 d6 d7 R7 R6 R5
{C = R4}
               rlcf    WREG,f          ; W   = d1 d0 C  R0 R1 R2 R3 R4
{C = d2}
               rrcf    Reverse,f       ; Rev = d2 d3 d4 d5 d6 d7 R7 R6
{C = R5}
               rlcf    WREG,f          ; W   = d0 C  R0 R1 R2 R3 R4 R5
{C = d1}
               rrcf    Reverse,f       ; Rev = d1 d2 d3 d4 d5 d6 d7 R7
{C = R6}
               rlcf    WREG,f          ; W   = C  R0 R1 R2 R3 R4 R5 R6
{C = d0}
               rrcf    Reverse,w       ; W   = d0 d1 d2 d3 d4 d5 d6 d7
{C = R7}


- Andy.
---------------------------------------------------------
Andrew David, Software Manager, Ultronics Ltd, Cheltenham
RemoveMEakdavidspamTakeThisOuTultronics.co.uk          http://www.ultronics.com


1999\11\01@113015 by Tracy Smith

picon face
--- Andy David <akdavidEraseMEspam.....ULTRONICS.CO.UK> wrote:

> - 16 cycles
> - 16 words
> - uses 1 addition byte or RAM.
> - Enter with byte to be reversed in WREG, ends with
> reversed byte in
> WREG.
> - Isochronous.

This is a variation of something Steve Hardy posted
several years ago.  Except Steve's version would
reverse two bytes and worked on the Midrange pics as
well.


IIRC:

; reverse r1 r2

  rlf  r1,f   ; get carry bit and put r1.7 into carry
  rrf  r2,f   ; get r1.7 and put r2.0 into carry

  rlf  r1,f   ; get r2.0 and put r1.6 into carry
  rrf  r2,f

  rlf  r1,f
  rrf  r2,f

  rlf  r1,f
  rrf  r2,f

  rlf  r1,f
  rrf  r2,f

  rlf  r1,f
  rrf  r2,f

  rlf  r1,f
  rrf  r2,f

  rlf  r1,f
  rrf  r2,f

  rlf  r1,f    ; get r2.7 and put the original
               ; carry bit back.


17 cycles to reverse two bytes

.lo

=====

__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com

1999\11\01@133601 by paulb

flavicon
face
Andy David wrote:

> - 16 cycles
> - 16 words

 17 if you want to reverse two bytes at once.

> - Enter with byte to be reversed in WREG, ends with reversed byte in
> WREG.

 No, it ends with the reversed byte in W, not WREG as written, or in
Reverse if you specify F as destination.  Unless you're talking of a PIC
with which I'm not familiar.

{Quote hidden}

; 17th instruction tidies everything up:
    rlf    WREG,f          ; WR  = R0 R1 R2 R3 R4 R5 R6 R7 {C = C¡}
--
 Cheers,
       Paul B.

'Efficient way to reverse a byte?"'
1999\11\02@041912 by Andy David

flavicon
face
part 0 435 bytes
It's for a 17c4x (hence the rlcf and rrcf rather than rrf and rlf), so
WREG is W.  WREG is mapped in RAM on the 17c's, so you can use
instructions on it. Perhaps I should have posted that it's for a 17c4x
rather than the mid- or low-range devices.


- Andy.

---------------------------------------------------------
Andrew David, Software Manager, Ultronics Ltd, Cheltenham
EraseMEakdavidspamultronics.co.uk          http://www.ultronics.com



'Two ASCII bytes to one Hex byte?'
2000\04\01@073238 by David Thompson
flavicon
face
part 0 16 bytes
</x-html>

2000\04\01@093507 by Scott Dattalo

face
flavicon
face
On Sat, 1 Apr 2000, David Thompson wrote:

> Hi all,
>
> I need something that would take two ASCII bytes and end up with a Hex value.
>
> For example...
>
> 33, 37 would become 25h (decimal 37)
>
> There are plenty of ASCII to Hex examples but they are a bit simplistic for this case. I thought maybe I could convert the first byte to hex (03h) and multiply that by 10 then add it to the second byte (converted to hex - 07h) to have reach the sum of 37, but there might be a more elegant way...
>
> Any ideas?

Why not just convert the two ascii values to bcd and call one of the bcd to hex
routines?

 swapf  ascii1,w  ; ascii1 = 33, w=33 (coincidence)
 andlw  0xf0      ; w = 30 (low nibble of ascii1)
 xorwf  ascii2,f  ; ascii2 = 30^37 = 07
 xorwf  ascii2,w  ; w = 30^07 = 37



or just brute force it:

 movlw  0x0f
 andwf  ascii2,f  ;Clear the upper nibble of the one's digit
 rlf    ascii1,w  ;2*tens (note carry will always be cleared here)
 andlw  0x1e      ;Clear upper nibble of tens digit
 movwf  ascii1    ;W = ascii1 = 2*original ascii1
 rlf    ascii1,f
 rlf    ascii1,f  ;ascii1 = 8*original ascii1
 addwf  ascii1,w  ;W = 2*original ascii1 + 8 *original ascii1
                  ;  = 10*original ascii1
 addwf  ascii2,w

Of course, this snippet assumes that ascii1, and ascii2 are valid numerical
ASCII digits.

Scott

2000\04\01@131143 by Thomas McGahee

flavicon
face
Scott,
Here is my brute force method for converting a two-byte
ascii decimal representation to the actual binary equivalent.
For example, ascii '3', '7' which is hex 0x33, 0x37
will become binary b'00100101' which is hex 0x25

;enter with ascii_msd and ascii_lsd loaded with ascii
;representation of decimal value to be converted to hex.
;Ascii must be in range 0x30 to 0x39. ( "0" to "9" )

;exit with hex byte in ascii_lsd (or w).
;Numerical value will be between 0 and 99 (decimal).

 movlw   0x0f        ;set up mask. w has 0x0f
 andwf   ascii_lsd,f ;ascii_lsd has hex/dec lsd. w has 0x0f
 andwf   ascii_msd,f ;ascii_msd has hex/dec msd. w has 0x0f
 bcf     status,c    ;carry=0 (so 0 gets rotated in next step)
 rlf     ascii_msd,w ;w has hex/dec msd*2, and ascii_msd has hex/dec msd
 movwf   ascii_msd   ;w has hex/dec msd*2, and ascii_msd has hex/dec msd*2
 rlf     ascii_msd,f ;w has hex/dec msd*2, and ascii_msd has hex/dec msd*4
 rlf     ascii_msd,f ;w has hex/dec msd*2, and ascii_msd has hex/dec msd*8
 addwf   ascii_msd,w ;w has hex/dec msd*10, and ascii_msd has hex/dec msd*8
 addwf   ascii_lsd,f ;ascii_lsd has msd*10 + lsd.
                     ;ascii_lsd now contains the desired hex byte

; or use addwf  ascii_lsd,w  if you want the hex byte in w.


I have an explicit clearing of the carry flag
prior to the rlf instruction. I notice that your (Scott's)
ascii to hex byte solution does not explicity clear the
carry flag. I don't see how you can be *sure* that the carry
is clear prior to your first rlf instruction. Or am I
missing something here?

Fr. Tom McGahee

{Original Message removed}

2000\04\01@133423 by Scott Dattalo

face
flavicon
face
On Sat, 1 Apr 2000, Thomas McGahee wrote:

{Quote hidden}

Yes you are (missing something). Immediately following the rlf is an ANDLW
instruction. If the carry is set (before the rlf), then the ANDLW 0x1e will
clear the lsb that got corrupted. At the same time, the RLF clears the carry
because David says the input consist of ASCII encode decimal digits.

Here's the routine again with additional comments:

;ascii1 and ascii2 are the tens and ones digit of a number we wish
;to convert to binary
;
; In C:
;
;  binary = (ascii1 & 0xf) * 10 + (ascii2 & 0xf);
;
; (I'd be interested to see how a compiler would generate the asm for this.)
;

  movlw  0x0f
  andwf  ascii2,f  ;Clear the upper nibble of the one's digit

; Multiply the ones digit by 10.
  rlf    ascii1,w  ;2*tens
                   ;Note that the carry is also cleared because
                   ;ascii1 is an ASCII number between 0x30-0x39
  andlw  0x1e      ;Clear upper nibble of tens digit
                   ;In addition, we clear the shifted in carry
  movwf  ascii1    ;W = ascii1 = 2*original ascii1
  rlf    ascii1,f  ;ascii1 = 4*original ascii1
  rlf    ascii1,f  ;ascii1 = 8*original ascii1
  addwf  ascii1,w  ;W = 2*original ascii1 + 8 *original ascii1
                   ;  = 10*original ascii1
  addwf  ascii2,w  ;

Scott

2000\04\04@042214 by Rich Leggitt

picon face
Hi Dave, how about:

       movlw 15                ; de-ASCIIfy the chars
       andwf char1,f
       andwf char2,f
       swapf char1,w           ; w = char1*10
       addwf char1,w
       addwf char1,w
       addwf char2,w           ; + char2


On Sat, 1 Apr 2000, David Thompson wrote:

{Quote hidden}

2000\04\04@141431 by Andrew Warren

face
flavicon
face
Rich Leggitt <EraseMEPICLISTspamspamspamBeGoneMITVMA.MIT.EDU> wrote:

> > I need something that would take two ASCII bytes and end up with
> > a Hex value.
> >
> > For example...
> >
> > 33, 37 would become 25h (decimal 37)
>
> how about:
>
>         movlw 15                ; de-ASCIIfy the chars
>         andwf char1,f
>         andwf char2,f
>         swapf char1,w           ; w = char1*10
>         addwf char1,w
>         addwf char1,w
>         addwf char2,w           ; + char2

Rich:

I assume that the "15" in "movlw 15" is decimal.

Your routine will work for 0-9 [ASCII 30-39], but it fails on A-F
[ASCII 41-46].  Here's a way that works for the whole range:

   LIST R = DEC

   MOVLW   '0'
   BTFSC   CHAR1,6
   MOVLW   'A'-10
   SUBWF   CHAR1

   MOVLW   '0'
   BTFSC   CHAR2,6
   MOVLW   'A'-10
   SUBWF   CHAR2

   ;The rest of the routine is as you wrote it:

   SWAPF   CHAR1,W
   ADDWF   CHAR1,W
   ADDWF   CHAR1,W
   ADDWF   CHAR2,W

-Andy


=== Andrew Warren - RemoveMEfastfwdKILLspamspamix.netcom.com
=== Fast Forward Engineering - San Diego, California
=== http://www.geocities.com/SiliconValley/2499

2000\04\04@160451 by Rich Leggitt

picon face
> Here's a way that works for the whole range:
>
>     LIST R = DEC
>
>     MOVLW   '0'
>     BTFSC   CHAR1,6
>     MOVLW   'A'-10
>     SUBWF   CHAR1
>
>     MOVLW   '0'
>     BTFSC   CHAR2,6
>     MOVLW   'A'-10
>     SUBWF   CHAR2

Aggh, hex!! But then what about lower case??

 movf char1,w ; '0'-'9'    'A'-'F'/'a'-'f'
 andlw 0x1f   ; 0x10-0x19  0x01-0x06
 addlw 0xf0   ; 0x00-0x09  0xf1-0xf6
 skpc
 addlw 0x19   ;            0x0a-0x0f
 movwf char1

-- Rich

2000\04\04@164921 by David E Arnold

picon face
Andrew,

I don't quite understand this question. ASCII isn't ASCII  a  7bit code? meaning
each character is represented with 7 bits and to convert to hex simply interpret
those 7 bits as interpret them as hex correct? which for 2 ASCII bytes would
require 2 bytes.  How in your example did you say 33 and 37 becomes 25hex.
This is only one byte of hex to represent 2 bytes of ASCII, not possible.

Please Clarify what's you mean, I'm really curious what I'm missing here.

thanks,
-Dave





Andrew Warren <fastfwdSTOPspamspamspam_OUTIX.NETCOM.COM> on 04/04/2000 11:14:05 AM

Please respond to pic microcontroller discussion list <spamBeGonePICLISTSTOPspamspamEraseMEMITVMA.MIT.EDU>

To:   KILLspamPICLISTspamBeGonespamMITVMA.MIT.EDU
cc:    (bcc: David E Arnold/SYBASE)
Subject:  Re: Two ASCII bytes to one Hex byte?




Rich Leggitt <EraseMEPICLISTspamEraseMEMITVMA.MIT.EDU> wrote:

{Quote hidden}

Rich:

I assume that the "15" in "movlw 15" is decimal.

Your routine will work for 0-9 [ASCII 30-39], but it fails on A-F
[ASCII 41-46].  Here's a way that works for the whole range:

   LIST R = DEC

   MOVLW   '0'
   BTFSC   CHAR1,6
   MOVLW   'A'-10
   SUBWF   CHAR1

   MOVLW   '0'
   BTFSC   CHAR2,6
   MOVLW   'A'-10
   SUBWF   CHAR2

   ;The rest of the routine is as you wrote it:

   SWAPF   CHAR1,W
   ADDWF   CHAR1,W
   ADDWF   CHAR1,W
   ADDWF   CHAR2,W

-Andy


=== Andrew Warren - @spam@fastfwd@spam@spamspam_OUTix.netcom.com
=== Fast Forward Engineering - San Diego, California
=== http://www.geocities.com/SiliconValley/2499

2000\04\04@171754 by Wagner Lipnharski

picon face
I don't see any reason WHY your PACKED BCD 37 (the 3 from 3"3" and the 7
from 3"7") should not be seem as binary 0011 0111 or hexa 37....  It is
really messy think that the PACKED BCD 37 is in true a decimal
representation and needs to be converted to hexa 25... really confused.

> > > I need something that would take two ASCII bytes and end up with
> > > a Hex value.
> > >
> > > For example...
> > >
> > > 33, 37 would become 25h (decimal 37)

Remember, any binary system is in true NOT DECIMAL, it can be octal,
hexadecimal, whatever you want, but it will be always binary represented
as 2^n.    The only place where you need Decimal representation is when
inputting or outputting numbers from/to human interface, there is no
other place in digital systems for decimal manipulations.

So, for example, if you have the numbers "5" and "7" it is ASCII
(digital, binary) 35h and 37h, or 0011 0101 and 0011 0111, that when
packed they will turn to be 57h (0101 0111). You can say that its
"decimal" representation is 53 and 55, but what for?

Of course if the result of a binary counter is 57h you should NOT send
35h and 37h to your LCD display, except if you want to see it in
hexadecimal format. This is one of the few moments you should convert it
first to decimal, will be 87, so ASCII 38h and 37h will be sent to the
LCD, then you will see decimal "87" at the display.

I am sorry if you already know that, but some other people don't, and it
is a great moment to explain it.

2000\04\04@181422 by Andrew Warren

face
flavicon
face
David E Arnold <spamBeGonePICLISTspamKILLspamMITVMA.MIT.EDU> wrote:

> isn't ASCII  a  7bit code? meaning each character is represented
> with 7 bits and to convert to hex simply interpret those 7 bits as
> interpret them as hex correct? which for 2 ASCII bytes would
> require 2 bytes.  How in your example did you say 33 and 37 becomes
> 25hex. This is only one byte of hex to represent 2 bytes of ASCII,
> not possible.

Dave:

In the example (convert 33H 37H to 25H), 33 and 37 are the ASCII
codes for the characters "3" and "7".  For clarity, the question
might have been stated "I have a two-digit decimal number (37, for
example) represented as two ASCII characters (33H and 37H).  How can
I convert the number from THAT representation into a single value?"

Does that make more sense?  The way to do the conversion is to first
convert the two characters from ASCII "3" and "7" to the numbers 3
and 7, then multiply the 3 by 10 and add it to the 7.  The result, 37
decimal, is equivalent to hex 25.

And actually... Now that I've re-read te question, I take back what I
wrote in an earlier message.  I misread the question and oroiginally
thought that the pair of ASCII characters represented a HEX number;
now I see that it's a decimal number.  So to Rich Leggitt:  Sorry;
your example would've worked fine.

-Andy


=== Andrew Warren - .....fastfwdspam_OUTspamix.netcom.com
=== Fast Forward Engineering - San Diego, California
=== http://www.geocities.com/SiliconValley/2499

2000\04\04@182426 by jamesnewton

face picon face
We say ASCII when we mean that the number is ready to be sent to an ASCII
terminal (window, screen, whatever) and displayed as the digits of the
number. A 33 in ASCII is a '3', 30 is '0' etc.. If you send a 7 to an ASCII
terminal, the bell rings (computer beeps, whatever) an 8 will cause the
terminal to backspace. You wanted to send a 37 or a 38. To send any single
digit from 0...9 you must add 30 to it before sending it to a terminal.

Conversely, when you receive a character from an ASCII terminal, if it is a
digit (in the range 30...39) you need to subtract 30 from it. If you want to
get multiple digits (e.g. allow the user to enter a number between 0...256)
you need to combine the digits into one binary value. So when the user types
234 you actually get 32 33 34 which you convert to 02 03 04 which you then
convert to 02*100 + 03*10 + 04.

Now, all of that was in decimal. PICs compute in binary. 02*100 in binary is
10 * 1100100. It's a bit hard to read so we usually use hexadecimal which is
easier to convert to and from binary since each hex digit is exactly a group
of 4 binary digits. In fact, most displays of the values in PIC registers
are in hexadecimal. When the original poster said he wanted to "end up with
a hex value", he was really saying he needed to convert from decimal to
binary. Our example problem expressed in hex would be 02 * 64h + 03 * 0Ah +
4 which is 0EAh (hex) or 11101010 (binary) or 234 (decimal) or '234' (ASCII
decimal)

Confused? Good, that's why I get paid the big bucks to design and not to
teach. I just wish I could tech. That's why I administrate.

---
James Newton TakeThisOuTjamesnewton.....spamTakeThisOuTgeocities.com 1-619-652-0593
http://techref.massmind.org NEW! FINALLY A REAL NAME!
Members can add private/public comments/pages ($0 TANSTAAFL web hosting)


{Original Message removed}

2000\04\04@191543 by Thomas McGahee

flavicon
face
David,
the ascii CODE 30H happens to REPRESENT the character "0" which
in turn represents the NUMBER "zero" which in turn has a VALUE
of 00h.

So the ascii codes 33h and 37h represent "3" and "7" which
together makes "37", which has the same numerical value as 25h.


The original poster was asking for a routine that would take
the two ascii codes 33h and 37h and give 25h as the result.
25h means 2*16 + 5 which equals 37 in decimal notation.

When programming any microcontroller it is often necessary
to make these transformations so that you can, for example,
extract the numbers in ascii format that you receive over a serial
link from a PC or other device, and use them to perform
calculations.

We also often have the need to go in the opposite direction
and take a hex byte like 25h and convert it to "37" so we
can display it on an LCD screen. So a lot of us end up
"re-inventing the wheel" and spending inordinate amounts
of time coming up with methods that thousands of people
have already figured out before us.

Some of us enjoy finding different ways to do the same thing.
Make it easier to understand, or faster, or "elegant",
or do it in such a way that no normal person would have
thought of doing it. There is a challenge in trying to
do it in less steps. And sometimes we don't have all that
much extra space in our code, and we want to cram just
one more routine in there, but we need three extra bytes
to cram it in there, so we go through our code looking
for ways to save a byte here and there. It can be
maddening, or it can be fun... depending on the deadline.


Fr. Tom Mcgahee

{Original Message removed}

2000\04\04@191552 by Scott Dattalo

face
flavicon
face
On Tue, 4 Apr 2000, Andrew Warren wrote:

> now I see that it's a decimal number.  So to Rich Leggitt:  Sorry;
> your example would've worked fine.

Which example? The first example he posted doesn't work. It computes 18*tens +
ones and not 10*tens + ones. The ones Tom and I posted a few days ago do work.

Scott

2000\04\04@195331 by Rich Leggitt

picon face
Um... dang. What I get for coding at 2AM. But hey, it LOOKED
good...
--Rich

On Tue, 4 Apr 2000, Scott Dattalo wrote:

{Quote hidden}

2000\04\05@022141 by Dr. Imre Bartfai

flavicon
face
Hi,

there is another remark may be of interest.

If you inputs a number in character format, do some calculations or so,
then outputs in character format, there is also another way as to convert
it to hex/binary, calculate and re-convert. You can convert it into the
so-called packed decimal format. E. g. the number "47" = 34h 37h will be
47h in packed decimal. It is possible to do some calculations (e. g.
additions) directly with this, and the reverse the procedure. The trick
is very simple:

1. You add the digits.
2. If the sum of the paricular digits is less, than 0Ah, do nothing.
3. If the sum of the digits exceeds 0Ah, add 6 to the result, and
  mark the carry.

E. g.

+47h    BCD
+26h    BCD

would result really 73. How?

1. Add the digits:

The result is 6Dh

3. D exceeds A, so add 6 to it. D+6 makes 13h

Write 3 into the result above in place of D and add that 1 of 13h as carry
to the 6. So you get 73. Then, it can be unpacked.

This procedure can be repeated for every digits.

Regards,
Imre


On Tue, 4 Apr 2000, Thomas McGahee wrote:

{Quote hidden}

> {Original Message removed}

2000\04\05@095709 by David Thompson

flavicon
face
Hi there,

You are right, and I'm sorry that you are confused but my application does
require exactly this conversion. If you really want to know, Caller-ID
information contains time and date info in this  "messy" format..

i.e.:
80  Message type
30  Message length
01 Date/Time parameter marker
30 "0"
34 "4"  04 = April
30 "0"
35 "5" 05 = 5th
32 "2"
32 "2" 22 = 10
30 "0"
34 "4" 04 = 04 pm
etc...

See... it may seem silly to you, but this is how Caller-ID was designed... I
have to run through this list and turn the quite definately "ASCII" numbers
into hex representation, in this case you would end up with 04,05,16,04.

Thank you to all those in the PIClist that replied with vastly better
routines than my own. All of your ideas have been carefully read and
evaluated and are much appreciated.

To those few that this problem seems to have confused... hmmm stupid as some
requests like mine may sound, as you can see by this example, there ARE some
stupid things we have to deal with.

Thanks again,

David
{Original Message removed}

2000\04\05@133259 by Scott Dattalo

face
flavicon
face
Here's another solution that saves a cycle:

  rlf   tens,w
  andlw 0x1e      ;w=2*tens
  movwf temp
  rlf   temp,f    ;temp=4*tens
  rlf   temp,f    ;temp=8*tens
  addwf ones,w
  addlw -'0'      ;convert from ASCII to decimal
                  ;w=tens*2 + ones
  addwf temp,w    ;w=10*tens + ones

2000\04\05@164021 by Dmitry Kiryashov

flavicon
face
Hi Scott.

Destructive way to save one more clock ;-)

       movfw   tens    ;*1
       addwf   tens,F  ;*2
       rlf     tens,F  ;*4
       addwf   tens,F  ;*5
       rlf     tens,W  ;*10
       addwf   ones,W
       addlw   low     -11.*'0'

Why so ? tens is '0' + x , ones is '0' + y , where x and y are in range
of 0..9 So finally '0'*(10+1)=48.*11.=528.=0x210 . it is constant so we
can subtract it back from result. Here we gone ;)

WBR Dmitry.


Scott Dattalo wrote:
{Quote hidden}


'[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)
TakeThisOuTjamesnewtonspamspampiclist.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, olinEraseMEspamcognivis.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"
RemoveMEspeffEraseMEspamspam_OUTinterlog.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 <@spam@PICLISTRemoveMEspamEraseMEMITVMA.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 --- EraseMEaiwspam@spam@cypress.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@jamesnewtonspam_OUTspam.....piclist.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
spamBeGonejillesEraseMEspamrendo.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...