26 of 323
ZBasic System Library 16 ZX Microcontroller Family specific details). If the flag is False, the routine sets it to True and then goes about using the timer. When it has finished its function, it sets the flag back to False. Your code can use the timer busy flag as the 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 you’ll know that the timer is not already in use. An example of code for this purpose (for ZX 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 System Library routines that require that timer from functioning. Processor Speed Issues Although the standard clock speed for ATmega-based ZX devices is generally 14.7456MHz, special versions are available that run slower and faster. ZX devices based on the ATxmega run at 29.4912MHz. The table below summarizes the differences that arise due to the difference in operating speeds. Processor Speed Variations Issue 7.3728MHz 14.7456MHz 18.432MHz 29.4912MHz RTC Tick Frequency 512 Hz 512 Hz 500 Hz 512 Hz RTC Fast Tick Frequency 512 Hz 1024 Hz 1 KHz 1024 Hz RTC Timer Frequency 115.2 KHz 230.4 KHz 72 KHz 230.4 KHz Multi-tasking Time Slice 1.95 mS 1.95 mS 2.0 mS 1.95 mS Default TimerSpeed1 Units 135.6 nS 67.8 nS 54.4 nS 67.8 nS Default TimerSpeed2 Units 1.085 µS 1.085 µS* 434 nS 271 nS CountTransitions() Sample Rate 204.8 KHz 409.6 KHz 512 KHz 737.3 KHz Note, particularly, that the value for TimerSpeed2 at 14.7456MHz is scaled to match the value corresponding to operating at 7.3728MHz. This is done for compatibility with BasicX devices that operate at the latter speed. Consult the preceding sections for information on which routines use the TimerSpeed1 and TimerSpeed2 values. It is highly recommended to use the built-in values Register.CPUFrequency, Register.RTCTickFrequency, and Register.RTCTimerFrequency instead of using hard-coded values. Doing so also simplifies code that must run on multiple devices that operate at different speeds. See the descriptions in the ZBasic Language Reference Manual for more details. Detailed Descriptions