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

BCD Conversion

Discussion in 'Microcontrollers' started by TucsonDon, Jul 28, 2017.

  1. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    I am using a rtcc that uses the first 6 bits in the register to counts the hours in bcd. I need to convert to from bcd to binary to display on the lcd, and from binary to bcd to set the time. How do I do this?
     
  2. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,886
    Likes:
    500
    Location:
    Cleveland, OH, USA
    ONLINE
    What language? What series of MCU?

    John
     
  3. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    Sorry, I am using C and a PIC18F45K22
     
  4. dave

    Dave New Member

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


     
  5. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,886
    Likes:
    500
    Location:
    Cleveland, OH, USA
    ONLINE

    Wish I could help, but I am still married to Assembly and the enhanced mid-range group.

    There are several algorithms for doing BCD to binary and back. PICList.com may have some in C; although, it is oriented to Assembly. Wikipedia has a pretty good description of the algorithms, if you want to roll your own, but I suspect you will find far more efficient approaches here or on Google.

    John
     
  6. ci139

    ci139 Active Member

    Joined:
    Apr 12, 2012
    Messages:
    389
    Likes:
    34
    was it to ascii
    BCD 00 ... 23 (BCD in hex 0x00 ... 0x23 as 4-bit binary represented decimals) ← ←
    in binary ???00000 ... ???10111 -- (binary in hex 0x00 .. 0x17 as 4-bit hexadecimals)
    BCD in ASCII as hex 0x30 0x30 ... 0x32 0x33 (in decimal 48 48 ... 50 51 in characters 0 0 .. 2 3 ↑↑)
    binary in ascii chars 00000000 ... 00010111 (ascii char codes in hex 0x3030303030303030 ... 0x3030303130313131)
     
  7. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    I an converting to ascii but using itoa to do that. I have included the page from the data sheet that shows the hour register.
     

    Attached Files:

    Last edited: Jul 29, 2017
  8. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,008
    Likes:
    316
    Location:
    Brisbane Australia
    ONLINE
    To convert BCD to binary can be done by subtracting the top nibble times six.

    I.E. 47 BCD is 0x47 (71d) - 4*6 = 71-24 = 47.

    To go the other way simply add (num/10)*6.

    Mike.
     
    Last edited: Jul 29, 2017
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,147
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    If I remember correctly, you use MikroC There are conversion routines built in for this..

    If you do not use MikroC
    Code (c):

    unsigned char DEC2BCD(unsigned char dec)
       {
       return (  dec /10 ) <<4  + (dec % 16);
       }
    unsigned char BCD2DEC(unsigned char bcd)
       {
       return  (bcd>> 4)* 10  + (bcd % 10);
       }
     
     
    • Like Like x 1
    • Thanks Thanks x 1
  10. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    Ian Rogers I am actually using MPLab. I searched the Microchip form but didn't find any information. Thanks for the code, I will give it a try.
     
  11. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    I am trying to implement the above code. I am reading from the rtcc over i2c in to Time[0] convert from bcd to dec and then into hh to be displayed on the lcd but all the ways I know to do it I get an error.
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,147
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Its funny!!! The main reason the digits are in BCD is so you can display them easily...

    Read hours and grab the first four bits.... then shift the 2 tens bits down.... The only time you need decimal is when you change the time!!
     
  13. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    please forgive my ignorance as I am a novice at C language, having said that I am unsure how to do what you suggest.
     
  14. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,203
    Likes:
    640
    Location:
    Derbyshire, UK
    Just google for code for the RTCC chip you're using, even if you can't find PIC C code for it, you'll find plenty of Arduino code you can adapt.
     
  15. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,147
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Nigel... No need! You have ASM code for software I2C and I have done the C variant..

    I have written my own I2C functions, but I do the whole thing as a one... ie.. The readclock(); writeclock(); functions take character pointers and do the BCD2BIN and visa~versa all in one...

    I don't even know what RTCC he is using?
     
  16. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,203
    Likes:
    640
    Location:
    Derbyshire, UK
    Neither do I, which was why I suggested google :D
     
  17. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    I am using PCF8583 on a mikro click board
     
  18. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    I am now tying to display the time that I get from the RTC in a LCD with this code
    Code (text):

    void TimeDisplay(void)
    {
        while (BusyXLCD())
        LCD_Move(0,1); //move to col 0 row 1
        while (BusyXLCD());
        itoa(lcd,hours,10);
        putsXLCD(lcd);
        while(BusyXLCD());
        putrsXLCD(":");
        while(BusyXLCD());
        itoa(lcd,minutes,10);
        putsXLCD(lcd);
        DelayXLCD();
    }
     
    How do I keep it in the "hh:mm:ss" format, ie so that the minutes are displayed 00,01,02,03 and not 0,1,2,3,
     
  19. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,008
    Likes:
    316
    Location:
    Brisbane Australia
    ONLINE
    Add a line after the itoa, if(hours<9) lcd='0' + lcd or if(hours<9) putsXLCD('0').

    I'm hoping someone will come up with a more elegant solution.

    Mike.
     
  20. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,147
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    I always use sprintf().... Much more useable..
    just include stdio.h..
    Code (c):

    void TimeDisplay(void)
    {
        char buff[17];
        sprintf(buff,"%02d:%02d",lcd.hours,lcd.minutes);
        while (BusyXLCD())
        LCD_Move(0,1); //move to col 0 row 1
        putsXLCD(buff);
    }
     
    Job done with the correct formatting..
     
    • Thanks Thanks x 1
  21. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    Ian Rogers with this format I no longer need itoa is that correct?
     

Share This Page