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.

BCD to Binary.

Status
Not open for further replies.

si2030

Member
Hi all,

In assembler (PIC) how do I convert BCD to Binary... Plenty of sites show the reverse. I want to convert and eight digit BCD number to its binary equivalent...

Simon
 
If you're talking about a packed BCD byte which is "tens" * 16 + "ones" then you just need to convert it to "tens" * 10 + "ones", perhaps something like this;

Regards, Mike

Code:
BCD_to_Bin
        movwf   Test            ; save Test value                 |B0
        swapf   Test,W          ; swap nybbles (into W)           |B0
        andlw   h'0F'           ; mask swapped tens digit         |B0
        movwf   Temp            ; put it in a work file           |B0
        addwf   Temp,W          ;  W = 2 * tens                   |B0
        addwf   Temp,F          ;  F = 3 * tens (C = 0)           |B0
        rlf     Temp,W          ;  W = 6 * tens                   |B0
        subwf   Test,W          ; 16 * tens + ones - 6 * tens     |B0
 
Last edited:
Hi Mike, Nigel

Yes its a packed byte that is... each nibble represents a power. eg 12312312 would be... 0001 0010 0011 0001 0010 0011 0001 0010. So in this case and what I am looking for is a 32 bit BCD number. Four bytes each with two nibbles each nibble representing one digit. The binary equivalent is "101110111101111011111000"

Mike how would you expand this to a 32bit conversion?

Thanks, Simon
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top