You are on page 1of 46

TimersandCounters

Many microcontroller applications require the


counting of external events, such as frequency of a
pulse train, or the generation of precise internal time
delays between actions.
Both of these tasks can be accomplished using
software techniques.
The 8051 has two 16-bit registers that can be used
as either timers or counters.
Each counter may be programmed to count internal
clock pulses, act as a timer, or programmed to count
external events as a counter.
1

Timers /Counters

The 8051 has 2 timers/counters:

Timer/Counter 0
Timer/Counter 1

They can be used as


1. The Timer :used as a time delay generator.
The clock source is the internal crystal
frequency of the 8051.

2. An event counter.
External input from input pin to count the
number of events on registers.
These clock pulses represent the number of
people passing through an entrance, or the
number of wheel rotations, or any other event
that can be converted to pulses.

TimersandCounters
Both Timer 0 and Timer 1 are 16 bits wide
Since 8051 has an 8-bit architecture, each 16-bits
timer is accessed as two separate registers of low
byte and high byte.
Accessed as low byte and high byte
The low byte register is called TL0/TL1 And
The high byte register is called TH0/TH1
Accessed like any other register

Contd.

Contd.

8051 timers use 1/12 of XTAL


frequency as the input of timers.
Because the input of timer is a regular,
fixed-periodic square wave, we can
count the number of pulses and
calculate the time delay.
XTAL
oscillator

8051

12

Timer
P1

to
LCD

TH0
Set
Timer 0

TL0
5

Timer SFRs
As mentioned before, the 8051 has two
timers which each function essentially the same
way. One timer is TIMER0 and the other is
TIMER1.
The two timers share two SFRs (TMOD
and TCON) which control the timers, and each
timer also has two SFRs dedicated solely to itself
(TH0/TL0 and TH1/TL1).
Weve given SFRs names to make it easier to refer to
them, but in reality an SFR has a numeric address.
It is often useful to know the numeric address that
corresponds to an SFR name.
6

Contd.
The SFRs relating to timers are:

Timer SFRs
When you enter the name of an SFR into
an assembler, it internally converts it to a number.
For example, the command:
MOV TH0,#25h
moves the value 25h into the TH0 SFR.
However, since TH0 is the same as SFR address
8Ch this command is equivalent to:
MOV 8Ch,#25h
8

The TMOD(Timer Mode)SFR


The TMOD SFR is used to control the mode of
operation of both timers.
Each bit of the SFR gives the microcontroller
specific information concerning how to run a timer.
TMOD is a 8-bit register
The lower 4 bits are for Timer 0
The upper 4 bits are for Timer 1
In each case,
The lower 2 bits are used to set the timer mode
The upper 2 bits to specify the operation
9

Contd.

10

Gate
Every timer has a mean of starting and stopping.
GATE=0
Internal control
The start and stop of the timer are controlled
by way of software.
Set/clear the TR for start/stop timer.
SETB TR0
CLR
TR0
GATE=1
External control
The hardware way of starting and stopping the
timer by software and an external source.
Timer/counter is enabled only while the INT pin
is high and the TR control pin is set (TR).

Contd.

12

13-bit Time Mode (mode 0)


Timer mode "0" is a 13-bit timer.
This is a relic that was kept around in the 8051 to
maintain compatibility with its predecessor.
When the timer is in 13-bit mode, TLx will
count from 0 to 31.
When TLx is incremented from 31, it will "reset" to 0 and
increment THx.
Thus, effectively, only 13 bits of the two timer bytes are
being used:
bits 0-4 of TLx and bits 0-7 of THx.
This timer can only contain 8192 values.
13

16-bit Time Mode (mode 1)


Timer mode "1" is a 16-bit timer.
This is a very commonly used mode.
It functions just like 13-bit mode except that all 16
bits are used.
TLx is incremented from 0 to 255.
When TLx is incremented from 255, it resets to 0
and causes THx to be incremented by 1.
Since this is a full 16-bit timer, the timer may
contain up to 65536 distinct values.
14

Contd.

15

8-bit Time Mode (mode 2)


