Page 1 of 1

Decimal to BCD

Posted: Wed Mar 19, 2014 12:41 am
by MJU
Thanks to the great Medelec35 I know how to convert BCD to decimal:



But how can I convert decimal back to BCD?

I'm using a DS3231 (real time clock) and need to send the time and date in BCD to set the clock.
For instance the seconds are the four LSB from a byte, and the tenths of the seconds are bit 4/5/6 from the byte (bit 7 is not used in the range 0-59).

So if I want to set the seconds, how can I convert the decimal value to a BCD value?

Re: Decimal to BCD

Posted: Wed Mar 19, 2014 1:33 am
by kersing
Use:

Code: Select all

bcd = (dec / 10) << 4 + (dec % 10)

Re: Decimal to BCD

Posted: Wed Mar 19, 2014 5:08 pm
by MJU
kersing wrote:Use:

Code: Select all

bcd = (dec / 10) << 4 + (dec % 10)


Thanks, but it doesn't seem to work??

As a test I use a decimal value and convert it to BCD : bcd = (dec / 10) << 4 + (dec % 10)
Then I convert the BCD back to decimal: dec = (bcd >> 4) * 10 + (bcd & 15)
And I get a different result than the first value..

Am I doing something wrong?

Re: Decimal to BCD

Posted: Wed Mar 19, 2014 8:15 pm
by hyperion007
I have just made a program for the DS3231 that you can use if you want.
This RTC is really nice and I like it much more than the DS1307. So much so that I just ordered 50 of them :)

I have to modify the program first so I'll attach it here in a couple of minutes.

Re: Decimal to BCD

Posted: Wed Mar 19, 2014 8:21 pm
by hyperion007
Here is the program. I'm not the best at commenting my programming so hopefully you'll understand my programming anyway :)

Re: Decimal to BCD

Posted: Wed Mar 19, 2014 10:08 pm
by Jordy101091
Maybe If you would like I can create a component for this DS3231 device.
Let me know If you are interested.

Regards Jordy

Re: Decimal to BCD

Posted: Wed Mar 19, 2014 10:10 pm
by hyperion007
I am :)
Would be so much simpler and neater. Feel free to use anything and everything you want from my program that I posted.

Re: Decimal to BCD

Posted: Wed Mar 19, 2014 10:25 pm
by Jordy101091
I will start tomorrow,
First a need to take my granny to the mall, so that will take most of the day :wink:
When Im back I will start building, but since I don't have this device here at home you need to be my test person, to see if everything is working as it should.
Hope you don't mind 8).

Regards Jordy.

PS. will let you know by PM

Re: Decimal to BCD

Posted: Wed Mar 19, 2014 10:38 pm
by hyperion007
Sure, no problem.

If you can check the datasheet to see if there is any feature you can add to the component that would be great. The module I have does not have pins for the interrupt pins that the IC has for example, so maybe a function to set the two different alarms? maybe a function to enable one of the INT outputs to output a 1hz square wave? Could be useful in many cases I think, one could simulate the 1hz sq wave output from a GPS for example. I will be using a GPS to set the RTC in at least one of my projects.

Re: Decimal to BCD

Posted: Wed Mar 19, 2014 11:46 pm
by MJU
Thanks to hyperion007 I've found these conversions

Decimal to BCD.
bcd = ((dec / 10) << 4) + (dec MOD 10)

BCD to decimal.
dec = ((bcd >> 4) * 10) + (bcd & 15)

Maybe these could be of any help in the future for anyone..
If anyone got a nicer way of doing this?

tags: bcd dec conversion bcd decimal