The bit RTCHour.5 is mixing the time up.. I think..
Why did they use it at a dual purpose.... !
The bit RTCHour.5 is mixing the time up.. I think..
Why did they use it at a dual purpose.... !
Lerameur,
So, just some basic high-level things to understand when dealing with these RTC ICs.
The data in the RTC registers are in BCD format.
When you make changes to them, say the Hours register you should follow some specific steps.
Get the BCD Hours from the RTC
Save the value of 12/24 mode in your code
Save the value of the am/pm bit value in your code
Mask out the am/pm and 12/24 bits if required
Convert the BCD hours to Decimal hours
Change the Decimal Hours to the value you want
Convert the Decimal Hours to BCD Hours
Set the 12/24 Bit correctly in the New BCD Hours value
Set the am/pm Bit correctly in the New BCD Hours value
Write the new BCD Hours value to the RTC
Try not not intermix operations.
Change the Hour value or change the 12/24 mode, but not both in the same action.
When in 12hr mode, change the time the way you need to for that mode.
When in 24hr mode, change the time the way you need to for that mode.
Another suggestion is create yourself a table for hours in Excel or a text editor.
List all the hours in 12hr mode and also for 24hr mode.
Then for each hour record the byte (in binary) value for the RTC Hour register you would need for that time, making sure to set/clear the 12/24 mode and am/pm bits appropriately.
Looking at that kind of table may help you visualize what you need in your code to change time values to/from different values and when changing 12/24 modes.
Just some thoughts that I followed quite a while back to get a handle on using RTC ICs.
Regards,
TABSoft
Regards,
TABSoft
Some additional direction.
12hr Mode
Display Value to LCD:
- Mask off 12/24Hr mode bit
- Mask off am/pm bit
- Display new masked value with Hex2 modifier
Change Hour Value in RTC:
- Mask off 12/24Hr mode bit
- Mask off am/pm bit
- Convert new masked value from BCD to Decimal
- Change Hour value in Decimal
- Convert Decimal Hour back to BCD
- Set am/pm bit
- Set 12/24Hr mode bit
- Write new value to RTC Hour register
24hr Mode
Display Value to LCD:
- Display value with Hex2 modifier
Change Hour Value in RTC:
- Convert value from BCD to Decimal
- Change Hour value in Decimal
- Convert Decimal Hour back to BCD
- Write new value to RTC Hour register
Here is a table that shows two hour values in their various permutations.
SampleTable.txt
Hope this helps.
Regards,
TABSoft
I am having difficulty dealing with the bit 5 and 6 of the hours..
using the subroutine add hours:
I have 11 with bi6 and bit = 0
click on addhour, brings me at hour 32 with bit 6 = 0 and bit 5 = 1
...ADDHour: 'increment hours
pause 200
if PortC.5=1 then return
if PortC.6=1 then
I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour]
hourmode = RTCHour.6
rtcAMPM = RTCHour.5
' lcdout $FE,1, "RTCH1: ", bin RTCHour
' lcdout $FE,$C0, "BCDR: ", dec BCDResult
' pause 2000
if hourmode = 0 then ' this is 24H mode 0H to 23H
' hourmode = 1
BCDResult = RTCHour
gosub Bcd2Dec
BCDResult = BCDResult + 1
if BCDResult > 23 then BCDResult = 0
Pause 2
BCDResult_temp = BCDResult
gosub Dec2Bcd
RTCHour = BCDResult
if BCDResult_temp > 12 then RTCHour.5 = 1
I2cwrite SDApin,SCLpin,$D0,$02,[RTCHour]
elseif hourmode = 1 then ' this is 12H mode 0H to 12H
' hourmode = 0
rtcAMPM = RTCHour.5 ' Assign AM/PM
RTCHour = RTCHour & %00011111 ' Bits 4-0 = Hours 0-12
BCDResult = RTCHour
' lcdout $FE,1, "RTCH1: ", bin RTCHour
' lcdout $FE,$C0, "BCDR: ", dec BCDResult
' pause 2000
gosub Bcd2Dec
BCDResult = BCDResult + 1 ' in decimal
if BCDResult > 12 then BCDResult = 1
Pause 2
gosub Dec2Bcd
RTCHour = BCDResult
RTCHour.6 = hourmode
RTCHour.5 = rtcAMPM
' lcdout $FE,1, "RTCH2: ", bin RTCHour
' lcdout $FE,$C0, "BCDR: ", bin BCDResult
' pause 2000
I2cwrite SDApin,SCLpin,$D0,$02,[RTCHour]
endif
endif
return
Concerning your table:
With 12Hr 04PM $64 0110 0100 100
Without 12Hr 04PM $4 0000 0100 4
what do you mean with or without ? the AM PM bit ?? because bit 6 is a 1 on the first line, a one means 24H mode, you have 12H showing. What am I not getting ???
Reading the DS1337 Datasheet if bit 6 is High (1) 12hr mode is selected. See snippet from the Datasheet below.
"The DS1337 can be run in either 12-hour or 24-hour mode. Bit 6 of the hours register is defined as the 12- or 24-hour mode-select bit. When high, the 12-hour mode is selected."
With or without refers to the need, when in 12Hr mode, to mask off(set to 0) BOTH the 12/24Hr and AM/PM bits when using LCDOUT, SEROUT, HSEROUT, etc ; and when changing the value of the Hours in your code to be written back to the RTC. Such as when you allow the user to change the time via buttons.
With = Bits ARE NOT masked ( leads to errors)
Without = Bits ARE masked
Not performing these actions when needed during hour value manipulation and displaying the values will lead to incorrect settings in the RTC and visually strange clock display. E.g. 44 being displayed on the LCD instead of the expected 4 AM or 64 being displayed on the LCD instead of expected 4 PM.
Last edited by Tabsoft; - 15th January 2015 at 05:44. Reason: Clarified with or without
Regards,
TABSoft
Just for grins, try doing 2 successive writes to the RTC with a 10ms pause in between.
I've ran into these kind of issues whenever the hours.6 bit is changed in the RTC.
Do this when you 1st initialize the RTC on startup and every time you write to the RTC.
The issue is when the 12/24hr mode bit is changed (hours.6).
From the DS
"All hours values, including the alarms, must be reinitialized whenever the 12/24-hour mode bit is changed."
Regards,
TABSoft
Bookmarks