• 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 Conversion

Status
Not open for further replies.

TucsonDon

Member
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?
 

jpanhalt

Well-Known Member
Most Helpful Member
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
 

ci139

Active Member
bcd to binary to display on the lcd
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)
 

TucsonDon

Member
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.
 

Attachments

Last edited:

Pommie

Well-Known Member
Most Helpful Member
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:

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
If I remember correctly, you use MikroC There are conversion routines built in for this..

If you do not use MikroC
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);
   }
 

TucsonDon

Member
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.
 

TucsonDon

Member
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.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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!!
 

TucsonDon

Member
please forgive my ignorance as I am a novice at C language, having said that I am unsure how to do what you suggest.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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?
 

TucsonDon

Member
I am now tying to display the time that I get from the RTC in a LCD with this code
Code:
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,
 

Pommie

Well-Known Member
Most Helpful Member
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.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
I always use sprintf().... Much more useable..
just include stdio.h..
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..
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top