Shop OBEX P1 Docs P2 Docs Learn Events
Alarm clock using ds 1302 — Parallax Forums

Alarm clock using ds 1302

anyone have any sample code on how to get a RTC to do something at a certain time like lite a light or sound a alarm.

I am using a propeller board of education with propeller spin.

Comments

  • I'm thinking you probably already have the time in some variables like hours and minutes.

    so how about something like:
    if (hours = hHour) AND (minutes = mMinute)
         do something like lite a light
         set a flag so you don't do it again
    
    

  • I'm thinking you probably already have the time in some variables like hours and minutes.

    so how about something like:
    if (hours = hHour) AND (minutes = mMinute)
         do something like lite a light
         set a flag so you don't do it again
    
    

    im getting an error i have

    if (heure = 11) AND (minutes = 18)
    lcd.init( 2, 19_200, 4) ' start lcd
    lcd.cls ' clear the lcd
    LCD.str(String("alert")) ' First line

    im getting the error at the = it says expected ")". not sure what that error is.
  • edited 2015-12-03 01:41
    if (hours = hHour) AND (minutes = mMinute)
         do something like lite a light
         set a flag so you don't do it again
    

    should be
    if (hours == hHour) AND (minutes == mMinute)
         do something like lite a light
         set a flag so you don't do it again
    
  • kodaleshen wrote: »
    anyone have any sample code on how to get a RTC to do something at a certain time like lite a light or sound a alarm.

    Pardon me for being picky about how the problem is framed, but this RTC (DS1302) will not do something. Some RTCs have an alarm function that can set an output at a certain time, but this one will require the Propeller to "do something". I believe that you already know that, but stating a problem incorrectly can lead to crazy answers on a forum.

    That said, the above post by Alexander points out a syntax issue. Within the conditional "IF" statement, using "==" will evaluate the equality of the two items (as opposed to "=" which is generally used to set one term equal to another).

    Cheers!
  • yes i caught that sorry for not stating it correctly, you know how it is when you are in the middle of a project and cant figure something simple i'm sure. I was able to realize that it was a syntax issue yesterday. also i have it communication with an LCD to show the time then display alert at a certain time. thanks for all the help everyone!
  • Sorry about the == bug; you'd think I would know better by now. senior moment, p'raps. Glad you got your alarm working.

    As an aside, one of the RTCs with a built-in alarm is the DS3232. It has a register for day-of-week which I would have implemented differently from the way they did. They specify a value (1-7) to specify the day-of-week. I would have done a mask so that you could have an alarm on a number of day-of-weeks, say Mon-Fri.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2015-12-04 16:03
    In the beginning I thought RTC device were a "must use" item for everything about time. But the Propeller can do it all better and just use the RTC in a routine at power up to reset the correct time on the Propeller.

    I have been doing this in Forth on the Propeller and using Unix's UTC as my clock's underlying format. It happens to be one 32bit value. Anything is possible, daily alarms, one shot alarms, weekly alarms, DOW alarms, Monthly alarms, Annual calender alarms. But they all read one 32 bit code in UTC that ticks at one-second rate.

    Of course, you can go either way. With using the RTC's alarm clock feature with independent power, it could actual turn on (and off) the Propeller when the alarm is triggered. That might be a very valid use.

    Forth is interactive. So it allows you to change your alarms or to override your alarms at anytime from a serial terminal. You can also monitor what alarms are set or divert an alarm to do something different - like go from beeping to flashing a LED.

    Coding daily alarms is much easier than longer periods as hours and minutes are regular cycle. DOW is also a regular cycle. But calendar events require something a bit more sophisticated as the months are different lengths, you have leap years, and the DOW relationship to calendar year has a 7 year cycle.
  • Good point, Loopy. The last two times that I used an RTC in a project, it was really just being used as a stopwatch to count seconds and minutes. With the Propeller, that function is easily accomplished with a counter in a cog that updates a Hub variable.
  • Thanks, I am feeling a little bit stupid about touting a change to Forth forth the project..

    While it does add interactive programing and interactive control the "Propeller Alarm", this project can use the UTC form in any language: SPIN, PASM, C, PropBasic.

    It just makes far more sense to me to read the RTC once, that to keep going back and forth over SPI or I2C (which can actually be a speed bottleneck).
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2015-12-05 16:39
    Actually, reading out the time and date in a RTC really annoys me. You have to cobble the pieces together to convert to a 32bit UTC.

    It would be much easier to just read a 32bit counter that is running continously on its own backup power. And I am please to see that such a device does exist and will even take its own crystal to clock by.

    The interface to the Propeller is I2C.

    If you used this, you would look up the UTC value to today's time and day and set the device by sending one 32bit value to it. From then on, you just have to read one 32bit value to represent everything and decode the portion that applies to your alarm. The 32bit counter contiously counts seconds, so I am sure it is for UTC.

    https://www.maximintegrated.com/en/products/digital/real-time-clocks/DS1672.html

    I know the other RTC chips are easy to buy in ready to go fashion, but this seems a much more direct approach for the Propeller. You can even have it share the same I2c pins with the existing EEPROM.
Sign In or Register to comment.