A simple timer
A simple timer
Hello all,
I need to create a simple timer that will let me know how long my circuit has been running on batteries. The format I need is H:mm:ss. I don't foresee the circuit running more than 9 hours 59 minutes and 59 seconds.
I can see that the chip is easy capable of doing this multiple ways. What I am looking for is the most efficient way to do this. I don't need to be extremely accurate maybe off by a few seconds. I just need to be able to record how long the batteries have been used for the purposes of replacement.
Thanks
Patrick
I need to create a simple timer that will let me know how long my circuit has been running on batteries. The format I need is H:mm:ss. I don't foresee the circuit running more than 9 hours 59 minutes and 59 seconds.
I can see that the chip is easy capable of doing this multiple ways. What I am looking for is the most efficient way to do this. I don't need to be extremely accurate maybe off by a few seconds. I just need to be able to record how long the batteries have been used for the purposes of replacement.
Thanks
Patrick
When it starts running on battery set Register.RTCStopWatch=0. Then read Register.RTCStopWatch periodically and do the math to get H:MM:SS.
- TotalSeconds = Register.RTCStopWatch \ 512
SS = TotalSeconds Mod 60
TotalMinutes = TotalSeconds \ 60
MM = TotalMinutes Mod 60
HH = Total Minutes \ 60
Last edited by DH* on 26 July 2006, 8:38 AM, edited 1 time in total.
Determining elapsed time from start of the current execution is trivial, Patrick; that's the value currently in the RTC, which starts at midnight every time you apply power. Your problem is how to store an accumulation of that time without missing much (you don't have the luxury of a "powering-down" interrrupt that might allow to to save the time just before losing control) - and you must avoid burning up EEPROM, which usually means you must distribute the accumulating value you want to store over several or many changing EEPROM locations.
This has been discussed on the Basic-X group; search there for "Persistant" and "Hobbs", and you'll can find Atmel's PDF on the matter in the file section.
Tom
This has been discussed on the Basic-X group; search there for "Persistant" and "Hobbs", and you'll can find Atmel's PDF on the matter in the file section.
Tom
Tom
Format to look like a stopwatch
Using some ideas I have seen I think I might have a sort a functioning stopwatch.
How do I format it so it looks correct? H:MM:SS this is kicking my butt.
I want to display it on my BPI-216 LCD display
Thanks
Patrick
How do I format it so it looks correct? H:MM:SS this is kicking my butt.
I want to display it on my BPI-216 LCD display
Thanks
Patrick
The easiest way to accomplish your objective, assuming that you have the values in variables name h, m and s is:
However, that may not produce the desired result when the component values are less than 10. You can ensure a fixed string length (i.e. leading zero added when necessary) with a little more work:
You could simplify this a bit by introducing a new subroutine to do some of the work. The code below also adds some defensive code to ensure that at most two digits are added for each element.
Code: Select all
Dim timeStr as String
timeStr = CStr(h) & ":" & CStr(,) & ":" & CStr(s)
Code: Select all
Dim timeStr as String
timeStr = ""
If (h < 10) Then
timeStr = timeStr & "0"
End If
timeStr = timeStr & CStr(h) & ":"
If (m < 10) Then
timeStr = timeStr & "0"
End If
timeStr = timeStr & CStr(m) & ":"
If (s < 10) Then
timeStr = timeStr & "0"
End If
timeStr = timeStr & CStr(s)
Code: Select all
Sub Main()
Dim h as Byte, m as Byte, s as Byte
Dim str as String
str = ""
Call addTimeElement(h, str, "")
Call addTimeElement(m, str, ":")
Call addTimeElement(s, str, ":")
Debug.Print str
End Sub
Sub addTimeElement(ByVal val as Byte, ByRef timeStr as String, ByVal prefix as String)
If (Len(prefix) > 0) Then
timeStr = timeStr & prefix
End If
If (val < 10) Then
timeStr = timeStr & "0"
ElseIf (val > 59) Then
val = 59
End If
timeStr = timeStr & CStr(val)
End Sub
- Don Kinzer
here's an extract from a ZBasic program that is in the FILES area of this forum.
viewtopic.php?t=55
It's another example of formatting time, and date as well. Hope this is helpful.
viewtopic.php?t=55
It's another example of formatting time, and date as well. Hope this is helpful.
Code: Select all
' return a string containing the date and time as passed, formatted a certain way...
public function formatDateTime1( _
byval y as integer, m as byte, d as byte, _
byval h as byte, byval mm as byte, byval sec as single _
)as STRING
formatDateTime1 = _
DayName(DayOfWeek(y, m, d)) & " " & _
MonthName(m) & " " & cstr(d) & ", " & cstr(y) & _
" " & byteToAscii2(h) & ":" & byteToAscii2(mm) & ":" & byteToAscii2(cbyte(sec))
end function
'''''''''''''''''''''''''''
' return a two digit ASCII version of n
private function byteToAscii2(byval n as byte) as STRING
byteToAscii2 = iif(n < 10, "0" & cstr(n), cstr(n))
end function
Conversion
Ok call me stupid, but I'll be damed if I can figure out how to convert the the tick numbers generated by Register.RTCTick into something usable.
I know that 512 ticks is a second and that 30720 is a minute, but getting it into something usable is something else.
So if I have something like
tick = Register.RTCtick
tick = tick / 512
if I simply divided it by itself I would get the seconds, but instead I get a message
64: Error: operator / requires two Single operands
Help!
Patrick
I know that 512 ticks is a second and that 30720 is a minute, but getting it into something usable is something else.
So if I have something like
tick = Register.RTCtick
tick = tick / 512
if I simply divided it by itself I would get the seconds, but instead I get a message
64: Error: operator / requires two Single operands
Help!
Patrick
Dave Houston showed you how to parse an RTC value in the first response of this thread. Pay attention. Your Helps! might soon be unheard.
> Error: operator / requires two Single operands
means what it says. Look up the difference between the two divide operators "/" and "\", in the ZBasic documentation.
> Error: operator / requires two Single operands
means what it says. Look up the difference between the two divide operators "/" and "\", in the ZBasic documentation.
Tom
Yep sure enough
Hey Tom,
You are correct I am not paying attention. Sorry for that.
Its just one of those days. It didn't even register that that is what he had done for me until I looked at it again.
Thanks
Patrick
You are correct I am not paying attention. Sorry for that.
Its just one of those days. It didn't even register that that is what he had done for me until I looked at it again.
Thanks
Patrick
In VB a simple way to add leading zeros is...
I think Don added Right to ZBasic recently.
Code: Select all
hhStr = "00" & CStr(HH)
hhStr = Right(hhStr,2)
I think Don added Right to ZBasic recently.
I did, along with Left(), but they're not in v1.2.7 that is generally available. I did post some code that implements the Left() and Right() functions that can be used in the interim.dhouston wrote:I think Don added Right to ZBasic recently.
- Don Kinzer