Searching \ for '24-bit binary to BCD?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=bit+binary+bcd
Search entire site for: '24-bit binary to BCD?'.

Truncated match.
PICList Thread
'24-bit binary to BCD?'
1999\06\04@232122 by Gabriel Gonzalez

flavicon
face
Hi,

several months ago I remember seeing a nice'n'tight little routine to
convert a 16 bit binary number to BCD.

Can anybody post a similar one for a 24 bit number?

This will be great help.

TIA

Gabriel

1999\06\05@012853 by Lynx {Glenn Jones}

flavicon
face
The way i convert to BCD is as follows. Note: this may not be most
efficient but it works. Also, this is to convert an 8bit into three BCD
digits, but its easily expandable
Start by subtracting 100 from your value. If result is negative, stop,
else increment 100's counter and loop
Next subtract 10. If result is negative stop. else increment 10's counter
and loop
Whats left is the units digit.

------------------------------------------------------------------------------
A member of the PI-100 Club:
3.1415926535897932384626433832795028841971693993751
058209749445923078164062862089986280348253421170679

On Fri, 4 Jun 1999, Gabriel Gonzalez wrote:

{Quote hidden}

1999\06\05@113527 by Dave Mumert

flavicon
face
Here as a code snippet that converts 16, 24 or 32 bit binary numbers to bcd.
It has been only slightly tested

Include   p16f84.inc

BitCount set 16 ;(must be 16, 24 or 32)

     CBLOCK 0x0C
        R0                   ;BCD byte 9 (upper nibble)
        R1
        R2
        R3
        R4                  ;BCD byte 0 (lower nibble)
        Byte3              ;BIN Inputs 2^32 (MSB)
        Byte2
        Byte1
        Byte0              ;BIN (LSB)
        Temp
        Count
     endc


        org        0000

        movlw   0FFH              ;test code with 0FFFFFFFFh
        movwf   Byte0
        movwf   Byte1
        movwf   Byte2
        movwf   Byte3
        call        BIN2BCD
        nop
        goto      $


BIN2BCD    ; Binary to BCD conversion
    If BitCount == 16
        movlw   .16
    endif
    If BitCount == 24
        clrf        R3
        movlw   .24
    endif
    If BitCount == 32
        clrf        R3
        clrf        R4
        movlw   .32
    endif
        movwf   Count
        bcf        STATUS,0        ; clear the carry bit
        clrf        R0
        clrf        R1
        clrf        R2

BCDLoop
        rlf         Byte0,F
        rlf         Byte1,F
     if BitCount == 24
        rlf         Byte2,F
        rlf         R3,F
    endif
    if BitCount == 32
        rlf         Byte2,F
        rlf         Byte3,F
        rlf         R4,F
        rlf         R3,F
    endif
        rlf         R2,F
        rlf         R1,F
        rlf         R0,F

        decfsz  Count,F
        goto     adjDEC
        return

adjDEC
    if BitCount == 32
        movlw   R4
        movwf   FSR
        call        adjBCD
        call        adjBCD
    endif
    if BitCount == 24
        movlw   R3
        movwf   FSR
        call        adjBCD
    endif
    if BitCount == 16
        movlw   R2
        movwf   FSR
    endif
        call        adjBCD
        call        adjBCD
        call        adjBCD
        goto      BCDLoop

adjBCD
        movlw      3
        addwf       INDF,W         ; add 3
        movwf      Temp               ; save it
        btfsc         Temp,3            ; test if result > 7
        movwf      INDF              ; get back the original number if < 8
        movlw      30
        addwf       INDF,W         ; add 30
        movwf      Temp
        btfsc         Temp,7           ; test if result > 7
        movwf      INDF              ; save
        decf          FSR,F
        retlw         0

end

Goodluck
Dave
spam_OUTdaveTakeThisOuTspammumert.com



----- Original Message -----
From: Gabriel Gonzalez <.....tgoKILLspamspam@spam@CHIH1.TELMEX.NET.MX>
Sent: Friday, June 04, 1999 9:17 PM
Subject: 24-bit binary to BCD?


{Quote hidden}

More... (looser matching)
- Last day of these posts
- In 1999 , 2000 only
- Today
- New search...