Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Looking for PIC code for 16bit binary to unpacked BCD

Status
Not open for further replies.
I used the 12-bit binary to unpacked BCD code below in my DS18B20 Thermometer Demo. It was relatively small and the four digit output was fine for the values I needed (000.0 to 999.9).

Code:
;
;  Peter Hemsley's 12-bit Bin_to_BCD (ProdH:ProdL binary input
;  and D3:D0 unpacked BCD output)
;
Bin2Dec
        movf    ProdH,W         ; 16-bit binary hi                |B0
        iorlw   0xF0            ; w=H2-16                         |B0
        movwf   D1              ; D1=H2-16                        |B0
        addwf   D1,f            ; D1=H2*2-32                      |B0
        addwf   D1,f            ; D1=H2*3-48                      |B0
        movwf   D2              ; D2=H2-16                        |B0
        addlw   -D'5'           ; w=H2-21                         |B0
        addwf   D2,f            ; D2=H2*2-37 Done!                |B0
        addlw   D'41'           ; w=H2+20                         |B0
        movwf   D0              ; D0=H2+20                        |B0
        swapf   ProdL,W         ; 16-bit binary lo                |B0
        iorlw   0xF0            ; w=H1-16                         |B0
        addwf   D1,f            ; D1=H2*3+H1-64                   |B0
        addwf   D0,f            ; D0=H2+H1+4, C=1                 |B0
        rlf     D0,f            ; D0=(H2+H1)*2+9, C=0             |B0
        comf    D0,f            ; D0=-(H2+H1)*2-10                |B0
        rlf     D0,f            ; D0=-(H2+H1)*4-20                |B0
        movf    ProdL,W         ;                                 |B0
        andlw   0x0F            ; w=H0                            |B0
        addwf   D0,f            ; D0=H0-(H2+H1)*4-20 Done!        |B0
        rlf     D1,f            ; C=0, D1=H2*6+H1*2-128 Done!     |B0
        movlw   D'5'            ;                                 |B0
        movwf   D3              ;                                 |B0
        movlw   D'10'           ;                                 |B0
mod0    addwf   D0,f            ; D(X)=D(X)mod10                  |B0
        decf    D1,f            ; D(X+1)=D(X+1)+D(X)div10         |B0
        skpc                    ;                                 |B0
        goto    mod0            ;                                 |B0
mod1    addwf   D1,f            ;                                 |B0
        decf    D2,f            ;                                 |B0
        skpc                    ;                                 |B0
        goto    mod1            ;                                 |B0
mod2    addwf   D2,f            ;                                 |B0
        decf    D3,f            ;                                 |B0
        skpc                    ;                                 |B0
        goto    mod2            ;                                 |B0
        return                  ;                                 |B0
 
Thanks Mike, the 12bit will work like a charm it's for sending 10bit A/D values to the serial port.

PS email me your address again, going to send you the Inchworm+ & Unicorn board (Nigel, Pommie, MRamos you too)
 
blueroomelectronics said:
Thanks Mike, the 12bit will work like a charm it's for sending 10bit A/D values to the serial port.

Have you checked my A2D tutorials?, they convert the value to ASCII digits and sent it out the serial port.
 
Dag nab it Nigel that was supposed to be my project of the week.

Basically like yours mine is Read A/D, binary to bcd 12bit, transmit on RS232.

Mine does not have the LCD option though. Might as well make mine transmit two channels.
 
blueroomelectronics said:
Dag nab it

That takes me back! - "Deputy Dawg" wasn't it?

Basically like yours mine is Read A/D, binary to bcd 12bit, transmit on RS232.

Mine doesn't do BCD, or at least what I understand by BCD? - it's purely a 16 bit to decimal conversion, followed by an ASCII conversion

Mine does not have the LCD option though. Might as well make mine transmit two channels.

The trick is to work out how to reliably seperate the different values at the other end, I would suggest sending something like AxxxxxBxxxxxCR/LF - where A and B signify the start of the respective readings, and xxxxx is the number. It's obviously a trivial thing to do, and is easily extended for more than two channels.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top