Decimal to BCD

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

Moderator: Benj

MJU
Posts: 449
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 113 times
Been thanked: 102 times
Contact:

Decimal to BCD

Postby MJU » Wed Mar 19, 2014 12:41 am

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?
These users thanked the author MJU for the post:
medelec35 (Wed Mar 19, 2014 2:06 pm)
Rating: 5%
 

kersing
Valued Contributor
Valued Contributor
Posts: 1907
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 552 times
Been thanked: 1064 times
Contact:

Re: Decimal to BCD

Postby kersing » Wed Mar 19, 2014 1:33 am

Use:

Code: Select all

bcd = (dec / 10) << 4 + (dec % 10)
These users thanked the author kersing for the post (total 3):
Benj (Wed Mar 19, 2014 11:40 am) • medelec35 (Wed Mar 19, 2014 2:07 pm) • MJU (Wed Mar 19, 2014 4:40 pm)
Rating: 15%
 
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

MJU
Posts: 449
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 113 times
Been thanked: 102 times
Contact:

Re: Decimal to BCD

Postby MJU » Wed Mar 19, 2014 5:08 pm

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

Postby hyperion007 » Wed Mar 19, 2014 8:15 pm

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

Postby hyperion007 » Wed Mar 19, 2014 8:21 pm

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 154 times
These users thanked the author hyperion007 for the post:
MJU (Wed Mar 19, 2014 11:47 pm)
Rating: 5%
 

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

Re: Decimal to BCD

Postby Jordy101091 » Wed Mar 19, 2014 10:08 pm

Maybe If you would like I can create a component for this DS3231 device.
Let me know If you are interested.

Regards Jordy
These users thanked the author Jordy101091 for the post:
medelec35 (Wed Mar 19, 2014 10:35 pm)
Rating: 5%
 
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

Postby hyperion007 » Wed Mar 19, 2014 10:10 pm

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: 27 times
Been thanked: 190 times
Contact:

Re: Decimal to BCD

Postby Jordy101091 » Wed Mar 19, 2014 10:25 pm

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

Postby hyperion007 » Wed Mar 19, 2014 10:38 pm

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: 449
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 113 times
Been thanked: 102 times
Contact:

Re: Decimal to BCD

Postby MJU » Wed Mar 19, 2014 11:46 pm

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