0% found this document useful (0 votes)
177 views

Unit-III 8051 Timers, Interrupts and Serial Communication: TH/TL (Timer High/ Timer Low)

The document discusses timers, interrupts, and serial communication in 8051 microcontrollers. It describes the two 16-bit timers, timer 0 and timer 1, and their associated registers - TH, TL, TMOD, and TCON. The four modes for timers - modes 0, 1, 2, and 3 - are explained in detail, along with how to generate delays using timers by calculating the timer start value based on the desired delay time. Interrupts and serial communication are also briefly mentioned.

Uploaded by

Rushikesh Nande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views

Unit-III 8051 Timers, Interrupts and Serial Communication: TH/TL (Timer High/ Timer Low)

The document discusses timers, interrupts, and serial communication in 8051 microcontrollers. It describes the two 16-bit timers, timer 0 and timer 1, and their associated registers - TH, TL, TMOD, and TCON. The four modes for timers - modes 0, 1, 2, and 3 - are explained in detail, along with how to generate delays using timers by calculating the timer start value based on the desired delay time. Interrupts and serial communication are also briefly mentioned.

Uploaded by

Rushikesh Nande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Unit-III

8051 Timers, Interrupts and Serial Communication

Introduction:

• There are many applications which require an accurate delay to be used.

• To generate accurate delay 8051 provides Timers/Counters.

• When used as timer, this section generates delay while when used as counter, it
counts the pulses applied on pins To and T1 of port 3.

• There are total two timers in 8051 of size 16 bit. i. e. timer 0 and timer 1 which are
independent of each other and may be used separately.

Timer/Counter Registers:

• The timer section itself has some registers which are:


TH/TL (Timer High/ Timer Low):
This pair forms the timer/counter. There are two such type of pairs for
timer 0 and timer 1 which are named as TH0 and TL0 for timer 0 and similarly
TH1 and TL1 for timer 1.
Each register is 8 bits so a pair forms a 16-bit timer. While used as timer,
these registers hold the starting point of timer whereas in counter, it contains the
final count.

Fig. Timer Registers


Above figure shows the two 16 bits timer registers Timer 0 and Timer 1 register
respectively. These are special function registers with addresses as follows:
THO= 8CH
TL0= 8AH
TH1= 8DH
TL1= 8BH

TMOD Register:
TMOD is timer mode register. As the name indicates, it is used to select the mode
of timer. It is an 8-bit register with address 89H, on reset it holds the value 00H. Both the
timer’s timer 0 and timer 1 use the same register.
The lower 4-bit are used for timer 0 and upper 4-bit are used for timer 1. In each
case the lower 2 bits are used to set the timer mode and upper 2 bits are to specify the
operation.

Fig. TMOD Register


M1, M0:
M0 and M1 are the bits used to select the timer mode. There are 3 modes available
in timer.
M1 M0 Mode Description
13-bit timer mode. (80-bit timer/counter THx with TLx as 5-bit
0 0 0
prescaler)
16-bit timer mode. 1-bit timer/counters THx and TLx are cascaded,
0 1 1
there is no prescaler.
8-bit auto reload. (8-bit auto reload timer/counter, THx holds a
1 0 2
value that is to be reloaded into TLx each time it overflows.
1 1 3 Split timer mode.

C/T (Counter/Timer):
This bit in the TMOD register is used to decide whether the timer is used as a delay
generator or an event counter. If C/T = 0, it is used as timer for time delay generation. The
clock source for the time delay is the crystal frequency of the 8051. If C/T = 1, it is used
as counter operation.

GATE:
Both the timers have this GATE bit. Every timer has a means of starting and
stopping. Some timers do by software, some by hardware, and some have both software
and hardware controls. The timers in 8051 have both. The start and stop of the timer is
controlled by the way of software by the TR (timer start) bits TR0 and TR1. Thus, TR bit
will start and stop the timer as long as GATE=0.
The hardware way of starting and stopping the timer by an external source is
achieved by making GATE=1 in TMOD register.

TCON Register:
TCON is an 8-bit addressable register used to control the activities of timers like
start, stop etc. The address of TCON is 88H.

TF1: Timer 1 flag (TCON.7)

This flag indicates the overflow of timer 1, when the value in timer 1 (TH1 and
TL1) rolls over from the maximum value (e. g. FFFFH in mode 1) to 0000H, this flag is set.

TR1: Timer 1 run control (TCON.6)

This flag is used to start or stop the timer 1. When made 1, it starts the timer
whereas 0 makes the timer 1 stop.

TF0: Timer 0 flag (TCON.5)

This flag indicates the overflow of timer 0, when the value in timer 0 (TH0
and TL0) rolls over from the maximum value (e. g. FFFFH in mode 1) to 0000H, this flag
is set.
TR0: Timer 0 run control (TCON.4)

This flag is used to start or stop the timer 0. When made 1, it starts the timer
whereas 0 makes the timer 0 stop.

IE1: External interrupt 1 edge flag (TCON.3)

It is set by hardware when external interrupt edge is detected (INT1). It is cleared


by hardware when interrupt is processed.

IT1: Interrupt 1 type control (TCON.2)

Is specifies the type of INT1 i. e. edge triggered or level triggered. If it is 0, interrupt


is level triggered and if it is 1, interrupt is edge triggered.

IE0: External interrupt 0 edge flag (TCON.1)

It is set by hardware when external interrupt edge is detected (INT0). It is cleared


by hardware when interrupt is processed.

IT0: Interrupt 0 type control (TCON.0)

Is specifies the type of INT0 i. e. edge triggered or level triggered. If it is 0, interrupt


is level triggered and if it is 1, interrupt is edge triggered.

Timer/Counter Modes:

Depending on application, the timers can be used in any of the four modes. These
modes are selected by TMOD register.

MODE 0 (13 Bit Timer):


As shown in the figure above, the clock frequency of 8051 is divided by 12 by
internal circuitry. The functions of C/T and GATE can be observed from above figure.
When working as counter, it takes the input from T0 instead of the oscillator.

When GATE is 1, the INT0 pin gets connected to timer to facilitate pulse width
measurement. (i. e. either GATE should be 0 or INT0 should be 1)

TR0 bit from TCON starts or stop the counting.

In mode 0 all 8 bits of TH and lower 5 bits of TL are used. Upper 3 bits of TL are
indeterminate and should be ignored. So, the total counts in mode 0 are 8192.

The 13-bit mode was introduced purposely in order to make the timer compatible
with MCS 48 family. This mode works similar for timer 1. Just the bits are changed like
TR1 instead of TR0 etc.

MODE 1 (16 Bit Timer):

The structure of mode 1 is similar to mode 0 except it uses all the 8- bits of TL
along with 8 bits of TH. So, the total count in mode 1is 65536.

As shown in the figure above, the clock frequency of 8051 is divided by 12 by


internal circuitry. The functions of C/T and GATE can be observed from above figure.
When working as counter, it takes the input from T0 instead of the oscillator.
When GATE is 1, the INT0 pin gets connected to timer to facilitate pulse width
measurement. (i. e. either GATE should be 0 or INT0 should be 1)

TR0 bit from TCON starts or stop the counting. The timer is started and stopped
in the same way of mode 0.

In mode 1 all 8 bits of TH and all the 8 bits of TL are used. So, the total counts in
mode 0 are 65535. This mode works similar for timer 1. Just the bits are changed like TR1
instead of TR0 etc.

MODE 2 (8 Bit Auto Reload):

The hardware configuration of mode 2 is shown in the figure above. As the name
implies, it uses only 8 bits of TL whereas TH holds the starting point of the timer.

When the timer rolls over from FFH to 00H, it not only sets the timer overflow flag
but also reloads the value from TH to TL. This mode is used for baud rate generation in
serial communication. Also, it is used for applications where delay is small and repeated,
e. g. Square wave generation.

MODE 3 (Split Timer):


The mode 3 splits the timer 0 in two parts of 8 bit each whereas timer 1 simply holds its
value. Here TL0 and TH0 are treated as separate 8-bit timers. From hardware
configuration, it can be observed that the control bits and flags of timer 0 i. e. C/T, GATE,
INT0, TR0 and TF0 are dedicated for TL0 whereas the control bits and flags of timer 1are
dedicated for TH0. Thus, TH0 now controls the timer 1interrupt.

Mode 3 provides application requiring for an extra 8-bit timer. So, with timer 0 in
mode 3, 8051 looks like having 3 timers. While timer 0 in mode 3, 8051 looks like having
3 timers. While timer 0 in mode 3, timer 1 may be switched on or off by switching it out
of and into its own mode 3 or can be still used for baud rate generation or in any
application not requiring interrupt.

Delay Generation:

1. In any mode, the timer counts in increasing manner i. e. from starting point upto
final count. (FFH or FFFFH).
2. Programmer cannot change the end points but the starting point depending upon
the required delay.
3. The clock frequency gets divided by 12 and then given to the timer section, i. e.
11.0592MHz/ 12=921.6KHz
4. So, the frequency of counter is f=921.6KHz.
5. Time required for counter to increment by 1 is (1/f) i. e. 1.085µsec (microsecond).
6. So, to calculate the required delay, we need to calculate the total count which is
multiplied by above value with the required delay.
7. The timer programming in either languages may be done in two ways. i. e. without
interrupt and with interrupt.
8. In either way timer need to started manually. The overflow needs to be monitored
through TF in without interrupt whereas monitoring is not required in interrupt
enabled programming.

Summary of delay generation:


1. Timer frequency (fT) = fosc / 12 = 921.6KHz
2. T= 1/ fT = 1/921.6K = 1.085 µsec.
3. Divide the required delay by 1.085 µsec to get ‘n’ (Delay/1.085 µsec).
4. Subtract the value from end point i. e. (65536- n) or (8192-n) depending on mode.
If mode 0 is used then use 8192-n.
If mode 1 is used then use 65536-n.
If mode 2 and 3 are used then use 256-n.
5. Convert the value from decimal to hex to get yyxx H.
6. Load the xx in TL0 and yy in TH0 if timer 0 is used.
7. Start the timer and observe TF0.

Programs:

1. Write a program to toggle all bits of port P1 continuously with some delay in
between. Use timer 0, 16-bit mode to generate delay.
2. Write a program to toggle only bit P1.5 continuously every 50ms. Use timer 0,
mode 1 (16-bit) to create the delay.
3. Write the program to toggle all the bits of P2 continuously after every 500ms. Use
timer 1, mode 1 to create the delay.
4. Write a program to toggle only pin P1.5 continuously every 250ms. Use timer 0,
mode 2 (8-bit auto reload) to create the delay.
5. Write a program to create a frequency of 2500 Hz on pin P2.7. Use timer 1, mode
2 to create the delay.

Polling and Interrupts:

Polling:

In polling the processor continuously tests every device in turn as to whether it


requires attention. The polling is carried out by a polling program that shares the
processing time with the currently running task. A device indicates it requires attention
by setting a bit in its device status register.

Most of the time, device will not require attention and when one does it will have
to wait until it is next interrogated by the polling program. This is inefficient method and
much of the processors time is wasted on unnecessary polls.

Interrupts:
An interrupt is a signal to the microprocessor from a device that requires
attention. The microprocessor will respond by setting aside execution of its current task
and deal with the interrupting device. When the interrupting device has been dealt with,
the microprocessor continues with its original task as if it had never been interrupted.

In order to be able to continue from where it was before the interrupt, the current
state of the microprocessor must be saved. This is achieved by saving the contents of all
its internal registers, both general purpose and special registers, to an area of memory
called the stack.

The registers will then be reinstated from the stack on completion of the interrupt
call, allowing the microprocessor to resume its original task.

Any device that generates an interrupt signal must also be accompanied by code
that will be run by the microprocessor instructing it how to end to the device’s needs.
Such code is referred to as an interrupt handler or interrupt service routine (ISR) and is
a small piece of software executed in response to a particular interrupt.

Every device that is capable of generating an interrupt signal will have its own
particular ISR. These interrupt service routines are essential for the operation of a
computer system and are loaded into memory at boot-up along with the kernel. To
summarize son far, whenever an interrupt signal is received, the microprocessor will
respond by

1. Completing its current instruction.


2. Transferring the contents of important registers to the stack.
3. Deal with the interrupting device, by executing its interrupt service routine.
4. Restore registers from the stack and resume execution of the task which was
interrupted.

Comparison between Polling and Interrupt

Polling Interrupt
1. Processor continuously checks for 1. The signal intimates the processor
signal. by interrupt.
2. Processor is always busy checking 2. Processor is never busy in checking
the state of the signal. the signal state.
3. Inefficient system as processor is 3. Efficient system as processor is not
busy. busy.
4. Time consuming 4. Less time consuming.
5. No extra hardware is required. 5. Extra hardware needed.

8051 Interrupts:

An interrupt is an asynchronous signal indicating the need of attention or a


synchronous event in software indicating the need for change in execution.

Some important things about interrupt needs to be considered:

1. Interrupt are always given highest priority.


2. For more than one interrupt, each interrupt has different priority level.
3. Many microcontrollers including 8051 allow change in default priority levels of
interrupt.
4. When interrupt occurs, the address of current execution is automatically saved
into stack and the address of interrupt is loaded in PC.
5. At the end of interrupt execution, the control returns to the ongoing task by
reference of its saved address.
6. Execution of interrupt is nothing but execution of a set of instructions called as ISR
(Interrupt Service Routine) stored at a fixed location.
7. The interrupt response time should be ideally zero.
8. More the interrupt more is the efficiency of system from multitasking point of
view.

Types of Interrupts:

The interrupts may be classified into several classes by different ways:

a) Maskable and Non-Maskable Interrupt:


