A simple timer

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
pcleats
Posts: 35
Joined: 12 December 2005, 11:57 AM

A simple timer

Post by pcleats »

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
DH*

Post by DH* »

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.
GTBecker
Posts: 616
Joined: 17 January 2006, 19:59 PM
Location: Cape Coral

Post by GTBecker »

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
Tom
pcleats
Posts: 35
Joined: 12 December 2005, 11:57 AM

Format to look like a stopwatch

Post by pcleats »

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
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

The easiest way to accomplish your objective, assuming that you have the values in variables name h, m and s is:

Code: Select all

Dim timeStr as String
timeStr = CStr(h) & ":" & CStr(,) & ":" & CStr(s)
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:

Code: Select all

Dim timeStr as String
timeStr = ""
If &#40;h < 10&#41; Then
  timeStr = timeStr & "0"
End If
timeStr = timeStr & CStr&#40;h&#41; & "&#58;"
If &#40;m < 10&#41; Then
  timeStr = timeStr & "0"
End If
timeStr = timeStr & CStr&#40;m&#41; & "&#58;"
If &#40;s < 10&#41; Then
  timeStr = timeStr & "0"
End If
timeStr = timeStr & CStr&#40;s&#41;
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

Sub Main&#40;&#41;
	Dim h as Byte, m as Byte, s as Byte
	Dim str as String

	str = ""
	Call addTimeElement&#40;h, str, ""&#41;
	Call addTimeElement&#40;m, str, "&#58;"&#41;
	Call addTimeElement&#40;s, str, "&#58;"&#41;
	Debug.Print str
End Sub

Sub addTimeElement&#40;ByVal val as Byte, ByRef timeStr as String, ByVal prefix as String&#41;
	If &#40;Len&#40;prefix&#41; > 0&#41; Then
		timeStr = timeStr & prefix
	End If
	If &#40;val < 10&#41; Then
		timeStr = timeStr & "0"
	ElseIf &#40;val > 59&#41; Then
		val = 59
	End If
	timeStr = timeStr & CStr&#40;val&#41;
End Sub
- Don Kinzer
stevech
Posts: 715
Joined: 22 February 2006, 20:56 PM

Post by stevech »

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.

Code: Select all

' return a string containing the date and time as passed, formatted a certain way...
public function formatDateTime1&#40; _
		byval y as integer, m as byte, d as byte, _
		byval h as byte, byval mm as byte, byval sec as single _
	&#41;as STRING

	formatDateTime1 = _ 
	  DayName&#40;DayOfWeek&#40;y, m, d&#41;&#41; & " " & _
	  MonthName&#40;m&#41; & " " & cstr&#40;d&#41; & ", " & cstr&#40;y&#41; & _
	  " " & byteToAscii2&#40;h&#41; & "&#58;" & byteToAscii2&#40;mm&#41; & "&#58;" & byteToAscii2&#40;cbyte&#40;sec&#41;&#41;

end function



'''''''''''''''''''''''''''
' return a two digit ASCII version of n
private function byteToAscii2&#40;byval n as byte&#41; as STRING
	byteToAscii2 = iif&#40;n < 10, "0" & cstr&#40;n&#41;, cstr&#40;n&#41;&#41;
end function
pcleats
Posts: 35
Joined: 12 December 2005, 11:57 AM

Conversion

Post by pcleats »

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
GTBecker
Posts: 616
Joined: 17 January 2006, 19:59 PM
Location: Cape Coral

Post by GTBecker »

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.
Tom
pcleats
Posts: 35
Joined: 12 December 2005, 11:57 AM

Yep sure enough

Post by pcleats »

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
DH*

Post by DH* »

In VB a simple way to add leading zeros is...

Code: Select all

hhStr = "00" & CStr&#40;HH&#41;
hhStr = Right&#40;hhStr,2&#41;

I think Don added Right to ZBasic recently.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

dhouston wrote: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.
- Don Kinzer
Post Reply