Timer mode "2" is an 8-bit auto-reload mode.
What is that, you may ask? Simple. When a
timer is in mode 2, THx holds the "reload value"
and TLx is the timer itself.
Thus, TLx starts counting up.
When TLx reaches 255 and is subsequently
incremented, instead of resetting to 0 (as in the case of
modes 0 and 1), it will be reset to the value stored in
THx.

16

Contd. 8-bit Time Mode (mode 2)


Whats the benefit of auto-reload mode?
Perhaps you want the timer to always have a
value from 200 to 255.
If you use mode 0 or 1, youd have to check in
code to see if the timer had overflowed and, if so,
reset the timer to 200.
This takes precious instructions of execution time
to check the value and/or to reload it.
When you use mode 2 the microcontroller takes
care of this for you.
17

Contd.

18

Contd.

19

Contd.

20

Split Timer Mode (mode 3)


Timer mode "3" is a split-timer mode.
When Timer 0 is placed in mode 3, it essentially
becomes two separate 8-bit timers.
That is to say, Timer 0 is TL0 and Timer 1 is TH0.
Both timers count from 0 to 255 and overflow
back to 0.
All the bits that are related to Timer 1 will now be
tied to TH0.

21

Contd. Split Timer Mode (mode 3)


While Timer 0 is in split mode, the real
Timer 1 (i.e. TH1 and TL1) can be put into modes 0,
1 or 2 normally--however, you may not start or stop
the real timer 1 since the bits that do that are now
linked to TH0.
The real timer 1, in this case, will be incremented
every machine cycle no matter what.

22

Example:
1. Indicate which mode and which timer are
selected for the following.
(a) MOV TMOD, #01H
Solution: We convert the value from hex to binary.
We have:
(a) TMOD = 00000001, mode 1 of timer 0 is
selected.

23

Exercise
1. Indicate which mode and which timer are
selected for each of the following.
(a) MOV TMOD, #20H
(b) MOV TMOD, #12H

24

The TCON(timer control) SFR


Finally, theres one more SFR that controls the two timers and
provides valuable information about them.
TCON (timer control) register is an 8-bit register.
which is bit addressable and in which Upper nibble
is for timer/counter, lower nibble is for interrupts

25

Contd.

26

Tcon contd
TR (Timer run control bit)
TR0 for Timer/counter 0; TR1 for Timer/counter 1.
TR is set by programmer to turn timer/counter on/off.
TR=0 : off (stop)
TR=1 : on (start)
TF (timer flag, control flag)
TF0 for timer/counter 0; TF1 for
timer/counter 1.
TF is like a carry. Originally, TF=0.
When TH-TL roll over to 0000 from
FFFFH, the TF is set to 1.
TF=0 : not reach
TF=1: reach

Contd.
As you may notice, weve only defined 4 of
the 8 bits.
Thats because the other 4 bits of the
SFR dont have anything to do with timers--they
have to do with Interrupts .
A new piece of information in this chart is
the column "bit address." This is because this SFR
is "bit-addressable."

28

Contd.
What does this mean? It means if you want to set the bit
TF1--which is the highest bit of TCON--you could execute
the command:
MOV TCON, #80h
or, since the SFR is bit-addressable, you could just execute
the command:
SETB TF1
This has the benefit of setting the high bit of TCON
without changing the value of any of the other bits of the
SFR.
Usually when you start or stop a timer you dont want to
modify the other values in TCON, so you take advantage
of the fact that the SFR is bit-addressable.
29

Contd.

30

Initializing a Timer
As youll recall, we first must decide what mode
we want the timer to be in.
In this case we want a 16-bit timer that runs
continuously; that is to say, it is not dependent on
any external pins.
We must first initialize the TMOD SFR.
Since we are working with timer 0 we will be
using the lowest 4 bits of TMOD. The first two
bits, GATE0 and C/T0 are both 0 since we want the
timer to be independent of the external pins.
31

Initializing a Timer
16-bit mode is timer mode 1 so we must clear
T0M1 and set T0M0.
Effectively, the only bit we want to turn on is bit
0 of TMOD.
Thus to initialize the timer we execute the
instruction:
MOV TMOD,#01h
Timer 0 is now in 16-bit timer mode.

32

8051- SERIAL COMMUNICATION


Computers transfer data in two ways:
Parallel
Often 8 or more lines (wire conductors) are
used to transfer data to a device that is only a
few feet away
Serial
To transfer to a device located many meters
away, the serial method is used
The data is sent one bit at a time.
33

