![]() ZBasic System Library
13
ZBasic Microcontrollers
(ATtiny, ATmega) and TimerC1 (ATxmega), respectively, are in use. Prior to using a timer, the ZBasic
System Library code checks the value of this variable to see if it is already being used. If it is not in use,
the system sets the flag to True and then proceeds to use the timer. When it is finished using the timer,
the system sets the busy flag to False.
Your appplication may do the same by passing the Register variable as a parameter to the Semaphore()
function in order to get exclusive access to the timer. Of course, you must set timer busy flag to False
when your code is finished with the timer to indicate that the timer is no longer in use. Likewise, you may
want to acquire a semaphore on a timer busy flag for the I/O Timer before calling a System Library routine
that uses I/O Timer. If you succeed in setting the semaphore youll know that the timer is not already in
use. An example of code for this purpose (for ZBasic devices that use Timer1 for the I/O Timer) is shown
below.
' wait until the timer is available
Do While (Not Semaphore(Register.Timer1Busy))
Call Sleep(0.5)
Loop
' use the timer
Call LockTask()
Register.Timer1Busy = False
Call ShiftOut(12, 13, 8, &H55)
Call UnlockTask()
Note, particularly, the line immediately before the call to ShiftOut(). After the semaphore is acquired
Regsister.Timer1Busy will be True. Unless it is set to False, the call to ShiftOut() will fail
because that subroutine will think that the timer is in use.
Caution: setting the busy flag for a timer to True and never setting it back to False will prevent the
proper functioning of all System Library routines that require that timer.
Some of the System Library routines that use the I/O Timer allow you to modify the frequency used to
clock the timer while others use a fixed frequency determined by the requirements of the routine. The
routines that do allow frequency modification are divided into two groups, one controlled by the value of
Register.TimerSpeed1 and the other controlled by the value of Register.TimerSpeed2. The
table below shows the System Library routines that use a timer and, where applicable, the timer speed
variable that controls the timer frequency.
System Library Routines Using TimerSpeed Values
Routine
TimerSpeed Value
ADCtoCom1()
Com1toDAC()
CountTransitions()
TimerSpeed1
FreqOut()
Get1Wire()
Get1WireByte()
Get1WireData()
I2CCmd()²
TimerSpeed1
I2CGetByte()²
TimerSpeed1
I2CPutByte()²
TimerSpeed1
InputCapture()
TimerSpeed1
InputCaptureEx()
TimerSpeed1
OutputCapture()
TimerSpeed1
OutputCaptureEx()
TimerSpeed1
OpenPWM()
OpenPWM8()
|