# 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
What language? What series of MCU?

John

#### TucsonDon

##### Member
Sorry, I am using C and a PIC18F45K22

#### jpanhalt

##### Well-Known 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

• 215.6 KB Views: 51
Last edited:

#### Pommie

##### Well-Known 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
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
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.

#### Nigel Goodwin

##### Super Moderator
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.

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
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?

#### Nigel Goodwin

##### Super Moderator
I don't even know what RTCC he is using?
Neither do I, which was why I suggested google

#### TucsonDon

##### Member
I am using PCF8583 on a mikro click board

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

#### TucsonDon

##### Member
Ian Rogers with this format I no longer need itoa is that correct?

Status
Not open for further replies.