TIMERS & Counters

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

9.

Timers & Counters


The microcontroller oscillator uses quartz crystal for its operation. Even though
it is not the simplest solution, there are many reasons to use it. The frequency of
such oscillator is precisely defined and very stable, so that pulses it generates
are always of the same width, which makes them ideal for time measurement.
Such oscillators are also used in quartz watches. If it is necessary to measure
time between two events, it is sufficient to count up pulses generated by this
oscillator. This is exactly what the timer does.

Most programs use these miniature electronic ‘stopwatches'. These are


commonly 8- or 16-bit SFRs the contents of which is automatically incremented
by each coming pulse. Once a register is completely loaded, an interrupt may be
generated!

If the timer uses an internal quartz oscillator for its operation then it can be used
to measure time between two events (if the register value is T1 at the moment
measurement starts, and T2 at the moment it terminates, then the elapsed time is
equal to the result of subtraction T2-T1). If registers use pulses coming from
external source then such a timer is turned into a counter.

This is only a simple explanation of the operation itself. It is however more


complicated in practice. 

HOW DOES THE TIMER OPERATE?


In practice, pulses generated by the quartz oscillator are once per each machine
cycle, directly or via a prescaler, brought to the circuit which increments the
number stored in the timer register. If one instruction (one machine cycle) lasts
for four quartz oscillator periods then this number will be incremented a million
times per second (each microsecond) by embedding quartz with the frequency
of 4MHz.
It is easy to measure short time intervals, up to 256 microseconds, in the way
described above because it is the largest number that one register can store. This
restriction may be easily overcome in several ways such as by using a slower
oscillator, registers with more bits, prescaler or interrupts. The first two
solutions have some weaknesses so it is more recommended to use prescalers or
interrupts.

USING A PRESCALER IN TIMER OPERATION


A prescaler is an electronic device used to reduce frequency by a predetermined
factor. In order to generate one pulse on its output, it is necessary to bring 1, 2 ,
4 or more pulses on its input. Most microcontrollers have one or more
prescalers built in and their division rate may be changed from within the
program. The prescaler is used when it is necessary to measure longer periods
of time. If one prescaler is shared by timer and watchdog timer, it cannot be
used by both of them simultaneously.

USING INTERRUPT IN TIMER OPERATION


If the timer register consists of 8 bits, the largest number it can store is 255. As
for 16-bit registers it is the number 65.535. If this number is exceeded, the timer
will be automatically reset and counting will start at zero again. This condition
is called an overflow. If enabled from within the program, the overflow can
cause an interrupt, which gives completely new possibilities. For example, the
state of registers used for counting seconds, minutes or days can be changed in
an interrupt routine. The whole process (except for interrupt routine) is
automatically performed behind the scenes, which enables the main circuits of
the microcontroller to operate normally.

This figure illustrates the use of an interrupt in timer operation. Delays of


arbitrary duration, having almost no influence on the main program execution,
can be easily obtained by assigning the prescaler to the timer.
COUNTERS
If the timer receives pulses from the microcontroller input pin, then it turns into
a counter. Obviously, it is the same electronic circuit able to operate in two
different modes. The only difference is that in this case pulses to be counted
come over the microcontroller input pin and their duration (width) is mostly
undefined. This is why they cannot be used for time measurement, but for other
purposes such as counting products on an assembly line, number of axis
rotation, passengers etc. (depending on sensor in use).

WATCHDOG TIMER
A watchdog timer is a timer connected to a completely separate RC oscillator
within the microcontroller.

If the watchdog timer is enabled, every time it counts up to the maximum value,
the microcontroller reset occurs and the program execution starts from the first
instruction. The point is to prevent this from happening by using a specific
command.

Anyway, the whole idea is based on the fact that every program is executed in
several longer or shorter loops. If instructions which reset the watchdog timer
are set at the appropriate program locations, besides commands being regularly
executed, then the operation of the watchdog timer will not affect the program
execution. If for any reason, usually electrical noise in industry, the program
counter ‘gets stuck' at some memory location from which there is no return, the
watchdog timer will not be cleared, so the register's value being constantly
incremented will reach the maximum et voila! Reset occurs! 

A/D CONVERTER
External signals are usually fundamentally different from those the
microcontroller understands (ones and zeros) and have to be converted therefore
into values understandable for the microcontroller. An analogue to digital
converter is an electronic circuit which converts continuous signals to discrete
digital numbers. In other words, this circuit converts an analogue value into a
binary number and passes it to the CPU for further processing. This module is
therefore used for input pin voltage measurement (analogue value).

You might also like