DS1302 measuring elapsed time (stop watch) ?
I got the RTC to work and display time/date onto an LCD, but what I am after is using it for measuring elapsed time, somewhere in the ranges of 45 min to 2hrs 45mins (seconds are not important) I want to set it up like a stopwatch where I can push a button to start measuring time elapsed and then push the button again to stop and display the amount of time elapsed. My problem is in figuring out how to do the math on the hrs/mins data from the DS1302 to count elapsed time? My clock is in 24hr mode, and then the next question would be how to compensate for timing 'over midnight' and doing the math from when the hours go from 24 to 1. I think what is confusing me is that the data is displayed in HEX2 format, and to go from hour HEX2 "09" to hour HEX2 "10" it is skipping HEX2 "0A" to HEX2 "0F"
I am thinking others have built stopwatch type applications, and if you could point me to any examples/discussions or just give a general idea of how to go about doing this?
Thanks
I am thinking others have built stopwatch type applications, and if you could point me to any examples/discussions or just give a general idea of how to go about doing this?
Thanks

Comments
·
·Here is where I used in a Project @ this Post
http://forums.parallax.com/showthread.php?p=737892
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any·
·
·
·
·
Sam
Post Edited (sam_sam_sam) : 1/8/2009 12:15:45 AM GMT
Here what you have to do if you use two DS1302 time chips
One word of caution you will run out of memory very fast if you are not ·careful how you write your code routine this happen to when writing this code using two time chip if you want to keep track of total timed
event
If you can find a way to use the DS1302 Ram to save these values Please Post Your Code
I would like to see it when your done
Here is the code where Rev the frist code
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any·
·
·
·
·
Sam
Post Edited (sam_sam_sam) : 1/8/2009 2:00:55 AM GMT
~techstudent
julian0 = hours.nib1 * 10 + hours.nib0 * 6 + minutes.nib1 * 10 + minutes.nib0
That converts the HEX2 into a number that goes straight from 0 to 1439 as time goes from 00:00 to 23:59. The format from the clock is actually called BCD (binary coded decimal), which is similar to HEX, but as you noticed, it includes only digits 0 to 9 and not A to F. The above calculation goes strictly left to right (no precedence for multiplication, a peculiarity of Stamp math).
At the end of the time interval you calculate again
julian1 = hours.nib1 * 10 + hours.nib0 * 6 + minutes.nib1 * 10 + minutes.nib0
Then
elapsed = julian1 - julian0 + 1440 // 1440
The purpose of +1440//1440 is to account for possible transitions past midnight, positive definite.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Another benefit is you can have multiple counters, each with a resolution of 1 second. These can count up (like a stopwatch) or down (like a count-down timer) and can occur concurrently with each other. If you like each can be enabled/disabled via flag variable.
Basically all you do is in your main loop when you need to count, each time the seconds change you update your counters as well as the old seconds value. This happens each iteration at exactly 1 second intervals and allows for many other events to happen between the changes, such as updating the enabled (if you use that) counters and activating any sort of indication of a completed event (or logging the date/time). I hope this helps. Take care.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Engineering
seems like this is exactly what I need to do, but I am not experienced enough to figure out the code. What I am trying to do is have my camera take pictures at defined intervals (e.g every hour or once every 24 hrs). I would like to use the DS1302 as a count up clock, and at the appropriate intervals, trigger the picture. Can you give me an example of code on how to implement that with the DS1302?
Thanks
' ========================================================================= ' ' File...... DS1302_Demo.bs2 ' Purpose... Demonstrate The DS1302 Clock & RAM Functions ' Author.... Chris Savage -- Parallax, Inc. ' E-mail.... [EMAIL="csavage@parallax.com"]csavage@parallax.com[/EMAIL] ' Started... ' Updated... 10-09-2005 ' ' {$STAMP BS2} ' {$PBASIC 2.5} ' ' ========================================================================= ' -----[ Program Description ]--------------------------------------------- ' This code demonstrates the use of the DS1302 RTC/RAM Chip with the BASIC ' Stamp 2 Microcontroller, making easy use of the 12 Hour & 24 Hour time ' modes, as well as accessing the RAM. The sample code will use the DEBUG ' window for input and output for simplicity. You can change the output ' to the device of your choice. For example, using a Parallax Serial LCD ' requires very few changes to the code to use it for output. ' A modified version of this code was adapted to display the date and time ' on a Parallax Serial LCD Display (#27976), also with backlight (#27977) ' and on Parallax Professional Development Board (#28138, coming soon!) ' using a MAX7219 8-Digit Display Driver. ' This code will work on all BS2 Stamp Modules. ' ' The Parallax 2 X 16 Serial LCD Displays are availble at the following ' links: ' [URL]http://www.parallax.com/detail.asp?product_id=27976[/URL] ' [URL]http://www.parallax.com/detail.asp?product_id=27977[/URL] ' ' Pin Assignments ' 0 DS1302 DataIO ' 1 DS1302 Clock ' 2 DS1302 Chip Select ' -----[ Revision History ]------------------------------------------------ ' -----[ I/O Definitions ]------------------------------------------------- DataIO PIN 4 ' DS1302.6 Clock PIN 3 ' DS1302.7 CS1302 PIN 5 ' DS1302.5 ' -----[ Constants ]------------------------------------------------------- WrSecs CON $80 ' Write Seconds RdSecs CON $81 ' Read Seconds WrMins CON $82 ' Write Minutes RdMins CON $83 ' Read Minutes WrHrs CON $84 ' Write Hours RdHrs CON $85 ' Read Hours CWPr CON $8E ' Write Protect Register WPr1 CON $80 ' Set Write Protect WPr0 CON $00 ' Clear Write Protect WrBurst CON $BE ' Write Burst Of Data RdBurst CON $BF ' Read Burst Of Data WrRam CON $C0 ' Write RAM Data RdRam CON $C1 ' Read RAM Data ' -----[ Variables ]------------------------------------------------------- index VAR Byte ' Loop Counter reg VAR Byte ' Read/Write Address ioByte VAR Byte ' Data To/From DS1302 secs VAR Byte ' Seconds secs01 VAR secs.LOWNIB secs10 VAR secs.HIGHNIB mins VAR Byte ' Minutes mins01 VAR mins.LOWNIB mins10 VAR mins.HIGHNIB hrs VAR Byte ' Hours hrs01 VAR hrs.LOWNIB hrs10 VAR hrs.HIGHNIB date VAR Byte month VAR Byte day VAR Nib ' Day year VAR Byte ' Year work VAR Byte ' Work Data ' -----[ Initialization ]-------------------------------------------------- Init: reg = CWPr ' Initialize DS1302 ioByte = WPr0 ' Clear Write Protect GOSUB RTC_Out ' Send Command ' -----[ Program Code ]---------------------------------------------------- Start: hrs = $00 mins = $00 secs = $00 GOSUB Set_Time DO LOW 0 GOSUB Get_Time DEBUG HOME, HEX2 hrs, ":", HEX2 mins, ":", HEX2 secs IF hrs = $00 AND mins = $59 AND secs = $00 THEN EXIT LOOP DEBUG CR, CR, "00:59:00 Elapsed" HIGH 0 PAUSE 100 GOTO start ' -----[ Subroutines ]----------------------------------------------------- RTC_Out: HIGH CS1302 ' Select DS1302 SHIFTOUT DataIO, Clock, LSBFIRST, [reg, ioByte] LOW CS1302 ' Deselect DS1302 RETURN RTC_In: HIGH CS1302 ' Select DS1302 SHIFTOUT DataIO, Clock, LSBFIRST, [reg] SHIFTIN DataIO, Clock, LSBPRE, [ioByte] LOW CS1302 ' Deselect DS1302 RETURN Set_Time: ' DS1302 Burst Write HIGH CS1302 ' Select DS1302 SHIFTOUT DataIO, Clock, LSBFIRST, [WrBurst] SHIFTOUT DataIO, Clock, LSBFIRST, [secs, mins, hrs, date, month, day, year, 0] LOW CS1302 ' Deselect DS1302 RETURN Get_Time: ' DS1302 Burst Read HIGH CS1302 ' Select DS1302 SHIFTOUT DataIO, Clock, LSBFIRST, [RdBurst] SHIFTIN DataIO, Clock, LSBPRE, [secs, mins, hrs, date, month, day, year] LOW CS1302 ' Deselect DS1302 RETURNI hope this helps
Here's what I did so far, using Chris's DS1302 template and ideas I gleaned from your response.
I can adjust time for exposure (shutter speed of camera) and interval the photos are taken.
The one last thing I would like to do is be able to stop the program after a certain period of time. Is there a way the program can count the number of iterations that the program completes from the start: portion through the goto start portion?
fsb
Please explain in more detail what you want to do here
Are you wanting to stop the " Timer "for some amount of time ?
or
Do you want to wait for some amount of time before the your code routine run again ?
What you have to do is different in the way you write the code for the first question vers the second question
I am trying to stop the process after a certain amount of time. For instance, if I am taking a picture every hour, I would, for instance, want to stop after 48 hrs. This would be 48 iterations of the program. Not sure if BS2 can do something like that. May have to learn spin
fsb
OR
Start: iterations = 00 hrs = $00 mins = $00 secs = $00 GOSUB Set_Time 'starts timer from zero DO GOSUB Get_Time 'moniters timer DEBUG HOME, HEX2 mins, ":", HEX2 secs IF hrs = int_hr AND mins = int_min AND secs = int_sec THEN EXIT ' ^ < .......You need to change this to a subroutine not EXIT ......> LOOP HIGH led PAUSE exp_time LOW led iterations = iterations + 1 ' <............ For this line to work the way you want this to work GOTO startSomething like this
I hope this helps
I will try this
fsb
If you need help with this Please ask and I will help you as much as I can how ever I do not have one set to test the code right now but it is not hard to do
This is some thing a little while ago so might give you idea what can be done
For the DS1302 to save your data all you need a 1 MFD super cap to do this with this set up it would hold you data for more than a week
Here is where you buy a MFD super cap
http://www.allelectronics.com/make-a-store/item/CBC-17/1-FARAD-5.5-VOLT-SUPER-CAP//1.html
WriteRam: ' Routine to Save Data to a DS1302 Time Chip ' Send Command reg = $FE HIGH Timer SHIFTOUT DataIO, Clock,LSBFIRST, [reg] SHIFTOUT DataIO, Clock, LSBFIRST, [PDSecs.HIGHBYTE,PDSecs.LOWBYTE,SLSecs.HIGHBYTE,SLSecs.LOWBYTE,S,M,H,T] LOW Timer RETURN ReadRam : 'Routine to Read Data from DS1302 Time Chip reg = $FF HIGH Timer SHIFTOUT DataIO, Clock, LSBFIRST, [reg] SHIFTIN DataIO, Clock, LSBPRE, [PDSecs.HIGHBYTE,PDSecs.LOWBYTE,SLSecs.HIGHBYTE,SLSecs.LOWBYTE,S,M,H,T] LOW Timer DEBUG HOME, "Product = ",DEC5 PD," Silo = ",DEC5 SL, CR, CR, DEC5 ? PDSecs, DEC5 ? SLSecs,CR DEBUG DEC2 T, " T ",CR, CR, " H = ", DEC2 H, " M = ", DEC2 M," S = ", DEC2 S Metered: GOSUB ReadRam PD = PDsecs + (PDsecs ** 13741) ' Caculate Sand used from Sand Silo SL = SLsecs + (SLsecs ** 13741) T = (SL/500) * 1 ' T = Caculate percentage of product used 'S = seconds ' Timer Counter Names are one letter because there is not enough- 'M = Minutes ' -space to show all Data on LCD Display 'H = Hours IF s = 60 THEN S = 00 M = M + 1 IF M = 60 THEN M = 00 H = H + 1 ENDIF ENDIF IF oldsecs <> secs THEN ' This Routine Compars the oldsecs to secs oldsecs = secs ' This Routine is so that Counter Update once a Seconds S = S + 1 ' This for the Timer Counter for the amount of time has that been used by the Sand Silo PDSecs = PDSecs + 1 SLSecs = SLSecs + 1 ENDIF GOSUB WriteRam RETURN