 
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            ;
Bin2Bcd
        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
PrintIt
        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"
PutDigit
        andlw   0x0F            ;
        skpz                    ;
PutNumber
        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: 2025/10/31 08:24, 
 
216.73.216.87,10-2-37-96:LOG IN | 
| ©2025 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://www.piclist.com/Techref/microchip/math/radix/b2bhp-8p3d-mm.htm"> PIC Microcontoller Radix Math Method Binary to BCD half-packed 8 bit to 3 digit - Mike McLaren</A> | 
| Did you find what you needed? | 
|  PICList 2025 contributors: o List host: MIT, Site host massmind.org, Top posters @none found - 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. | 
| Ashley Roll has put together a really nice little unit here. Leave off the MAX232 and keep these handy for the few times you need true RS232! | 
.