Need help comparing timestamps with the DS1302 and Prop
electromanj
Posts: 270
Hello all. I'm playing around with a simple demo to get familiar with the DS1302 and I've run into a snag.
For this demo the Propeller reads the seconds from the DS1302 into the variable secondrecord. Then if I press a pushbutton the Prop reads the seconds into the second variable. If less then 10 seconds has expired the time difference is displayed on the PST and "To soon!" is displayed. If more then 10 seconds has passed the time (hours min secs) is displayed. This all works well with exception of when seconds rolls around past :00. I've tried to correct this by subtracting secondrecord from 60 and adding second.
example: secondrecord = :53 second = :04. (60-53) +04 = (7)+4 = 11. That's not working out very well at all. I was hoping someone could look at my code and tell me where I've gone horribly wrong.
Thanks. traVis
For this demo the Propeller reads the seconds from the DS1302 into the variable secondrecord. Then if I press a pushbutton the Prop reads the seconds into the second variable. If less then 10 seconds has expired the time difference is displayed on the PST and "To soon!" is displayed. If more then 10 seconds has passed the time (hours min secs) is displayed. This all works well with exception of when seconds rolls around past :00. I've tried to correct this by subtracting secondrecord from 60 and adding second.
example: secondrecord = :53 second = :04. (60-53) +04 = (7)+4 = 11. That's not working out very well at all. I was hoping someone could look at my code and tell me where I've gone horribly wrong.
Thanks. traVis
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 OBJ SN : "Simple_Numbers" rtc : "DS1302_full" debug : "SerialMirror" VAR byte hour, minute, minuterecord, second, secondrecord, day, month, year, dow, timedif PUB init dira[0] := 0 Debug.start(31, 30, 0, 115200) waitcnt(clkfreq/5+cnt) rtc.init( 4, 5, 6 ) waitcnt(clkfreq/5+cnt) main Pub main repeat rtc.readTime( @hour, @minute, @second ) 'read time from DS1302 secondrecord := second 'capture seconds minuterecord := minute 'capture minutes debug.str(string("secondrecord =")) debug.str(SN.decx(secondrecord,2)) debug.str (string(13)) repeat until ina[0] == 0 'wait for button press rtc.readtime ( @hour,@minute,@second ) 'get current time reading debug.str (string("second =")) debug.str (SN.decx(second,2)) debug.tx(13) if minute > minuterecord if second <=9 timedif := (60-secondrecord) + second 'trying to subract secondrecord from 60 and add it to second debug.str(string("modified timedif",13)) else timedif := second - secondrecord debug.str(string("stardard timedif",13)) debug.str(string("Time difference in seconds =")) debug.str(SN.decx(timedif,2)) debug.tx(13) if timedif >= 10 Debug.str( SN.decx(hour,2) ) Debug.str( string(":")) Debug.str( SN.decx(minute,2) ) Debug.str( string(":") ) Debug.str( SN.decx(second,2)) Debug.Str(String(13,10)) waitcnt( clkfreq + cnt ) else Debug.Str (String("To soon!",13,10)) waitcnt (clkfreq+cnt)
Comments
timedif := (60-secondrecord)+second
If I plug specific numbers into that equation the result is correct. There something going on there that I don't understand.
But 15:34:30 = (60)(60)(15) + (60)(34) + 30 = 54,000 + 2040 + 30 = 56,070
Just make a simple method that returns the second since 12:00am. That's a lot easier, and accurate over a minute.
I display a result of 00000016
In a loop waiting for an input to go low you will never miss a second.
just another way of skinning the proverbial cat
regards
Jeff
Is there a problem with how the data is being stored in the variables compared to using a decimal value for multiplication or subtraction?
@Unsoundcode, hitting that RTC I2C bus pretty hard eh. Why not use a seconds timer while waiting for the low?
does that matter? just asking.
The DS1302_full converts the bcd to a binary value from the RTC so the conversion should be ok for the comparison
Jeff T.
@ Mike G Thanks for your help. The code in the first post is complete exept for a few changes I made in the last few minutes, minus the objects. Here is the archived edition.
traVis
Jeff
timedif:= second-secondrecord returns the desired result just fine (unless second happens to have wrapped past :59 which is the reason for the next line)
timedif:=60-secondrecord+second never gives the desired result. Once the 60 is out in there things go wacky. If I declare the variable to a number such as 52 things work fine. The problem arises when 60 is used in an operation with the variable as recieved from the DS1302 full object.
(or have I been at this too long today?)
Doesn't matter as long as you get the results you want. I thought this might a bit more efficient.
Jeff T.
If it hadn't been through this many times before I would feel pretty silly right now for working pretty much all day on this problem only to find out the damage was being done by this:
changed to
after changing that all is good! But as to be expected the joy was short lived as the program started treating any second difference as valid and displaying the time. On a roll, I changed to
and that took care of that issue!
I wish I could take credit for being so smart and figuring this out but the simple truth is I just kept trying this and that until the magic moment arrived!
My thinking was that by writing <= i was saying greater then or equal to..... oops.
If someone could review my initial code and the fix that I have here and explain what was happening I would be grateful....
Thanks to Mike G and Jeff for your help. I really appriciated your replies!
traVis.
>= is greater than (but not equal)
=> is equal to or greater than