![]() ZBasic System Library
295
ZBasic Microcontrollers
Type
Subroutine
Invocation
Sleep(time)
Parameter
Method
Type
Description
time
ByVal
Single or int16
The amount of time to delay, in seconds
(Single) or ticks (int16)
Discussion
This routine suspends the current task for a period of time up to as long as specified. If the RTC is not
enabled in your application, the resolution of the delay period is 1mS. If the RTC is enabled, the
resolution is the same as an RTC tick period, i.e. 1/F_RTC_TICK (typically 1.95mS for ZX devices). The
actual sleep time experienced by the calling task depends on what other tasks actually do that may run in
the interim. It is possible that the task will be suspended indefinitely depending on what another task
might do.
Note that if the current task is locked, this call will unlock it.
There is a subtle difference between Delay() and Sleep() when the RTC is enabled and the arguments
are non-zero. For Delay() the specified time is the minimum amount of delay that the task will
experience assuming that no other task is ready to run and the actual delay could be up to 1 unit longer
than the specified delay. For Sleep(), the specified time is the maximum amount of delay that the task will
experience assuming that no other task is ready to run and the actual delay could be up to 1 unit less
than the specified delay.
It is important to note that internally, the sleep function utilizes an integral tick value. If the supplied
parameter is a Single value it is converted to the equivalent integral number of ticks (if the value is a
known constant at compile time, otherwise at run time). Consequently, if a Single value is specified that
is less than the equivalent of one tick the sleep time will be zero ticks.
Example
Do
Call PutPin(Pin.RedLED, 0)
Call Sleep(0.5)
' a half-second delay
Call PutPin(Pin.RedLED, 1)
Call Sleep(256)
' a half-second delay
Loop
This loop causes the red LED to turn on and off alternately for a half second each.
Compatibility
The BasicX documentation specifically indicates that Sleep() will unlock a locked task. However, tests
indicate that this only happens if the parameter to Sleep() is non-zero. This implementation unlocks a
task on any Sleep() call.
See Also
Delay, DelayUntilClockTick, Pause, WaitForInterval, Register.RTCStopWatch
|