PDA

View Full Version : DS1307 - How to store Decimal Value & how to convert back to BCD?



financecatalyst
- 2nd January 2011, 17:16
I was reading about this chip (1307) and also few posts about it, I understand that it outputs and accepts data in BCD format. I have tried searching the forum for the answers. But few came very near to what I want. I tried some of the methods but could not reach to a satisfactory conclusion.

Please help me with the formula for the following:
Case 1:
If I have a temp (byte) variable. And I have the decimal value 56 in it
1) How do I change and store the BCD value in a variable to be sent to the 1307?To clarify more, I need excat 56 in BCD

Case 2:
If I have a temp (byte) variable. And I have the BCD value 56 in it
1) How do I change and store the Decimal value in a variable? To clarify more, I need excat 56 in decimal form inside my variable & NOT 86 as an equivalent decimal value for HEX.

I am making a timer to switch anything ON/OFF at user defined time. My whole code works using decimal values including the LCDOUT command for sec,min, hrs & days

But I am badly stuck in getting time from the 1307 & changing it to decimal & use it in my code. The same holds true for reversing the process and writing to the 1307.

I find it easy to convert days into decimal. But the complexity of using BCD & the structure of HRS being different to minutes & seconds is proving difficult to overcome. I am looking to opt for a 24Hr format.

mister_e
- 2nd January 2011, 19:25
If it's just for display purpose, use LCDOUT modifiers such as DEC, HEX and see what happen.

There's a couple of example in here about how to convert BCD in Decimal. Look at this one
http://www.picbasic.co.uk/forum/showthread.php?t=852

and this
http://melabs.com/resources/samples/submitted/mn1307.txt

financecatalyst
- 2nd January 2011, 23:44
Thanks a lot. Got my answer at http://www.picbasic.co.uk/forum/showthread.php?t=852