Maskable interrupt means which can be avoided. Non-maskable interrupt
which cannot be avoided are called as non-maskable interrupt.
b) Hardware and Software Interrupt:
Hardware interrupt occurs by external or internal hardware. The
asynchronous signal is nothing but the hardware interrupt. The synchronous
signal which is predefined by programmer.

c) Vectored and Non-vectored Interrupt:

The vectored interrupt is the one which starts execution from fixed
location after receiving interrupt. The interrupt having particular address is called as
vectored interrupts and interrupts which don’t have any address are non-vectored
interrupts.

d) Edge triggered and level triggered interrupt:

IE (Interrupt Enable Register):


The IE register is an 8-bit register used for enabling or disabling any one or all
interrupt sources.
EA: (Enable All) No interrupt will be enabled if this bit is 0. So to use any interrupt, this
bit should be 1.
ES: Its is set to enable the serial interrupt.
ET1: It is set to enable the timer 1 overflow interrupt.
EX1: It is set to enable the external 1 interrupt. (INT1)
ET0: It is set to enable the timer 0 overflow interrupt.
EX0: It is set to enable the external 0 interrupt. (INT0)
Bit 5 and 6 are not used in 8051: Bit 5 used as timer 2 overflow interrupt enable in
8052.

IP (Interrupt Priority Register):

