25 of 323
ZBasic System Library 15 ZX Microcontroller Family 2) The timer is used only for channels 1-4. The table below shows the correspondence between the allowable values for the TimerSpeed registers and the resulting clock frequency applied to the I/O Timer in terms of the CPU frequency. The divisor specified is applied to the CPU clock frequency to yield the I/O Timer clock frequency. For compatibility with BasicX (but only for ZX processors running at 14.7456MHz), some of the routines effectively divide the timer frequency by 2 so that the time units associated with parameters or return values is preserved. If you change the timer speed setting, the scale factor is still applied. TimerSpeed Selector Values TimerSpeed Value ATmega ATxmega 0 Off Off 1 F_CPU / 1 F_CPU / 1 2 F_CPU / 8 F_CPU / 2 3 F_CPU / 64 F_CPU / 4 4 F_CPU / 256 F_CPU / 8 5 F_CPU / 1024 F_CPU / 64 6 External T1 F_CPU / 256 7 External T2 F_CPU / 1024 8-15 n/a Event 0-7 The default values of Register.TimerSpeed1 and Register.TimerSpeed2 are shown in the table below. Default TimerSpeed Values CPU Family TimerSpeed1 TimerSpeed2 ATmega 1 2 ATxmega 2 4 Note that setting the value of either of the timer speed registers other than by direct assignment using an assignment statement will produce undefined results. Also note that on the ZX-24 series devices, the T1 input signal is common with Port C, bit 7. If you wish to use an external clock source you’ll have to configure pin 5 to be an input so as not to interfere with that signal. Of course, transitions on Port C bit 7 can be used to clock the timer when the T1 input signal is selected. There are several important facts to keep in mind if you modify either of the timer speed values. Firstly, the timer speed values are initialized by the system when it begins running and they are never modified by the system thereafter. If you change a timer speed value, that value will be used by all of the related System Library routines until you change it again. Secondly, the applicable TimerSpeed value is used during the configuration and setup of each I/O function. If you change the TimerSpeed value after a particular I/O function is configured, the change will not affect I/O functions configured before that change. Note, also, that values returned by some of the System Library routines are scaled based on the default timer speed values. If you change the timer speed setting, you’ll have to apply an additional scale factor in order to get the correct results. For example, if you set Register.TimerSpeed2 to 3 on an ATmega- based device running at 14.7MHz and then call the subroutine PulseIn(), a pulse having a width of 100µS will return the value of approximately 12.5µS since the clock speed that you specified is 1/8 that of the default. In order to get the correct pulse width, in seconds, you will have to multiply the value returned by 8. Those return values that are not scaled to seconds represent a number of periods of the timer frequency. So, for example, if you change Register.TimerSpeed1 to 2 on an ATmega-based device running at 14.7MHz, the values returned by InputCapture() represent units of 542nS instead of the default 67.8nS. The other Register value related to the I/O Timer is the “timer busy” flag, e.g. Register.Timer1Busy. Whenever a System Library routine that requires the I/O Timer prepares to execute, it first checks the value of this Boolean flag to see if the timer is already in use. If the flag is True, the routine will not execute; usually returning without doing anything (but see the descriptions of the various routines for