please dont rip this site

PIC Microcontoller Radix Math Method

Binary to BCD half-packed 8 bit to 3 digit

From Mike McLaren, K8LH, (Westland, MI, USA)

While collaborating on a little 10F200 project recently (256 words of program memory and 16 bytes RAM) I came up with the following little Bin2Bcd routine. The additional code (PrintIt, PutDigit, etc.) simply prints the "0".."255" number as right justified with leading zero suppression;

;                                                                 *
;  8 bit to 3 digit half-packed BCD, Mike McLaren, K8LH (Jan-09)  *
;								  *
;   input: WREG, 0x00..0xFF, 0..255				  *
;  output: tens, 0x00..0x25, packed bcd				  *
;          ones, 0x00..0x09					  *
;								  *
;  12 words, 2 variables
tens    equ     0x1A            ; packed BCD 'hundreds' & 'tens'
ones    equ     0x1B            ; single BCD 'ones'
mask    equ     0x1C            ;

        clrf    tens            ;
        decf    tens,F          ; preset 'tens' to -1
div10   movwf   ones            ;
        incf    tens,F          ; bump 'tens', 0x00..0x25
        movlw   6               ; using "packed bcd" format
        addwf   tens,W          ; bcd "digit carry"?
        skpndc                  ; no, skip, else
        movwf   tens            ; fix 'tens'
        movlw   10              ; ones = ones - 10
        subwf   ones,W          ; borrow?
        bc      div10           ; no, branch, else
        movlw   " "             ; prep leading zero suppression
        movwf   mask            ; mask = 0x20 = " " (space char)
        swapf   tens,W          ; get hundreds, 0..2
        call    PutDigit        ; print " " or "1".."2"
        movf    tens,W          ; get tens, 0..9
        call    PutDigit        ; print " " or "0".."9"
        movf    ones,W          ; get ones, 0..9
        goto    PutNumber       ; always print "0".."9"
        andlw   0x0F            ;
        skpz                    ;
        bsf     mask,4          ; mask = 0x30 = "0"
        iorwf   mask,W          ; wreg = " " or "0".."9"
        goto    Put232          ; Put232 or PutLCD

file: /Techref/microchip/math/radix/b2bhp-8p3d-mm.htm, 5KB, , updated: 2010/10/4 05:41, local time: 2024/7/13 18:08,

 ©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=""> PIC Microcontoller Radix Math Method Binary to BCD half-packed 8 bit to 3 digit - Mike McLaren</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?


Welcome to!


Welcome to!