DS1302 12 Hour Mode - Help!
lrohn
Posts: 33
Hello All,
I have been playing with the DS1302 now for about a month and can now really appreciate this handy little RTC chip but I am having a problem getting it into 12 hour mode.
I am really in need of someones help at this point.
To bring you up to speed, I know how to initialize the chip by clearing the write protect mode and then writing the time to the chip and displaying it on the debug terminal.
I have followed my very same steps with the write protect register cleared and then wrote $80 (%10000000) to register $84 (write hours register) to make Bit 7 of that register a 1. Per the docs Bit 7 of the hours register is defined as the 12- or 24- hour mode select bit (Bit 7 a 1 for 12 Hour mode or Bit 7 a 0 for 24 hour mode). Is then goes on to say that "The hours data must be re-initialized whenever the 12/24 bit is changed.
So the steps I am taking are to initialize the chip by clearing the write protect register. Then I access register $84 and write $80 (%10000000) to it to set Bit 7 to a 1 (12 hour mode). And then I run my code to set the time to 12:59:00. I wait a minute and the time changes to 13:00:00 not 1:00:00 as I had expected!
Has anyone else ran into this roadblock that might be able to offer me any help. It would be greatly appreciated.
Thanks again!
Luke
I have been playing with the DS1302 now for about a month and can now really appreciate this handy little RTC chip but I am having a problem getting it into 12 hour mode.
I am really in need of someones help at this point.
To bring you up to speed, I know how to initialize the chip by clearing the write protect mode and then writing the time to the chip and displaying it on the debug terminal.
I have followed my very same steps with the write protect register cleared and then wrote $80 (%10000000) to register $84 (write hours register) to make Bit 7 of that register a 1. Per the docs Bit 7 of the hours register is defined as the 12- or 24- hour mode select bit (Bit 7 a 1 for 12 Hour mode or Bit 7 a 0 for 24 hour mode). Is then goes on to say that "The hours data must be re-initialized whenever the 12/24 bit is changed.
So the steps I am taking are to initialize the chip by clearing the write protect register. Then I access register $84 and write $80 (%10000000) to it to set Bit 7 to a 1 (12 hour mode). And then I run my code to set the time to 12:59:00. I wait a minute and the time changes to 13:00:00 not 1:00:00 as I had expected!
Has anyone else ran into this roadblock that might be able to offer me any help. It would be greatly appreciated.
Thanks again!
Luke
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
Luke
Plus you must make Bit 5 = 1 for PM(decimal 32). For AM, Bit 5 = 0
This is fairly simple to do by adding a 32 and 128 to the Hours value, just before you send it to the chip.
Example:
'First, you set the hours(1 to 12) as usual, then
Temp = Hours + 32 + 128 'if AM do not add 32
HIGH RTCReset
SHIFTOUT Dta, Clk, LSBFIRST, [noparse][[/noparse]%0\1, RTCCmd\5, %10\2, Temp]
LOW RTCReset
Dave G
A snippet of my code can be found below. I tried what you explained to me and instead of 12:59:50 I get 00:59:50. I also tried to run the program with the commented out code and got the following 12:59:50 but 10 seconds later the time changed to 13:00:00.
HIGH CE
SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]WrSecs, Seconds] ' Seconds = 50
LOW CE
HIGH CE
SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]WrMin, Minutes] 'Minutes = 59
LOW CE
Temp = Hours + 32 + 128 'if AM do not add 32 ' Hours = 12
HIGH CE
SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]%0\1, WrHrs\5, %10\2, Temp]
LOW CE
' HIGH CE
' SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]WrHrs, Hours]
' LOW CE
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
4 digit serial LED
http://hometown.aol.com/newzed/index.html
Try this code:
'make Hours = 12 then
Hours = Hours + 32 + 128 'if AM do not add 32
HIGH CE
SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]WrHrs, Hours]
LOW CE
Also, to read the "hour" you should only look at Hours.LOWNIB.
To read whether it is AM or PM, look at Hours.Bit5.
Dave G
Thanks for help. I tried your program and modified it only slightly to work with the BS2SX that I am using. What I did was change the stamp directive to '{$STAMP BS2sx} and the Serin baud rate from 16468 to 16624 to make it compatible with the 9600 baud mode of the BS2SX.
I also changed the pin constants to be compatible with the pins I was using which were pins 0, 1 and 2.
After doing this I was able to set the time in the debug terminal. I have to say that this is some slick code and the program ran flawlessly. Thanks for sharing it with me. By pressing "S" I was able to set the time. I set it to 12:59:00 and 30 seconds later the time changed to 13:00:00.
I then went and modified line 80 from tMode = T24hr to tMode = T12hr and ran the program again. I set the time just as I did before to 12:59:00 and 30 seconds later the time read 13:00:00. The time did not change to 1:00:00 as I had hope. There has got to be some small thing that I am overlooking to get the clock to work in 12 hour mode and I just can't seem to find out what.
I am attaching a copy of the modified code so that you may look at it. Do you have any other suggestions?
I really appreciate your involvement with this endeavor and your help provided.
Thanks again,
Luke
Post Edited (LRohn) : 12/14/2004 10:10:37 AM GMT
I made the change to my code as you suggested and ran the following sub routines to set and read the time from the DS1302 and to display it in the debug terminal:
Set_Time:
HIGH CE
SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]WrSecs, Seconds]
LOW CE
HIGH CE
SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]WrMin, Minutes]
LOW CE
'make Hours = 12 then
Hours = Hours + 32 + 128 'if AM do not add 32
HIGH CE
SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]WrHrs, Hours]
LOW CE
RETURN
Get_Time:
HIGH CE
SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]RdBurst]
SHIFTIN DataIO, Clock, LSBPRE, [noparse][[/noparse]seconds, minutes, Hours, date, month, day, year]
LOW CE
RETURN
Update_Display:
DEBUG HOME, "The Time is: " ,HEX hours.LOWNIB,":",HEX2 minutes ,":" ,HEX2 seconds
RETURN
I had the same output on the debug terminal whether I used hours.LOWNIB when I displayed the time and wrote it to the debug terminal or if I just read and displayed hours.
The display became jerky however when I displayed hours.LOWNIB as shown in the code below, but the output was exactly the same.
The results were:
I set "hours" to 12, "minutes" to 59 and "seconds" to 50 and the time displayed on the deug terminal was B2:59:50 and ten seconds later it changed to A1:00:00
Does this make any sense?
I'll include my entire code for you to look over.
Once again I appreciate you helping me overcome this hurdle.
Thanks again,
Luke
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
4 digit serial LED
http://hometown.aol.com/newzed/index.html
·
GetClk:
· HIGH Rst
· SHIFTOUT Dio,Clkc,LSBFIRST,[noparse][[/noparse]191]
· SHIFTIN Dio,Clkc,LSBPRE,[noparse][[/noparse]secs,mins,hrs]
· SHIFTIN Dio,Clkc,LSBPRE,[noparse][[/noparse]date,month,day,year]
· SHIFTIN Dio,Clkc,LSBPRE,[noparse][[/noparse]CTRL]
· LOW Rst
RETURN
Between LOW rst and RETURN insert the following:
IF hrs>$12 THEN
hrs = $01
ENDIF
This works for me - should work for you.· Don't know what the problem with T12hour is - I wrote exactly what the data sheet specified.· Maybe it has something to do with the AM/PM.· Anyway, now you have a fix.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
4 digit serial LED
http://hometown.aol.com/newzed/index.html
·
IF hrs > $12 THEN
·· hrs = hrs - $12
ENDIF
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Knight Designs
324 West Main Street
P.O. Box 97
Montour Falls, NY 14865
(607) 535-6777
Business Page:·· http://www.knightdesigns.com
Personal Page:··· http://www.lightlink.com/dream/chris
Designs Page:··· http://www.lightlink.com/dream/designs
·
LRohn, take note.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
4 digit serial LED
http://hometown.aol.com/newzed/index.html
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Knight Designs
324 West Main Street
P.O. Box 97
Montour Falls, NY 14865
(607) 535-6777
Business Page:·· http://www.knightdesigns.com
Personal Page:··· http://www.lightlink.com/dream/chris
Designs Page:··· http://www.lightlink.com/dream/designs
·
Your program looks fine. The only problem is with the DEBUG statement. You need to change the HEX modifiers to
DEC modifiers.
Original:
DEBUG HOME, "The Time is: " ,HEX hours.LOWNIB,":",HEX2 minutes ,":" ,HEX2 seconds
New:
DEBUG HOME, "The Time is: " ,DEC2 hours.LOWNIB,":",DEC2 minutes ,":" ,DEC2 seconds
I'll try to explain why you saw "A1:00:00" at 1:00pm:
In the 12 hour mode, at 1pm, the Hours register contains the following binary number: %10100001
LowNib = %0001 = 1 o'clock
HighNib = %1010 = Hex A
HighNib.Bit0 = Not used in 12 hour mode
HighNib.Bit1 = 1, if PM
HighNib.Bit2 = Not used
HighNib.Bit3 = 1, if 12 hour mode
So, when in the 12 hour mode, you must display ONLY the Hours.LOWNIB, because the
HighNib can contain the PM Bit and the Mode Bit, which confuses things.
Dave G