Unit-III 8051 Timers, Interrupts and Serial Communication: TH/TL (Timer High/ Timer Low)
Unit-III 8051 Timers, Interrupts and Serial Communication: TH/TL (Timer High/ Timer Low)
Introduction:
• 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:
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.
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.
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.
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.
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.
Timer/Counter Modes:
Depending on application, the timers can be used in any of the four modes. These
modes are selected by TMOD register.
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)
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.
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.
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.
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 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.
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:
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
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:
Types of Interrupts:
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.
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.
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
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.
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.