System Timer

Top  Previous  Next

The system object provides a timer event- on_sys_timer- that is generated periodically. The period depends on the platform. If the platform doesn't have a sys.onsystimerperiod property, then this period is fixed at 0.5 seconds. If the platform supports this property, then 0.5 second is the default period and the property allows your program to adjust it.

If your software has some periodic tasks you can put the code into the on_sys_timer event handler. Notice that when generated, this event goes into the queue and waits in line to be processed, just like all other events. Therefore, you cannot expect great accuracy in the period at which the on_sys_timer event handler is entered. You can just expect that on average you will be getting this event every 0.5 seconds.

There is also a sys.timercount read-only property. Timer counter is a free-running counter that is initialized to 0 when your device is powered up and increments every 0.5 seconds. The sys.timercount is useful in determining elapsed time, for instance, when you are waiting for something.

Here is an example. Supposing, you are supposed to wait for the serial data to arrive, but you are not willing to wait more than 10 seconds:        

 

 

dim w as word

...

...

w=sys.timecount 'memorize time count at the beginning of waiting for serial data

while ser.rxlen=0

 'nope, no data yet, do we still have time?

 if sys.timercount-w>20 then goto enough_waiting 'we quit after 10 seconds

 doevents 'polite waiting includes this

wend

 

...

...

 

 

note_tip-wt

The above example does not represent best coding style. Generally speaking, this is not a great programming but sometimes you just have to wait in a cycle.