Shop OBEX P1 Docs P2 Docs Learn Events
DS1302 12 Hour Mode - Help! — Parallax Forums

DS1302 12 Hour Mode - Help!

lrohnlrohn Posts: 33
edited 2004-12-14 18:01 in BASIC Stamp
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

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-12 19:08
    I found it such a hassle to deal with that bit that I just deal with it myself. A lot of my programs want to deal with the time of day as a single value (0 - 1439), and converting to a human-friendly format after that is easy.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • lrohnlrohn Posts: 33
    edited 2004-12-12 19:14
    I understand that and know how to convert from 24 to 12 using the code supplied in some of your examples but it just drives me crazy that I can't figure out how to make the chip do it as documented by the manufacturer. Do you suppose that the individual bit needs to be addressed and singled out with something like hours.BIT7?

    Luke
  • lrohnlrohn Posts: 33
    edited 2004-12-12 19:19
    I also think that I might not be re-initializing the the hours data as the manufacture calls out for by just setting the the entire time. They state "The hours data must be re-initialized whenever the 12/24 bit is changed".
  • DaveGDaveG Posts: 84
    edited 2004-12-12 20:37
    I believe the trick to using the 12 hour mode is to make Bit 7 = 1(decimal 128), EACH TIME you set the hours.
    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
  • lrohnlrohn Posts: 33
    edited 2004-12-13 02:02
    Hello Dave, Thanks for the help, I really appreciate it and with your help I think I can overcome this hurdle!

    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
  • NewzedNewzed Posts: 2,503
    edited 2004-12-13 13:50
    LRohn, I have attached a working program for the DS1302.· Take a look at Line 80 - this gives you a 12 or 24 hour option.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    4 digit serial LED

    http://hometown.aol.com/newzed/index.html
  • DaveGDaveG Posts: 84
    edited 2004-12-13 15:32
    LRohn,

    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
  • lrohnlrohn Posts: 33
    edited 2004-12-14 10:09
    Hello Sid,

    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
  • lrohnlrohn Posts: 33
    edited 2004-12-14 11:27
    Hello Dave,

    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
  • NewzedNewzed Posts: 2,503
    edited 2004-12-14 14:54
    Luke, I always run 24 hour mode.· I have a DS1302 running but I don't want to stop it.· I will set up another DS1302 and see if I can get it ro run in 12 hours mode.· I'll get back to you.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    4 digit serial LED

    http://hometown.aol.com/newzed/index.html
    ·
  • NewzedNewzed Posts: 2,503
    edited 2004-12-14 16:01
    LRohn, I set up another DS1302 and the 12 hour mode would not work for me, so I created a fix.· Find the Getclk routine:

    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
    ·
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-12-14 16:06
    Newzed said...

    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, shouldn't it be:

    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
    ·
  • NewzedNewzed Posts: 2,503
    edited 2004-12-14 16:13
    That is probably a better way, Chris.· My clock hasn't got that far yet but come to think of it, it would probably keep repeating hrs = $01.· Thanks[noparse]:)[/noparse]



    LRohn, take note.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    4 digit serial LED

    http://hometown.aol.com/newzed/index.html
    ·
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-12-14 16:39
    Yeah, I have several time-keeping routines that go way back to the 6502 (VIC-20).· And I'm with Jon Williams, where applicable, working with 1440 seconds in the day and converting from there is almost always more flexible in that it makes it easier to calculate ranges and whether or not a time has passed...If you use subroutines for various functions, it makes it easier and takes up less program space than converting on the fly.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    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
    ·
  • DaveGDaveG Posts: 84
    edited 2004-12-14 18:01
    Luke,

    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
Sign In or Register to comment.