Shop OBEX P1 Docs P2 Docs Learn Events
DS 1307 Real Time clock and functions — Parallax Forums

DS 1307 Real Time clock and functions

ryankh2450ryankh2450 Posts: 5
edited 2007-07-17 01:58 in BASIC Stamp
I'm relatively new to stamp programming and electronics. I went through most of the exercises on the homework board sometime ago, but I still have much to learn.

I am using a PDB board and BS2px stamp to prototype an electronic unit that will control the cycle times (on/off) of electrical devices. Very simple task. However, I am having some issues. It wasn't all that easy for me to pull the time out of the built in real time clock - but I did manage to figure it out. What I am trying to do now seems like it should be very simple. I need to use the clock to time a device to turn on for a certain amount of seconds, turn that device off and turn another one on etc. It needs to do this for 6 devices. Accuracy is important, but only within +/- one second. If someone knew how to make the clock work like a timer, instead of a clock, it would make my job much easier and I wouldn't be forced to start throwing things.

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2007-07-16 16:42
    "Forced to throw things" -- my goodness.

    Well, it's not a count-down timer.· So, you'll have to establish a hierarchy of tasks, poll/query the RTC, and then perform those tasks.
    What_to_do:
      Get the time
      IF time = x THEN turn_on_A
      IF time = y THEN turn_off_B
     
    GOTO What_to_do
    
    
  • ryankh2450ryankh2450 Posts: 5
    edited 2007-07-16 17:04
    I think I am probably making this way too difficult. Luckily, I don't have to use the timer as a count down timer, infact, it would be much better if it would just count up - in seconds.

    I can make the clock display in hours[noparse]:mins:[/noparse]secs and print the day below... that is all good. However, when I try to combine the hours and mins, it gives me the wrong answer [noparse][[/noparse]secs = (hours * 60) + secs] and even that is flawed because if it is 10:00:12 is not 12 seconds.... at least if one is trying to run the clock like a timer.

    Thank you very much PJ Allen. It's hard to help those that can barely help themselves. I am learning though.....

    Attached is the code I am using.
  • ryankh2450ryankh2450 Posts: 5
    edited 2007-07-16 17:06
    A quick note that I should have added.... inbetween the 'start chime and 'end chime is where the actual program goes that turns the devices on and off.
  • SSteveSSteve Posts: 808
    edited 2007-07-16 17:19
    You may also want to look at the DS1602. It's an elapsed time counter instead of a clock. It merely counts seconds, so it might be more applicable to your task.

    It uses synchronous serial communication instead of I2C, but there is a good explanation of this protocol in experiments #28 & #29 in the StampWorks Manual (downloadable for free from [noparse][[/noparse]url]http://www.parallax.com/detail.asp?product_id=27297[noparse][[/noparse]/url).

    You can use the clock chip, but you just need to do a little math. If your time intervals are all less than 60 seconds, you won't even need to do too much of that because you can ignore everything except the seconds field of the current time. Something like this should work:

    gosub GetTime
    target = (secs + interval) // 60
    repeat
      gosub GetTime
    until secs = target
    ' take action
    



    If you have intervals greater than 60 seconds, you'll need to incorporate minutes into the equation.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
  • SSteveSSteve Posts: 808
    edited 2007-07-16 17:23
    I was writing my reply while you were writing your two additional posts.

    "(hours * 60) + seconds" is incorrect. It's "(hours * 3600) + (minutes * 60) + seconds". But 16 bits isn't enough to hold the number of seconds in a day. Do you need to be able to count that many seconds?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
  • ryankh2450ryankh2450 Posts: 5
    edited 2007-07-16 17:42
    I need to be able to count up to 16 mins in seconds.... I made a mistake in my example - I meant (mins * 60) not hrs.

    Thank you
  • SSteveSSteve Posts: 808
    edited 2007-07-16 17:48
    In that case, something like this:
    gosub Get_Clock
    target = (mins * 60 + secs + interval) // 3600
    repeat
      gosub Get_Clock
    until mins * 60 + secs = target
    ' take action
    
    



    Keep in mind that this is just pseudo-code to give you an idea. It's not plug-and-play.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-07-16 18:33
    You have to remember that the data from these clock chips is coded in BCD, not flat decimal. So the conversion formula is

    elapsedSecs = minutes.nib1 * 10 + minutes.nib0 * 6 + seconds.nib1 * 10 + seconds.nib0

    That is computed left to right and comes out at 3599 at the end of an hour.

    You could reset the clock to zero at the start of each event, and time up to the assigned interval.

    Another way to do this with the DS1307 is to use the square wave output from pin 7. With a pullup resistor and connection to a Stamp pin, that gives you a one second heartbeat and the program could simply count up seconds by watching the transitions on that pin.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • NewzedNewzed Posts: 2,503
    edited 2007-07-16 18:38
    Tracy, I have a DS1307 piggyback and the·clock setting and time readout··is coded in HEX2 format.· Works great and very simple to handle.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-07-16 19:01
    Good point, Sid,

    The I2CIN command can use the HEX2 formatters to read in the data, and in that case, the time values in the Stamp will no longer be in BCD format.

    I2CIN 8,$D1,0,[noparse][[/noparse]STR clockdata\7]  ' read seconds to year, 7 BCD values
    elapsedSecs = minutes.nib1 * 10 + minutes.nib0 * 6 + seconds.nib1 * 10 + seconds.nib0
    



    versus

    I2CIN 8,$D1,0,[noparse][[/noparse]HEX2 seconds,HEX2 minutes, HEX2 hours, HEX2 DAY, HEX2 DATE, HEX2 month, HEX2 year] 
    elapsedSecs = minutes * 60 + seconds  ' values are not BCD, can use simpler math
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • ryankh2450ryankh2450 Posts: 5
    edited 2007-07-16 19:11
    Thank you very much for all the help and examples. I used Tracy's example, tweaked it a little to fit what I need. The formula is correct and that's what matters. It is going to work, I just need to figure out the argument to put it all together... I am able to display elapsed seconds and that's 99% of my battle right there.

    Thank you very much. I think ya'll might keep me from getting fired today.
  • NewzedNewzed Posts: 2,503
    edited 2007-07-16 22:04
    Tracy, I just completed an order for 30 clock piggybacks.· In addition to the regular timekeeping, he wanted the date displayed per the Julian calendar.· Since I am not prone to extoll my accomplishments, I won't tell you how clever I think the program is.··I have attached it so you could take a look - maybe someday you'll need a Julian calendar.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
  • NewzedNewzed Posts: 2,503
    edited 2007-07-16 22:06
    Forgot to mention, Tracy - it's a DS1302, not a DS1307.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-07-17 01:58
    Hi Sid,

    Granted, you are feeling very clever, and I too,once a century or so, feel that I have outwitted Father Time. So I had put up a page about Julian time calculations here, even before the year 2000 bug came along. I actually do use Julian time frequently. For example, when a data logging project has to start on such and such a date at a certain time and then go for a certain number of days and hours, it is much more convenient to reduce it all to Julian time, simple sequential numbers from an arbitrary starting point. The events become simple comparisons of magnitude, and special case programming for crossing boundaries of days, months, years is avoided. And if you carry it far enough, centuries and epochs. Someone else can worry about that starting in 2099.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.