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            ;

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

Comments:

Questions:

Code: