DS1307 - 12 hour vs 24 hour

Hello all,

I have gotten a DS1307 working with my Arduino (easier than I expected!)

However, it appears to be running in 24-hour mode, and I want 12-hour mode. I understand the theory behind how to make it switch, and thanks to the code I stole (which included functions to set and get the time) I even know where to make the change, but I just don't know exactly what to do.

The code I stole had this line in the function to get the time:

  *hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm

the bcdToDec() function converts from BCD to decimal, FWIW. I'm guessing I need to change the 0x3f part of this line? If so, what to? I'm betting this will be an easy question for someone to answer who has more experience in this than I do.

I suppose I could put a little case statement that subtracted 12 from the value if it was over 12, but that would seem like cheating.

I suppose I could put a little case statement that subtracted 12 from the value if it was over 12, but that would seem like cheating.

Read my mind exactly. Except you would also have to make a variable for am and pm...

But:

Real Time Clock IC DS1307 : 24/12 Hour control

The second is the 24/12 hour control which is bit 6 of address 2. It is set high for 12 hour mode and low for 24 hour mode.

From: http://www.best-microcontroller-projects.com/real-time-clock-ic.html

So we do some conversions: HEX 0x3F = Binary 00111111

So we change bit 6 to 1, which now makes it: 01111111

That converted to HEX is: 0x7F

I think...

After some more thought, I might leave the configuration as it is (returning 24 hour time) and just do some math to convert to 12 hour where I want it, since there are a few parts in the program where 24 hour would be more convenient, and others where 12 hour would work best.