Shop OBEX P1 Docs P2 Docs Learn Events
DS1302 as an Hour meter — Parallax Forums

DS1302 as an Hour meter

Sutton MurraySutton Murray Posts: 88
edited 2008-08-13 22:07 in BASIC Stamp
Mike Green helped me to understand the way calculations are done from the DS1302 BCD format. I ended up with a formula "rawTime = (hr10 * 600) + (hr01 * 60) + (mn10 * 10) + mn01" to convert·time only(seconds still needed). The date is still even a more complex·issue(months 28,30,31 days, leapyear). I am slowly suceeded although i am not seeing this·as a solution. Starting working on the date it is quite clear that this is no a short program and the stamp capasity·is·quickly being used up. I need a lot of free stamp resourches for other parts of my project.
This long·process is all only about·substrat one simple sums varibles from another.
Surely their must be an easier way? Can anyone help me with coding on such type of program.


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For in much wisdom is much grief, and he who increases knowledge increases sorrow." ~Ecclesiastes 1:18

Comments

  • SRLMSRLM Posts: 5,045
    edited 2008-08-10 18:41
    Use the GPS. It's highly accurate, gives you the date no matter the month, and it's easy to use.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-08-10 18:44
    How about starting with a simple statement of what you want your program to accomplish rather than being caught up in details (losing sight of the forest for the trees)?
  • Sutton MurraySutton Murray Posts: 88
    edited 2008-08-10 21:37
    GPS ??? i've lost you on that one.

    Mike it feels really hopeless following this route, i was hoping for a miracles. I started off with a simaliar trial on the date as was done on the time.
    "rawDate = (Date10 * 120) + (Date01 * 12) + (Month10 * 24) + Month01". This is not working. First i need to under stand what section of process starts first in the DS1302 date format to get this formula working.
    I've done further studies relating more in your previous given formula, it seems that your way(formula) will result in a shorter program version. Found some details from Peter H. Anderson sampling "YEARS = VAL.NIB1*10 + VAL.NIB0" to start with. Converting BCD to natural binary.
    Tell me is this route the only options?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For in much wisdom is much grief, and he who increases knowledge increases sorrow." ~Ecclesiastes 1:18
  • Mike GreenMike Green Posts: 23,101
    edited 2008-08-10 22:01
    "Genius" programming made simple:

    1) When something seems overly complicated, buggy, too big, too complicated, it probably is. Start over with the original goal. Leave whatever assumptions you started with and start with the overall project goal. Don't go down the same path (at least not at first and with a lot of thought based on now new experience).

    2) If you're already thinking in terms of what parts you're going to use or what techniques you want to use, you're probably too far along down the decision making path. You may have external constraints (like your boss told you so), but more commonly you've set them yourself. You may be better off using some cheap, simple part than the fancy one that's already paid for. Maybe the cheap part will save you an immense amount of time and effort and yield the same functional result (and maybe not ... sometimes fancy parts are worth the cost). Don't assume.

    3) Be prepared to "throw one away". You can plan and design until doomsday and sometimes trying out some ideas will give you much much more information. The 2nd iteration of a design is often much cleaner because you've learned a lot doing the 1st iteration. Often some assumptions that you made early on in the design (despite being warned not to!) have constrained the rest of the implementation. Unless you run up against some fatal boundary (like you're out of memory or it really is way too slow), try to finish the 1st iteration. You'll learn more that way for the 2nd iteration.

    Post Edited (Mike Green) : 8/10/2008 10:14:09 PM GMT
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2008-08-10 22:58
    Once you have the year, month and day of month in natural binary (converted from DS1302 BCD), then you can use the following strange formula to convert that into a sequential day of year, 1 to 365 or 366 as it accounts for leap years.

    ' MM= month, DD=day of month, YY=year from 00 to 99 in the 21st century.
    JD=MM-1*30+(MM/9+MM/2)-(MM max 3/3*(YY//4 max 1 +1))+ DD
    ' JD is the day of year, 0 to 365 or 366

    This can be extended to Julian Day number counting the number of days across the years, for example, the number of days elapsed since Jan 1st, 2001.

    More math here:

    www.emesystems.com/BS2math4.htm#JulianDate

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2008-08-11 03:27
    Sutton Murray

    Please explain just what do you want with this HOUR METER


    For example
    ··································Just an··idea.gif


    What will control this Hour meter ?

    Do you want to START and STOP this Hour meter and keep the amount of time that has elapsed

    If yes to this question what will START the Timer and What will STOP the timer

    This might give some· idea.gif·s what can be done look at this post

    http://forums.parallax.com/showthread.php?p=737892

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 8/11/2008 3:43:46 AM GMT
  • Sutton MurraySutton Murray Posts: 88
    edited 2008-08-11 14:48
    Mike: Thanks for giving me the confidents, I am not planning on giving up. I will only stop when completed.

    Tracy: Seeing that I have no other reply on my search for a miracle, I will be focusing on your work(also so started with Mike). Your last Julian formulas
    "Julian minutes extended to cover many years in double precision" this is exactly what I need. If I get stuck I'll be back.
    Some excellent work that went into this, I'm glad I didn't have to go through all of that. Many thanks for sharing this me.

    Sam_sam_sam: I had a look at your project, this also has some great thoughts behind it all. I never thought on the idea of stopping the clock. Your project I will be experimenting with to get the full understanding of it.

    My hour meter will be used as a indicator reading to indicate the number of hours a specific machine has worked since it was a new machine. Start and stop are at no specific time in the program as the machine operator will simply externally switch on and off the power. It is also important to still have the real time and date active for fault recording records(other machine monitoring values).

    When I succeed in this hour meter project I will also put this on the completed projects for others.
    But for now I first need to do work of it all.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For in much wisdom is much grief, and he who increases knowledge increases sorrow." ~Ecclesiastes 1:18
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-08-11 15:49
    Sutton,

    There are two basic ways I see this being done. One is to follow Tracy’s method, using serial date as needed to covert the current date into serial format to be compared against the original start date. This not only simplified things, since the same routine can be used for the start and current date/time, but you actually know the exact date/time the unit went into service, assuming the clock was set and the info stored in NV memory.

    The other method would involve using the DS1302 as a virtual time base…In this manner it doesn’t matter what the date/time is, as long as the DS1302 has been initialized at startup so seconds are counting up. In your main routine you would essentially detect a change in seconds and use that to increment a group of counter variables. This offers a different kind of simplicity because you don’t need to worry about setting the date/time or adjusting it. In both cases NV memory could be realized using the DS1302 with a battery backup. This would prevent wearing out EEPROM constantly updating it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2008-08-12 19:40
    Sutton Murray,

    I would like to·see what you come with keep us post


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam
  • Sutton MurraySutton Murray Posts: 88
    edited 2008-08-12 20:22
    Chris

    I'm currently busy trying your exact first mentioned method.(Tracy· method). I have thrown together a short piece from Tracys formulas. See attached(slob e work, test runs only). I am·now thinking on adding the· subtraction from these·jointed word variables. I guess this needs another complicated formula. But I am already alarmed with the·half used·up Ram space.·The ram space is needed for other processes.(I no this program can improve on the ram usage, but still i have only started with it)
    Your second option i had thoughts about, but in the way of counting every routine not the seconds. But·different processes in the routine will result in an un accurate count interval lengths.(un accurate time).··I·previously figured that i need to use the·DS1302 own ram·to solve the power shutdown at any location of the program. It needed to·place the latest date and time in the 1302 ram at every routine. This to keep a record·for the unexpected power shutdown and·yes the EEPROM·that will not last in such usage.

    It seems·that your·counter method will be a better option for my project.
    How will one go about in counting the seconds in the routine?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For in much wisdom is much grief, and he who increases knowledge increases sorrow." ~Ecclesiastes 1:18
  • Sutton MurraySutton Murray Posts: 88
    edited 2008-08-12 21:35
    Chris

    I have added a new idea of my own(your second mentioned option).

    The program is not completed and not tested yet, i have to go and can·only work on it again·hopefully tomorrow.

    Note that i use the initialization stage in a different program ones only·used(before this every time used program).

    So note that these sections are not found in this program.

    But, In general am i on the right track?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For in much wisdom is much grief, and he who increases knowledge increases sorrow." ~Ecclesiastes 1:18
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2008-08-13 02:58
    It does take several variables to do the calculation, but most of them can be temporary. You are using a BS2pe. The scratchpad ram is a great place to store the results of the calculation, or in the clock ram of the DS1302.

    As I mentioned in that article, there are 52067520 minutes in the 21st century, 3124051200 seconds, and either of those numbers fit in a 32 bit double precision variable. The article also shows how to subtract two double precision numbers so that you can find out things like the elapsed time between two events, or whether a certain time is greater than or less than a reference time. On the Stamp, 32 bit math is double precision, but doing the same thing with a mishmash of incommensurate time units is very messy and bug prone.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Sutton MurraySutton Murray Posts: 88
    edited 2008-08-13 16:28
    Tracy

    I only now looked at modifiers for the byte sizes to word in the scratchpad ram. Thanks your advice will help me to try and resolve my ram space problem.

    I have attached another file Tracy, I was hoping that you will confirm the correctness of this program. The use of binary input values since 2001 and up to the second.
    Is my inputs/convertions of BCD and binary correct in this program?
    Is the 2x second stage effort to 1 seconds to the double word correct?
    Will the last final result in my program be correct?

    I no I ask a lot of questions: will the byte modifier to word values in the scratchpad ram of the DS1302 be handles in the same way as with the stamps Scratchpad ram with the Put_Get and modifiers use.

    I have not come to any subtraction yet, at a flight i don't seem to see it on your web. Where is this?

    Tracy, I will very much appreciate your help.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For in much wisdom is much grief, and he who increases knowledge increases sorrow." ~Ecclesiastes 1:18
  • Sutton MurraySutton Murray Posts: 88
    edited 2008-08-13 18:03
    Need help, all values in my last placed program ends up incorrect

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For in much wisdom is much grief, and he who increases knowledge increases sorrow." ~Ecclesiastes 1:18
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2008-08-13 22:07
    I don't have time now to look at what you have done. But I wonder when you say "all values ... end up incorrect." In such cases your best approach is to DEBUG working upward in small steps.

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