Contd.

34

Types of Serial communications


.

8051- SERIAL COMMUNICATION


One of the 8051s many powerful features
is its integrated UART, otherwise known as a
serial port.
The fact that the 8051 has an integrated serial port
means that you may very easily read and write values
to the serial port.
If it were not for the integrated serial port, writing a
byte to a serial line would be a rather tedious process
requiring turning on and off one of the I/O lines in
rapid succession to properly "clock out each
individual bit, including start bits, stop bits, and parity
bits.
36

RxD and TxD pins in the


The 8051 has two
pins for transferring
8051

and receiving data by serial


communication. These two pins are
part of the Port3(P3.0 &P3.1)
These pins are TTL compatible and
hence they require a line driver to
make them RS232 compatible
Max232 chip is one such line driver in
use.
Serial communication is controlled by
an 8-bit register called SCON register,it

8051 Interrupts
An interrupt is an external or internal event
that disturbs the microcontroller to inform it
that a device needs its service.

A Microcontroller can serve various devices.


There are two ways to do that:
interrupts &
Polling-is the process where the computer or
controlling device waits for an external device to
check for its readiness.

The program which is associated with the


interrupt is called the interrupt service
routine (ISR) or interrupt handler.

Steps in executing an interrupt


Upon receiving the interrupt signal the
Microcontroller , finish current instruction
and saves the PC on stack.
Jumps to a fixed location in memory
depending on type of interrupt
Starts to execute the interrupt service
routine until RETI (return from interrupt)
Upon executing the RETI the microcontroller
returns to the place where it was
interrupted. Get pop PC from stack

Interrupt Sources
Original 8051 has 6 sources of
interrupts
Reset
Timer 0 overflow
Timer 1 overflow
External Interrupt 0
External Interrupt 1
Serial Port events (buffer full, buffer
empty, etc)

Interrupt Enable Register

Upon reset all Interrupts are


disabled & do not respond to the
Microcontroller
These interrupts must be enabled
by software in order for the
Microcontroller to respond to them.
This is done by an 8-bit register
called Interrupt Enable Register
(IE).

EA : Global
enable/disable.
--: Undefined.
ET2 : Enable Timer 2
interrupt.
ES : Enable Serial port
interrupt.
ET1 : Enable Timer 1
interrupt.
EX1 :Enable External 1

Enabling and disabling an


interrupt

By bit operation

Recommended in the middle of program


SETB
SETB
SETB
SETB
SETB
SETB

EA
ET0
ET1
EX0
EX1
ES

SETB
SETB
SETB
SETB
SETB

IE.7
IE.1
IE.3
IE.0
IE.2

SETB

IE.4

;Enable
;Enable
;Enable
;Enable
;Enable
;Enable

All
Timer0 ovrf
Timer1 ovrf
INT0
INT1
Serial port

By Mov instruction
Recommended in the first of program
MOV IE, #10010110B

Interrupt Priorities
What if two interrupt sources interrupt at
the same time?
The interrupt with the highest PRIORITY
gets serviced first.
All interrupts have a power on default
priority order.
1. External interrupt 0 (INT0)
2. Timer interrupt0 (TF0)
3. External interrupt 1 (INT1)
4. Timer interrupt1 (TF1)
5. Serial communication (RI+TI)

Priority can also be set to high or low


by IP reg.

Interrupt Priorities (IP) Register


---

---

PT2

PS

PT1

PX1

PT0

IP.7: reserved
IP.6: reserved
IP.5: Timer 2 interrupt priority bit (8052 only)
IP.4: Serial port interrupt priority bit
IP.3: Timer 1 interrupt priority bit
IP.2: External interrupt 1 priority bit
IP.1: Timer 0 interrupt priority bit
IP.0: External interrupt 0 priority bit

PX0

Interrupt Priorities Example


---

---

PS

MOV IP , #00000100B
priority order
1.
2.
3.
4.
5.

PT2

PT1

PX1

PT0

or SETB IP.2 gives

Int1
Int0
Timer0
Timer1
Serial

MOV IP , #00001100B gives priority order


1.
2.
3.
4.
5.

Int1
Timer1
Int0
Timer0
Serial

PX0

You might also like