please dont rip this site

PIC Microcontroller Radix Math Method

4 Bit Binary to 1 ASCII HEX digit

Converting a Nybble to ASCII

This is a question that comes up all the time when the contents of a byte are to be displayed/output. Does your code need to be fast, or does your code need to be small?

4 Bit Binary to 1 ASCII HEX digit FAST

The most obvious and fastest way of doing this is:

NybbletoASCII:
; Call with binary nybble in W. Returns ASCII value in W.
; From: http://www.myke.com/basic.htm
  andlw H'0F' ; (optional )
  addwf  PCL, f            ;  Add the Contents of the Nybble to PCL/ 
  dt	 "0123456789ABCDEF"  ;  return the ASCII as a Table Offset

4 Bit Binary to 1 ASCII HEX digit SMALL

Several people have independently reduced the core of nybble-to-ASCII conversion to 4 instructions in size, zero RAM. Can you spot that core inside these 6 instruction subroutines?

NybbletoASCII:
; from Sean H. Breheny [shb7 at CORNELL.EDU]
; Sean H. Breheny KA3YXM [at rocket-roar.com]
    andlw H'0F' ; (optional )
    addlw 0-D'10'               ; test: Is W < 10 ?
    skpnc ; btfsc STATUS,C  ; If 0 <= W <= 9, skip ahead
        addlw 'A'-'0'-D'10' ; If A <= W <= F, add ASCII char 'A', and
                             ; subtract enough to make the next
                             ; line have no effect
    addlw '0'+D'10'            ; Add ASCII character '0' as well as
                             ; replace the original 10 that was subtracted
    return

Another:

NybbletoASCII:
; From: Scott Dattalo
    andlw H'0F' ; (optional )
    addlw    6
    skpndc
      addlw   'A'-('9'+1)
    addlw    '0'-6
    return

example

All of the above routines are interchangeable. Some test code that calls those routines with all possible values of W:


test_nybble_to_ascii:
    ; for( i = 0; i < 256; i++){ nybble_to_ascii(i) };
    movlw h'00' ;
    movwf index
nybble_test_for_loop:
    ; nybble_to_ascii(i);
    movf index,W
    call nybble_to_ascii
    nop ; place to put breakpoint to see if it converted correctly
nybble_test_for_test:
    decfsz index,f
    goto nybble_test_for_loop
    ; ... continue
    return    

Comments:

Questions:


file: /Techref/microchip/math/radix/b2a-4b1a.htm, 4KB, , updated: 2008/2/11 15:46, local time: 2024/12/26 09:53,
TOP NEW HELP FIND: 
3.144.227.3:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://linistepper.com/Techref/microchip/math/radix/b2a-4b1a.htm"> PIC Microcontroller Radix Math Method 4 Bit Binary to 1 ASCII HEX digit</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?