Decimal to BCD

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Decimal to BCD

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

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Decimal to BCD

Post by kersing »

Use:

Code: Select all

bcd = (dec / 10) << 4 + (dec % 10)
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Decimal to BCD

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

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Decimal to BCD

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

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Decimal to BCD

Post by hyperion007 »

Here is the program. I'm not the best at commenting my programming so hopefully you'll understand my programming anyway :)
Attachments
RTC DS3231 PIC18F26K80.fcfx
(49.57 KiB) Downloaded 338 times

User avatar
Jordy101091
Posts: 519
Joined: Sat Jan 08, 2011 4:02 pm
Location: The Netherlands
Has thanked: 25 times
Been thanked: 188 times
Contact:

Re: Decimal to BCD

Post 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
the will to learn, should not be stopped by any price

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Decimal to BCD

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

User avatar
Jordy101091
Posts: 519
Joined: Sat Jan 08, 2011 4:02 pm
Location: The Netherlands
Has thanked: 25 times
Been thanked: 188 times
Contact:

Re: Decimal to BCD

Post 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
the will to learn, should not be stopped by any price

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Decimal to BCD

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

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Decimal to BCD

Post 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

Post Reply