The interrupt priority register is used to change the predefined priorities of the interrupt.
The predefined priorities are as follows:
PRIORITY INTERRUPT
1 External 0
2 Timer 0 Overflow
3 External 1
4 Timer 1 Overflow
5 Serial Interrupt
If you wish to change the priorities as per our application, we can do it using IP register.
It is an 8-bit register. It is a bit addressable register and can be accessed with address
B8H.
PS: Serial Interrupt priority bit.
PT1: Timer 1 interrupt priority bit.
PX1: External 1 interrupt (INT1) priority bit.
PT0: Timer 0 interrupt priority bit.
PX0: External 1 interrupt (INT0) priority bit.
Bit 5, 6 and 7 are not used: Bit 5 is used for priority of timer 2 overflow interrupt in
8052.
The priority level of interrupt can be changed to low or high by making the respective
bits 0 or 1. After reset, the values of all bits are 0. If only one bit is set, it will have highest
priority otherwise the default priorities will be considered.

Serial Communication:
There are two types of communications used in microcontrollers, parallel and
serial. Considering the disadvantages of parallel communication like hardware cost and
limitation of distance, the serial communication is preferred. In PC’s the serial port is
available which is called as COM port.
8051 has both parallel and serial communication. Parallel communication is
achieved through four parallel ports whereas serial communication is done through RxD
and TxD pins of UART. It is done through synchronous communication.
The transmission rate is generally expressed in terms of bits per second which is
called as bps rate or the popular name is Baud Rate. The common values are 2400, 4800,
9600 etc. Notice that no unit is used after the numbers but by default it is bits per second.

SCON (Serial Control Register):


The SCON is an 8-bit serial control register. It controls the serial communication.
It not only contains the mode control bits but also 9th bit of transmission and reception
as well as serial interrupts. The direct address is 98H. The value after reset is 00H. The
format of SCON is shown below.
SM0 SM1 SM2 REN B8 RB8 TI RI
BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
SM0 and SM1- Serial Mode Selection Bits:
These bits are used to select the mode of serial communication. There are total 4
modes of serial communication.
SM0 SM1 Mode Description Baud Rate
0 0 0 8-bit Shift Register 1/12 the quartz frequency
0 1 1 8-bit UART Determined by the timer 1
1 0 2 9-bit UART 1/32 the quartz frequency
(1/64 the quartz frequency)
1 1 3 9-bit UART Determined by the timer 1
SM2- Multipurpose communication enable:
This bit enables the multiprocessor communication feature in mode 2 and 3. If
activated, the RI will not be activated if the received stop bit (for mode 1) or 9 th bit RB8
(mode 2 & 3) is 0. For mode 0, SM2 should be 0.

REN- Reception Enable:


When it is set, reception will be enabled, otherwise disabled.

TB8- Transmission bit 8:


This is the 9th received bit in mode 2 and 3. In mode 1, it contains the stop bit if
SM2 was 0. In mode 0, it is not used.

TI- Transmission interrupt:


This is the transmission interrupt flag. It indicates that the data has been
transmitted. In mode 0, it is set by hardware at the end of 8th bit or at starting of stop bit
in other modes. It must be cleared by software before transmitting next byte.

RI- Reception Interrupt:


Similar to RI, this bit is reception interrupt flag. It indicates that the data byte has
been received. In mode 0, it is set by hardware at the end of 8th bit or halfway through the
stop bit time in other modes. It must be cleared by software for receiving next byte.

SBUF (Serial Buffer Register):


The SBUF register is an 8-bit serial buffer register which works just as mail box. It
means it temporarily stores the data to be transmitted as well as the received data. Also
the framing of start and stop bits is also done in SBUF while transmission. The direct
address of SBUF is 99H.
While programming using assembly, data may be written in SBUF immediately or
using accumulator whereas received data can be taken out through accumulator only.
The value of SBUF after reset is undefined.

X X X X X X X X
BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
Serial Communication Modes:
There are four modes of serial communication.
Mode Description Baud Rate
0 8-bit Shift Register 1/12 the quartz frequency
1 8-bit UART Determined by the timer 1
2 9-bit UART 1/32 the quartz frequency
3 9-bit UART Determined by the timer 1

Mode 0- 8-bit Shift Register:


In mode 0, serial data is transmitted and received through the RXD pin, while the
TXD pin output clocks. The baud rate is fixed at 1/12 the oscillator frequency. On
transmit; the least significant bit (LSB bit) is sent/received first.
TRANSMIT: Data transmit is initiated by writing data to the SBUF register. In fact, this
process starts after any instruction being performed upon this register. When all 8 bits
have been sent, the TI bit of the SCON register is automatically set.

RECEIVE: The data reception through the RXD pin starts upon the two following
conditions are met; bit REN=1 and RI=0. When all 8 bits have been received, the RI bit of
the SCON register is automatically set indicating that one byte receive is complete.
Since there are no START and STOP bits or any other bit except data sent from SBUF
register in the pulse sequence, this mode is mainly used when the distance between
devices is short, noise is minimized and operating speed is of importance.

Mode 1- 8-bit Shift Register:


The mode 1 is an 8-bit UART mode in which the data byte is framed with a start bit
(always 0) and stop bit(always 1). So in total 10 bits are transmitted.
TRANSMIT: While transmission, the data byte is framed and then sent through SBUF. TI
is set by hardware upon sending stop bit.
RECEIVE: When start bit 0 is received on RXD pin, the reception is initiated. Note that
REN must be 1 to initialize reception. RI is set by hardware upon reception of last bit.
The baud rate in this mode is not fixed and it is determined by the count in timer 1. The
timer 1 is used in mode 2 for generation of baud rate.
Mode 2- 9-bit UART:
The mode 2 is 9 bit mode. An extra feature added is the parity bit for error
checking. It transmits a start bit (0), 8 data bits, 9th parity bit and a stop bit (1). The 9th bit
is stored in TB8 bit of TCON. Similar to other modes, TI is set after transmission. The
transmission and reception process is similar to mode 1.
The baud rate in this mode is fixed which is 1/32 of the oscillator frequency when SMOD
is 1 and 1/64 when SMOD is 0.

Mode 3- 9-bit UART mode:


This mode is similar to mode 2 except the baud rate. The baud rate in this case is
determined by the count in timer 1 similar to mode 1. This is mostly used for serial
communication.

RS 232 Standard:
The well-known serial communication standard i. e. RS232. The term RS means
Recommended Standards. It is most popular UART protocol used in embedded systems.

Features of RS 232
1. Input and output voltage levels are not TTL compatible.
2. 1 bit is represented by -3 to -25V.
3. 0 bit is represented by +3 to +25V.
4. -3 to +3 undefined.
5. MAX 232 IC chips are used as level converter between PC and microcontroller
to change the voltage levels from CMOS to TTL and vice versa.
6. Interfaced with a 9 pin DB-9 connector.

DB-9 Connector:
The connector used for connection in RS 232 is DB-9. This connector is
available in male and female version. It is derived from old DB-25 connector. The
DB-25 connector was being used for dot matrix printers.
DB-9 Connector
To reduce number of pins, many pins from DB-25 were removed and only 9
important pins were kept in DB-9.

Pin description of DB-9


Pin Description of DB-9:
1. DCD (Data Carrier Detect): This pin is used by the modem (also referred to
as data communication equipment DCE) to inform the PC (also referred to as
data terminal equipment DTE) that connection between modem and a valid
carrier has been made. This is input for PC. This is an active low signal.
2. RxD (Received data 0): The serial data is received on this pin. So it is
connected to TxD of other side.
3. TxD (Transmitted data): The serial data is transmitted through this pin. So it
is connected to RxD of other side.
4. DTR (Data terminal ready): When a terminal (DTE) is turned on, it sends
DTR to indicate that it is ready for communication. It is ouput from DTE.
5. GND (Signal ground)
6. DSR (Data Set Ready): Similar to DTR, this signal is asserted by DCE to
indicate that it is ready. So it is output from DCE. This is an active low signal.
So DTE will not send data if this pin is high, i.e. inactive.
7. RTS (Request to send): This is a handshaking signal used when DTE wants to
transmit data byte. This is an active low signal. This is an input to DCE.
8. CTS (Clear to Send): This signal is used by DCE as an acknowledgement for
RTS signal. It is an active low signal which indicates that DCE has received RTS
signal and is ready to receive data from DTE.
9. RI (Ring indicator): It is an ouput from DCE indicating that telephone is
ringing. It s least often used handshaking signal which is used when PC used
for answering the phone.

Interfacing of 8051 microcontroller with MAX 232:


The serial communication is used for communication between microcontroller to
microcontroller or microcontroller to PC. In case of microcontroller to microcontroller,
both transmitter and receiver are in same level. So the communication may be done using
only RxD and TxD pins as the speed of both devices are same. Bit in case of
microcontroller to other devices like PC, the speed is different so handshaking signals
need to be used. Also the voltage levels are different. So a voltage converter is used
between two devices. The voltage converter IC is called MAX 232. It is also known as line
driver. Note that 2 pairs of devices can communicate simultaneously. The pin diagram
and typical connection diagram is as shown below.

You might also like