PIC Microcontoller Radix Math Method

Binary to BCD unpacked, 24 bit to 4 digits

from Oliver Broad [obroad at TELINCO.CO.UK] Oliver Broad says

The following program is based on the binary to BCD code from the microchip appnote. The method seems odd at first, compared to the more conventional divide-by-10-and-output-the-remainder.

It was originally written to convert data from an AD7714 sigma delta ADC

; Looping version of Binary to BCD converter for 24 bit values
; Code is designed for MPSIM testing, so insert stimuli on ports
; B and B and B, and read out R0,R1,R2,R3.

; Code is based on the fact that a bcd number may be multiplied
; by two by a simple bit shift followed by a BCD fixup. The
; fixup is greatly simplified by performing it BEFORE the shift,
; which is why 3 and 30H are used instead of 6 and 60H.


        LIST P=16C73
        INCLUDE P16C73.INC
        CBLOCK  20H
        R3,R2,R1,R0        ;MUST START ON BINARY XXXXX000.
        COUNT, LBYTE, MBYTE, HBYTE
        ENDC
        ORG     0
MAIN    MOVFW   PORTB
        MOVWF   LBYTE
        MOVFW PORTB
        MOVWF MBYTE
        MOVFW   PORTB
        MOVWF   HBYTE
        CALL    BIN2BCD
        GOTO    MAIN
BIN2BCD MOVLW   18H
        MOVWF   COUNT
        CLRF    R0
        CLRF    R1
        CLRF    R2
        CLRF    R3
        BCF     STATUS,C
        GOTO    BIN2BC2
BIN2L   MOVLW   R3
        MOVWF   FSR
BCDADJ  MOVLW   03H
        ADDWF   INDF,F
        BTFSS   INDF,3
        SUBWF   INDF,F
        MOVLW   30H
        ADDWF   INDF,F
        BTFSS   INDF,7
        SUBWF   INDF,F
        INCF    FSR,F
        BTFSS   FSR,2
        GOTO    BCDADJ
BIN2BC2 RLF     LBYTE,F
        RLF  MBYTE,F
        RLF     HBYTE,F
        RLF R3,F
        RLF     R2,F
        RLF     R1,F
        RLF     R0,F
        DECFSZ  COUNT,F
        GOTO    BIN2L
        RETLW   0
        END


Comments: