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?
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
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
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:
I just wanted to say a big "THANKS!" for all the great help, tips, and first hand knowledge you provide. I've been able to get my PIC projects running much better with your help.
Take care, and Thanks,
baumgarc AT gps.caltech.edu+
Questions:
file: /Techref/microchip/math/radix/b2a-4b1a.htm, 4KB, , updated: 2008/2/11 15:46, local time: 2024/11/28 09:18,
3.147.68.201: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? <A HREF="http://ecomorder.com/techref/microchip/math/radix/b2a-4b1a.htm"> PIC Microcontroller Radix Math Method 4 Bit Binary to 1 ASCII HEX digit</A> |
Did you find what you needed? |
Welcome to ecomorder.com! |
Welcome to ecomorder.com! |
.