Shop OBEX P1 Docs P2 Docs Learn Events
Method to keep track of dates without adding a DS1302 RTC? — Parallax Forums

Method to keep track of dates without adding a DS1302 RTC?

T&E EngineerT&E Engineer Posts: 1,396
edited 2009-03-26 15:29 in General Discussion
I have a 6432 display board that I want to be able to display a message based on the day to set of dates. For example, if it was Valentines Day, it would display 1 or 2 Valentines Day messages. If it was the Holiday Season (e.g. a weeks worth of dates lets say), it would display Holiday type messages. I already know how to display messages but I want to find an algorythm that can keep track of a date or set of dates that I can use on an SX28 (or probably more likely an SX48). The messages will just be in DATA statements or in an array string or something.

Does anyone have any ideas on how this can be easily done vice having to add a DS1302 (Real Time Clock) to check the dates. Can the SX chips keep track of dates and how? I don't need to get down to the second or hour - just day or group of days.

Thanks.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tom Smykowski: Well-well look. I already told you: I deal with the customers so the engineers don't have to. I have people skills; I am good at dealing with people. Can't you understand that? What is wrong with you people?

Comments

  • ZootZoot Posts: 2,227
    edited 2009-03-25 14:34
    Why the resistance to an RTC? With a crystal it's only several dollars. If you were just doing day of the week and time it wouldn't matter perhaps, but once you want to track the actual date things get tricky pretty fast.

    That said, you could put the length of each month into data statements for reading (e.g. 31, 28, 31, 30, 31, 30, etc) so that as you count days upward you know when to roll the month over. You would need:

    - secs count
    - mins count
    - hours count (to 23)
    - days count
    - months count
    - year (even one digit would be OK, just so you could track if it's a leap year or not)

    Don't forget code for changing all those values so you can reset time in the event of a power off or reset (another advantage of an RTC - easy battery backup).

    Psuedo-code:

    
    IF secs >= 60 THEN
        secs = 0
        INC mins
        IF mins >= 60
           mins = 0
           INC hours
           IF hours >= 24 THEN
               hours = 0
               INC dayOfWeek
               IF dayOfWeek > 7 THEN
                   dayOfWeek = 1
               ENDIF
               INC date
               READ AddrLabel + month - 1, totalDays
               IF month = 2 THEN
                   IF year & $3 = 0 THEN
                       totalDays = 29
                   ENDIF
               ENDIF
               IF date > totalDays THEN
                   date = 1
                   INC month
                   IF month > 12 THEN
                       month = 1
                        INC years
                    ENDIF
               ENDIF
           ENDIF
        ENDIF
     ENDIF    
    
    
    



    I think that covers it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php


    Post Edited (Zoot) : 3/25/2009 2:39:54 PM GMT
  • ZootZoot Posts: 2,227
    edited 2009-03-25 14:35
    P.S. -- holidays can be a bear -- example figure out Thanksgiving -- it's not so easy......... stuff like Christmas and New Year's is easy obviously. I would still use table lookups for most holidays.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php


    Post Edited (Zoot) : 3/25/2009 2:40:36 PM GMT
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2009-03-25 15:26
    So would using an RTC be easier? I could but just wanted to keep it simple. But it doesn't sound like it will.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-03-25 15:39
    Quite some math will be involved, either way.

    http://www.cpearson.com/excel/holidays.htm

    regards peter
  • Old_LadyOld_Lady Posts: 20
    edited 2009-03-25 15:41
    RTC would be a *much* simpler solution!
    I don't have a big SW background, but I've bumped up against the limits of the SX48 already... and had to cut out stuff in order to make it fit. I'm assuming that a big look up table would take a lot of room, especially since (depending on the year) the holidays can switch weeks, like Zoots said earlier.
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2009-03-25 16:16
    T&E Engineer,

    Agreed, an RTC would be a much simpler solution, but if you were doing this as a learning exercise you could spend the time coding.

    There is a "tricky" way to determine the number of days in each month without a lookup table by looking at the month value as a binary nibble and comparing Bit0 with Bit4.
    Excluding February, if the Bits disagree, then the month has 31 days, otherwise the month has 30 days.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • ZootZoot Posts: 2,227
    edited 2009-03-25 16:22
    RTCs are so cheap and easy to use and will probably be more accurate over the long-term than the SX (esp. if using a resonator on the SX). Digikey does USPS first-class shipping these days, so you can probably have them send you a chip and crystal for < $5 including shipping.

    Remember though, that the RTC will not be a holiday Calendar -- you'll still have to figure out holidays from the date or day of week or week of the month, etc. As I wrote above, for dated holidays it's easy, but for "drifting" holidays (e.g. Thanksgiving, Labor Day, etc.) it can be much trickier.

    There are some bonuses to an RTC also, like maintaining time even when shut-down or during reset, a bit of extra (battery backed-up) RAM, etc.

    Old_Lady: the lookup table for days in a month would only be 12 bytes -- so it's not really big at all:

    AddrLabel:
    DATA 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • T&amp;E EngineerT&amp;E Engineer Posts: 1,396
    edited 2009-03-25 17:16
    OK - I will go with the DS1302 since I already have working time/date code for this and the 6432 board.

    Thanks again for all the great input and thought into this!
  • pjvpjv Posts: 1,903
    edited 2009-03-25 17:20
    Hi All;

    In regard to time keeping, I need to convert in both directions between regular (Julian?) Years, Months, Days, Hours etc. and "UNIX" time, and find it quite a bear. I use Phil Pilgrim's clever algorithms for that, but in SX/B it consumes tons of precious code space that I have compacted quite a bit. Leap years really screw things up.

    Does anyone have clever shorthand tricks to share?

    Cheers,

    Peter (pjv)
  • ZootZoot Posts: 2,227
    edited 2009-03-25 19:11
    pjv -- can you post your code (maybe in a new thread). I haven't seen PhiPi's algorithms. Presume you are talking about UNIX epochal time since 1 Jan 1970. I presume leap seconds need to be handled manually as they come up unless the device is online and has access to a timeserver (in which case you could obtain epochal time w/o calculation anyway). en.wikipedia.org/wiki/Leap_second

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • StarManStarMan Posts: 306
    edited 2009-03-26 15:29
    I don't know if this helps anyone but it's kind of interesting.· I learned it in my very first programming class at UCSD in 1981.· We were learning the engineering progamming language of the future, Fortran IV... with punchcards on a Burroughs.

    For any given date it calculates the day of the week.

    Here's the wiki: http://en.wikipedia.org/wiki/Zeller's_congruence

    Chris I.
Sign In or Register to comment.