## 8 Bit Binary to 2 ASCII digits

...take a number in the range of 0x00 to 0xFF in W and output two ASCII characters, the most significant character into CHAR_HI and the least significant into W. No other registers can be used.

From: Scott Dattalo

movwf  char_hi
swapf  char_hi,w
andlw  0x0f

skpndc

xorwf  char_hi,w
xorwf  char_hi,f
xorwf  char_hi,w

andlw  0x0f

skpndc

My resulting code is 15 cycles, isochronous

Sean H. Breheny [shb7 at CORNELL.EDU] says:

movwf CHAR_HI           ; Place W in CHAR_HI to store
swapf CHAR_HI,W         ; Place CHAR_HI in W with most
; significant nibble in lower
; part of W
andlw 0x0F                      ; Clear upper part of W

addlw -10                       ; Same as single digit, do conversion
btfsc STATUS,C
; Swap W and CHAR_HI
xorwf CHAR_HI,F         ; CHAR_HI = CHAR_HI xor W
xorwf CHAR_HI,W         ; W = W xor CHAR_HI xor W
; = (W xor W) xor CHAR_HI
; = 0 xor CHAR_HI = CHAR_HI
xorwf CHAR_HI,F         ; CHAR_HI = CHAR_HI xor W xor CHAR_HI
; = (CHAR_HI xor CHAR_HI) xor W
; = 0 xor W = W

andlw 0x0F              ; Now we have original W back in W
; Clear upper part so we are left
; with original least significant
; nibble

addlw -10               ; Same as single digit, do conversion
btfsc STATUS,C

Kübek Tony [tony.kubek at FLINTAB.COM] says:

another version: uses one stack-level more is not 'faster' but less code space :)
;
***********************************************************************
;
;  CONV_BYTE_HEX - Converts the byte in w to two ASCII hex digits ('0'-'9', 'A'-'F' )
;  Returns with top nibble ASCII digit in char_hi and the bottom one inW
;  NOTE uses TWO deep stack ( nested calls ) !!
CONV_BYTE_HEX
MOVWF   char_hi ; save byte temporarily
SWAPF   char_hi,W
; convert top nibble
CALL    CONV_NIBBLE_HEX
; top nibble in w, exchange char_hi with w
XORWF   char_hi,F
XORWF   char_hi,W
XORWF   char_hi,F
; and convert low nibble
CONV_NIBBLE_HEX
ANDLW   0x0F    ; mask out top nibble, preserv low nibble
ADDLW   0xF6    ; add 246, to check if 0-9 or a-f, w=nibble+246
BTFSC   STATUS,C ; if carry set then a-f
ADDLW   0x07     ; carry set, then add 7, w=nibble+246+7 ( note nibble known to be a-f )
; 1) w = low(nibble+246+58) = 48+nibble ( if 0-9 )
; 2) w = low(nibble+246+7+58) = 55+nibble ( if a-f )
; nibble converted to ASCII hex in w
RETURN

• This is a useful algorithm. I think PIC programmers have to very inventive to code so efficiently with so few instructions and available program memory.

However, one comment that bears consideration:

Why not leave the ASCII for the HIGH nibble in W, and then output the LOW nibble ASCII into a variable called CHAR_LO.

You need to rename CHAR_HI to CHAR_LO , and to change the second instruction in the sequence

from swapf CHAR_HI,W

to swapf CHAR_LO,F

and of course alter the comments accordingly

Other than that, it should all be OK

Consider where the ASCII hex representation of the byte is likely to be used, i.e. as output to a terminal / screen connected by UART or other character-by-character means.

You need to transmit the text with the ASCII hex for the most significant nibble first, so it makes more sense to leave that in W at the end of the routine.

e.g. movf MY_BYTE,W
call hex2ascii ; the conversion routine
call send ; your serial TX routine, with char in W
movf CHAR_LO,W
call send ; your serial TX routine, with char in W

If you tried to do a similar thing with the existing routine, you'd need to save the LOW nibble hex value somewhere (in anothe RAM address) temporarily while moving the high one back into W for transmission. You are far more likely to write a generic "send" routine assuming the character is in W.

Cheers

+

Says:

A warning to users: This code is actually not usable for the range 0x00 to 0xFF, but only 0x00 to 0x99. Any nibbles of a value from 0xA to 0xF are converted to 0x3A through 0x3F, which is not correct ASCII. This code is only safe to use if your values will not go above 0x99.
+

Code:

• AlexanderVangelovspam at ~NOSPAM~gmal.com shares this code:
After calling bin2hex, usualy data must be sended to some interface: hi-byte first than lo-byte
But we get char_hi and WREG (char_lo)
Better way to have char_lo and W (char_hi)
So:
<blockquote>
bin2hex:
MOVWF   char_lo
CALL    convert_nibble
XORWF   char_lo,F
XORWF   char_lo,W
XORWF   char_lo,F
SWAPF   WREG,W
convert_nibble:
ANDLW   0x0F
BTFSC   STATUS,C
RETURN
</blockquote>

+

 file: /Techref/microchip/math/radix/b2ah-8b2a-sd.htm, 6KB, , updated: 2013/8/2 09:04, local time: 2018/12/13 22:57, TOP NEW HELP FIND:  35.175.182.106:LOG IN

 ©2018 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! /  PIC Microcontoller Radix Math Method - 4 Bit Binary to 2 ASCII digits

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.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant" "No. But I'm interested. me at when this page is expanded."

 PICList 2018 contributors: o List host: MIT, Site host massmind.org, Top posters @20181213 RussellMc, Van Horn, David, Sean Breheny, David C Brown, Neil, Isaac M. Bavaresco, Bob Blick, Harold Hallikainen, AB Pearce - UKRI STFC, John Gardner, * 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.

.