Continue to Site

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.

  • 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?
 
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
 
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)
 
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

  • hour reg.pdf
    215.6 KB · Views: 201
Last edited:
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:
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);
   }
 
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.
 
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.
 
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!!
 
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... 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?
 
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,
 
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.
 
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

New Articles From Microcontroller Tips

Back
Top