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. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,166
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    sprint() calls itoa anyway!! So you are kinda using it!!
     
  2. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    I found this code online and it works perfect for reading from the RTC in BCD and converting to binary to display on the LCD
    Code (text):

    void TransformTime(void)
    {
        hours    =  (((hours & 0xF0)  >> 4)*10  + (hours & 0x0F)) & 0x3F;   // Transform hours
        year     =   (year & 0xC0) >> 6;                                     // Transform year
        day      =  (((day & 0x30) >> 4)*10    + (day & 0x0F)) & 0x3F;      // Transform day
        month    =  (((month & 0x10)  >> 4)*10 + (month & 0x0F)) & 0x3F;    // Transform month
    }
     
    now I need to go the other way to send BCD to the RTC when set the time
     
  3. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,022
    Likes:
    317
    Location:
    Brisbane Australia
    Correct.

    Edit, when I posted this the two replies above weren't showing.

    Mike.
     
  4. dave

    Dave New Member

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


     
  5. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,022
    Likes:
    317
    Location:
    Brisbane Australia

    As I said in post 7 to go from binary to bcd add (bin/10)*6.
    I.E
    bcd=bin+((bin/10)*6);
    OR
    num+=(num/10)*6;

    Mike.
     
    • Thanks Thanks x 1
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,166
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    You're as bad as me..... 16 not 6
     
  7. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,022
    Likes:
    317
    Location:
    Brisbane Australia
    Let's see,

    47 decimal in BCD is 0x47 = 73 binary.

    47+((47/10)*6 is 47+4*6 = 73 = correct :happy:

    Your adding the difference between 10 and 16.

    Mike.
     
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,166
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Sorry! I bow to your algorithm... Not usually the way I do it.. I'm too used to shifting!!
     
  9. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    Gentlemen, thank you for your input. Pommie I had looked at Ian's algorithm in post #8 but had read right over yours, thanks for pointing it out.
     
  10. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    Code (text):

    typedef unsigned char bool;
    union
    {
        struct
        {
            bool pumptimer1:1;      /*using the bits to flag if */
            bool pumptimer2:1;      /*a run timer is in use*/
            bool pumptimer3:1;
            bool pumptimer4:1;
            bool pumptimer5:1;
            bool pumptimer6:1;
            bool pumptimer7:1;
            bool pumptimer8:1;
        };
    }pmptmr;        //pump timer status

      ++bb;
        timer 0x01 <<bb;            //bit shift to the left "bb" times
        if (pmptmr && timer &= On)
        {
            I2C1_MasterRead(*pdata,3,RTCC,*pflag);
        }
       
    I am setting up a timer to start and stop a pump. The RTC has 240x8 bytes that I am using in five byte blocks. I am using the above struct to flag which timers are in use. I am tring to mask pmptmr with timer (and bit shifting) to pick out which are active but I need help to get it to work
     

Share This Page