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:

See also:


file: /Techref/microchip/math/radix/b2a-4b1a.htm, 4KB, , updated: 2008/2/11 15:46, local time: 2017/11/23 12:41,
TOP NEW HELP FIND: 
54.224.164.166:LOG IN

 ©2017 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://www.piclist.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 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?

  PICList 2017 contributors:
o List host: MIT, Site host massmind.org, Top posters @20171123 RussellMc, Van Horn, David, James Cameron, Sean Breheny, alan.b.pearce, IVP, Neil, David C Brown, Bob Blick, Denny Esterline,
* Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.
 

Welcome to www.piclist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .