1. 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.
    Dismiss Notice

Convert BCD to Binary

Discussion in 'Microcontrollers' started by apricot_star, Aug 28, 2008.

  1. apricot_star

    apricot_star New Member

    Joined:
    Aug 17, 2008
    Messages:
    29
    Likes:
    0
    Hi anyone knows how to Convert BCD to Binary in PIC18F2620 using c language?
     
  2. ESchemainda

    ESchemainda New Member

    Joined:
    Mar 15, 2008
    Messages:
    21
    Likes:
    1
    Convert BCD to Binary

    I do it by hand.
    Supposing you have an array of BCD numbers.

    int BCD_numbers[] = {3, 2, 7, 6, 8};
    int result, i;

    result = 0;
    for(i=0;i<5;i++)
    result *= 10 + BCD_numbers;

    result now should contain 0x7FFF.:confused:
     
    • Like Like x 1
  3. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,007
    Likes:
    316
    Location:
    Brisbane Australia
    Assuming it's packed BCD in an integer then the following should work,
    Code (text):

    unsigned int BCDtoI(unsigned int BCD){
    unsigned int result;
        result=(BCD>>12)*1000;
        result+=((BCD>>8)&0x0f)*100;
        result+=((BCD>>4)&0x0f)*10;
        result+=((BCD)&0x0f);
        return result;
    }
    Mike.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. apricot_star

    apricot_star New Member

    Joined:
    Aug 17, 2008
    Messages:
    29
    Likes:
    0

    is it okie to write in this way:

    int BCD2BINARY(int x)
    {
    int binary;
    binary = ((x & 0xF0 )>>4 ) * 10 + (x & 0x0F);
    return (binary);
    }
     
  6. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,007
    Likes:
    316
    Location:
    Brisbane Australia
    Doing it that way is fine as long as your number is only 2 digits.

    Edit, if it's only a 2 digit number then you can do,
    bcd-=((bcd&0xf0)>>3)*3;

    Mike.
     
    Last edited: Aug 28, 2008
  7. poppy2008

    poppy2008 New Member

    Joined:
    Aug 25, 2008
    Messages:
    25
    Likes:
    0
    hi pommie can u explain ur coding,i am new in c,but i am very much interested to learn,please
     
  8. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,007
    Likes:
    316
    Location:
    Brisbane Australia
    All the code is doing is getting the relevant digit and multiplying by the decimal value. So, BCD>>12 will get the thousands digit which is then multiplied by 1000.

    Mike.
     
  9. apricot_star

    apricot_star New Member

    Joined:
    Aug 17, 2008
    Messages:
    29
    Likes:
    0
    Hi pommie,

    Thanks for the reply, however i was wondering why must it shift by 3 and times 3?
     
  10. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,007
    Likes:
    316
    Location:
    Brisbane Australia
    The way to convert a BCD number to it's binary equivalent is to divide the high digit by 16 and then multiply by 10. Another way to do the same thing is to subtract the high digit * 6 (16-10). To do this in code you do BCD>>4 to get the high digit and then multiply by 6. As 6 is 3*2 then you can reduce the number of shifts by 1 and multiply by 3. This is just the same as saying that BCD/16*6 is the same as BCD/8*3.

    The less efficient (but more logical) way to write the same thing is,
    bcd-=((bcd&0xf0)>>4)*6;

    Mike.
     
  11. vietbk

    vietbk New Member

    Joined:
    Jan 6, 2009
    Messages:
    1
    Likes:
    0
    Convert BCD to Binary
    Hi anyone knows how to Convert BCD to Binary in Psoc using assembly language?
     
  12. nickelflippr

    nickelflippr Member

    Joined:
    Oct 20, 2006
    Messages:
    891
    Likes:
    18
    Location:
    Oregon, USA
    Ha, was just looking at the 18F data sheet instructions. The assembler instruction, DAW, looks custom fit for the BCD to BIN decode. Would this do the trick?

    Code (text):
    ;Decode say a DS1307 minutes from BCD to binary
    ;minutes = 0x27 BCD
        call    FN_READ8BIT
        movff      READ8BIT,MIN
        movf       MIN,W,BANKED
        daw
        movwf  MIN,BANKED
    ;MIN is now 27 minutes in binary?
     

Share This Page