ZBasic Language Reference
60
ZX Microcontroller Family
3.7 Timers
The ZX series CPUs incorporate several timers, the actual number depending on the underlying CPU.
On all ZX models, Timer0 and Timer2 are 8-bit timers while the remaining timers are 16-bit. The table
below summarizes timer usage.
Timer Usage by CPU Type
Underlying CPU
RTC
I/O
Serial
PWM 16-bit
InputCapture
OutputCapture
mega32
Timer0
Timer1
Timer2
Timer1
Timer1
Timer1
mega644
Timer0
Timer1
Timer2
Timer1
Timer1
Timer1
mega328P
Timer0
Timer1
Timer2
Timer1
Timer1
Timer1
mega128
Timer0
Timer1
Timer2
Timer1/3
Timer1/3
Timer1/3
mega1281
Timer2
Timer4
Timer0
Timer1/3
Timer1/3
Timer1/3
mega1280
Timer2
Timer4
Timer0
Timer1/3/4/5
Timer1/3/4/5
Timer1/3/4/5
The RTC Timer is used to generate interrupts to manage the real time clock (RTC). The I/O Timer is
used for several I/O functions that require accurate timing. The Serial Timer is used to implement the
software UART for the serial channels Com3 to Com6 and, alternately, 8-bit PWM generation. When a
specific timer is not being used by the system, you are free to use that timer in your program as you wish.
To do so, youll have to read the Atmel datasheet for the ATmega32, ATmega644, ATmega128,
ATmega1280 or ATmega1281 microcontroller to determine how to program the timers and then use the
built-in registers to read and write the timer registers. Further discussion of that topic is beyond the scope
of this document.
3.8 Built-in Variables
The set of pre-defined registers comprises two sub-groups: actual CPU registers and control program
variables. All of these built-in variables must be referenced using the Register prefix or within a With
Register compound statement.
Example
Dim tick as Long
tick = Register.RTCTick
In addition to the particular registers available for each underlying processor, three special register
variables are provided that correspond the PORT, PIN and DDR registers associated with a particular I/O
pin. The form of these three special register functions is: Register.Port(<pin>),
Register.Pin(<pin>) and Register.DDR(<pin>), respectively. In all three cases, the property
.DataAddress may be appended to get the address of the register associated with the specified pin.
Example
val = Register.Port(C.2)
' reads Register.PortC
Register.Port(C.2) = val
' writes to Register.PortC
addr = Register.Pin(A.4).DataAddress
' address of Register.PortA
These three special register variables will often be used in conjunction with the ZBasic System Library
function PortMask() which returns the bit mask for an I/O port corresponding to a specified pin.
Example
Const pin as Byte = C.7
Call SetBits(Register.DDR(pin), PortMask(pin), &Hff)
' make a pin an output