You are on page 1of 4

9.

Timers & Counters


Microcontroller is composed of various digital components. For the proper operation of
these digital logics clock signal is needed. The clock signal is required to establish a
communication and synchronization between various parts of the microcontroller. The clock
signal is generated by oscillator circuit. A basic oscillator provides a high frequency signal
without any input signal. There are many types of oscillators. Normally, Quartz crystal is
preferred for its operation. The Quartz crystal oscillator has the ability to generate a constant
frequency signal under varying load and is very stable. The output pulse generated from this
oscillator will be of equal width (repetitive pulse) which makes them suitable for time
measurement. Such oscillators are also used in quartz watches. If it is necessary to measure time
duration for any event, it is sufficient to count up pulses generated by this oscillator. This is
exactly what the timer does.

Timers are normally used for two purposes. Counter may be programmed to count internal clock
pulses, acting as a timer, or programmed to count external pulses as a counter. There are
commonly 8- or 16-bit SFRs, (Special Function Register) to represent the timer status. The
TMOD and TCON are the two registers used for that purpose.

Timer counter interrupt is generated when the contents of which are automatically incremented
by each coming pulse. Once a register is completely loaded, an interrupt may be generated.

HOW DOES THE TIMER OPERATE?

One of the foremost works of the timer is to measure the time. When the timer is used for this
purpose then it is called as internal timer since it is measuring the time interval between the two
events say T1 and T2. If the register value is T1 at the moment measurement starts, and T2 at the
moment it ends, then

The elapsed time = T2-T1

The timer register is reset to the initial condition. The timer register is incremented by one for
every clock cycle. In practice, pulses generated by the quartz oscillator are once per each
machine cycle, directly or via a prescaler. For example, if the frequency of the crystal is 12 MHz,
then the frequency for Timer will be 1MHz (1/12 of crystal frequency) and hence the time (T =
1/f) taken by the Timer to count by one is 1µs (1/1MHz).

USING A PRESCALER IN TIMER OPERATION

A prescaler is an electronic device used to reduce frequency by a predetermined factor. The high
frequency signal is taken as input and is converted into low frequency by using frequency
division. A prescaler takes the basic timer clock frequency and divides it before feeding it to the
timer. The prescaler value is a integer that depends on 1-2P, where P is the number of scaler bits.
Most microcontrollers have one or more prescalers built in and their division rate may be
changed from within the program. The prescalers are normally a integer say 2, 4, 8, etc. 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

Whenever a normal program is interrupted by some event then that event is said to be interrupt.
The interrupt will be triggered a specified event occurs. If the timer register consists of 8 bits, the
largest number it can store is (28-1)255. As for 16-bit registers it is (2 16-1) 65,535.The register
can count up till this value, if this number is exceeded, the timer will be automatically reset and
the counter go to initial zero condition. This condition is called an overflow.

When an interrupt is triggered, the following actions are taken automatically by the
microcontroller:

1. The current Program Counter (PC) is saved on the stack, low-byte first.
2. Interrupts of the same and lower priority are blocked.
3. In the case of Timer and External interrupts, the corresponding interrupt flag is cleared.
4. Program execution transfers to the corresponding interrupt handler vector address.
5. The Interrupt Handler Routine executes.

For example, the state of registers used for counting seconds, minutes or days can be changed in
an interrupt routine. Once the process is over the interrupt flag is cleared.
COUNTERS

The timer can also perform as a counter. If the pulse is from internal clock then it works as timer.
If the timer receives pulses from the microcontroller input pin, then it turns into a counter. 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 timing device such that it is set for a preset time interval and an event must occur during that
interval else the device will generate the timeout signal on failure to get that event in the watched
time interval. On that event, the watchdog timer is disabled to disable generation of timeout or
reset. Assume that we anticipate that a set of tasks must finish in 100 ms interval. The watchdog
timer is disabled and stopped by the program instruction in case the tasks finish within 100 ms
interval. In case task does not finish (not disabled by the program instruction), watchdog timer
generates interrupts after 100 ms and executes a routine, which is programmed to run because
there is failure of finishing the task in anticipated interval. Timeout may result in processor start
a service routine or start from beginning. A watchdog timer is a timer connected to a completely

separate RC oscillator within the microcontroller. An application in mobile phone is that display

is off in case no GUI interaction takes place within a watched time interval. The interval is
usually set at 15 s, 20 s, 25 s, 30 s in mobile phone. This saves power.

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.

A/D CONVERTER

Microcontroller can understand only the digital values (0 and 1). External signals are usually
fundamentally different from those the microcontroller understands and have to be converted. An
analog to digital converter (A/D converter) is an electronic circuit which converts continuous
signals to discrete digital numbers. In other words, this circuit converts an analog value into a
binary number and passes it to the CPU for further processing. This makes us to interface all of
the analog devices to perform our tasks. Common ADCs are 8 bit, 10 bit and 12 bit. This module
is therefore used for input pin voltage measurement (analog value).

You might also like