Unit-5
8051 Interrupts and Timers/Counters
Interrupt is an event that requires the CPU to stop the current program execution and perform some service related to the event.
For example Reading a book and the phone rings then we stop reading and get the phone and talk then return to the book where one read and resume to read
• The phone call is an interrupt and the talk is an interrupt service routine (ISR) or an interrupt handler.
Figure 1. Interrupt Structure
On ‘RESET’ all the interrupts get disabled, and therefore, all these interrupts must be enabled by a software.
8051 handles five interrupt sources which include:
- Two internal known as Timer Interrupts
- Two external and
- Serial interrupt.
Each of these interrupts have interrupt vector address. Highest priority interrupt is the Reset having vector address 0x0000.
Figure 2. Interrupt Structure
This register can be used to enable or disable interrupts programmatically. This register is an SFR. The address is A8H. This byte is bit addressable. So it can be programmed by the user.
Bit Address | AF
| AE | AD | AC | AB | AA | A9 | A8 |
Bit Details | EA | X | x | ES | ET1 | EX1 | ET0 | EX0 |
Figure 3. IE Register
Bit Details | High Value(1) | Low Value(0) |
EA | Least significant 5 bits can decide enable or disable these five interrupts | Disable all five interrupts . It just ignores the rest five bits |
ES | Enable Serial Port Interrupt | Disable Serial Port Interrupt |
ET1 | Enable Timer1 Interrupt | Disable Timer1 Interrupt |
EX1 | Enable external interrupt1 | Disable external interrupt1 |
ET0 | Enable Timer0 interrupt | Disable Timer0 Interrupt |
EX0 | Enable external interrupt 0 | Disable external interrupt 0 |
Figure 4.Interrupts
The external interrupts of 8051 are INT0and. INT1 These interrupts can be programmed to either edge-triggered or level triggered. The TCON register can be used top rogram external interrupts to edge or level triggered. The TCON isTimer Control. TCON is another bit addressable SFR. Here the address is 88H.
Bit Address | 8F | 8E | 8D | 8C | 8B | 8A | 89 | 88 |
Bit Details | TF1 | TR1 | TF0 | TR0 | IE1 | IT1 | IE0 | IT0 |
Figure 5. TCON register
Bit Details | High Value(1) | Low Value(0) |
IT0 | Set INT0 as negative edge triggered input | Set INT0 as active low level triggered input. |
IT1 | Set INT1 as negative edge triggered input | Set INT1 as active low level triggered input. |
IE0 | This will be 1. When INT0 is activated as level triggered | This will be 0 when INT0 is activated as edge triggered |
IE1 | This will be 1. When INT1 is activated as level triggered | This will be 0 when INT1 is activated as edge triggered |
TR0 | Set Timer0 as run mode | Set Timer0 as stop mode. |
TR1 | Set Timer1 as run mode | Set Timer1 as stop mode |
TF0 | High when Timer0 overflow occurs | After resetting the timer 0 this will also be changed to 0 state |
TF1 | High when Timer1 overflow occurs | After resetting the timer T1 this will also be changed to 0 state |
Serial Port Interrupt
The serial ports can be used either Transmitting mode or reception mode. The interrupt status for the Transmission is provided by TI, and status for Reception is provided by RI. These are two bits of SCON(Serial Control). This is also a bit addressable SFR. The address is98H
Bit Address | 9F | 9E | 9D | 9C | 9B | 9A | 99 | 98 |
Bit Details | SM0 | SM1 | SM2 | REN | TB8 | RB8 | TI | RI |
Figure 6. SCON register
Bit Details | Description |
SM0 | This is Serial Port Mode0 shift register |
SM1 | This is Serial Port Mode 1 (8-bit UAR + variable) |
SM2 | Enable multiprocessor communication in the mode 2 or 3 |
REN | Set or reset by the software to enable or disable the Reception |
TB8 | It indicates the 9th bit that will be transmitted in mode 2 or 3. It can be set or reset by software |
RB8 | In mode 2 or 3 the 9th bit was received in mode 1. |
TI | The transmission interrupt flag . It can be set by hardware |
RI | The receiver interrupt flag . It can be set by hardware |
Figure 7. Serial Interrupt
Key Takeaways:
An interrupt is an event that occurs randomly in the flow of continuity.
A timer is a specialized type of clock which is used to measure time intervals. A timer that counts from zero upwards for measuring time elapsed is often called a stopwatch. It is a device that counts down from a specified time interval and used to generate a time delay, for example, an hourglass is a timer.
A counter is a device that stores (and sometimes displays) the number of times a particular event or process occurred, with respect to a clock signal. It is used to count the events happening outside the microcontroller. In electronics, counters can be implemented quite easily using register-type circuits such as a flip-flop.
The 8051 has two timers: timer0 and timer1. They can be used either as timers or as counters. Both timers are 16 bits wide. Since the 8051 has an 8-bit architecture, each 16-bit is accessed as two separate registers of low byte and high byte.
Timer0 registers is a 16 bits register and accessed as low byte and high byte. The low byte is referred as a TL0 and the high byte is referred as TH0. These registers can be accessed like any other registers.
Figure8. Timer 0
Timer1 registers is also a 16 bits register and is split into two bytes, referred to as TL1 and TH1.
Figure 9.Timer 1
TMOD (timer mode) Register:
This is an 8-bit register which is used by both timers 0 and 1 to set the various timer modes. In this TMOD register, lower 4 bits are set aside for timer0 and the upper 4 bits are set aside for timer1. In each case, the lower 2 bits are used to set the timer mode and upper 2 bits to specify the operation.
Figure 10.TMOD Register
- The first bit is a GATE bit. Every timer has a means of starting and stopping. Some timers do this by software, some by hardware, and some have both software and hardware controls.
- The hardware way of starting and stopping the timer by an external source is achieved by making GATE=1 in the TMOD register. And if we change to GATE=0 then we do not need external hardware to start and stop the timers.
- The second bit is C/T bit and is used to decide whether a timer is used as a time delay generator or an event counter. If this bit is 0 then it is used as a timer and if it is 1 then it is used as a counter.
- In upper or lower 4 bits, the last bits third and fourth are known as M1 and M0 respectively. These are used to select the timer mode.
M0 | M1 | Mode | Operating Mode |
0 | 0 | 0 | 13-bit timer mode. 8 bit timer/counter THx and TLx as –bit prescalar |
0 | 1 | 1 | 16-bit timer mode , 16-bit timers/counters THx and TLx as 5-bit prescalar |
1 | 0 | 2 | 8-bit auto reload mode, 8-bit auto reload timer/counter; THx holds a value which is to be reloaded into TLx each time it overflows |
1 | 1 | 3 | Split timer mode |
Mode 1-
- It is a 16-bit timer; therefore it allows values from 0000 to FFFFH to be loaded into the timer’s registers TL and TH.
- After TH and TL are loaded with a 16-bit initial value, the timer must be started.
- This is done t by “SETB TR0” for timer 0 and “SETB TR1” for timer 1.
- After the timer is started. It starts count up until it reaches its limit of FFFFH.
- When it rolls over from FFFF to 0000H, it sets high a flag bit called TF (timer flag).
- This timer flag can be monitored. When this timer flag is raised, one option would be stop the timer with the instructions “CLR TR0“ or CLR TR1 for timer 0 and timer 1 respectively.
- Note that timer flag is TF0 for timer 0 and TF1 for timer1. After the timer reaches its limit and rolls over, in order to repeat the process the registers TH and TL must be reloaded with the original value and TF must be reset to 0.
Mode0-
- Mode 0 is exactly same like mode 1 except that it is a 13-bit timer instead of 16-bit.
- The 13-bit counter can hold values between 0000 to 1FFFH in TH-TL. Therefore, when the timer reaches its maximum of 1FFH, it rolls over to 0000, and TF is raised.
Mode 2-
It is an 8 bit timer that allows only values of 00 to FFH to be loaded into the timer’s register TH.
After TH is loaded with 8 bit value, the 8051 gives a copy of it to TL. Then the timer must be started. It is done by the instruction “SETB TR0” for timer 0 and “SETB TR1” for timer1. This is like mode 1.
After timer is started, it starts to count up by incrementing the TL register. It counts up until it reaches its limit of FFH.
When it rolls over from FFH to 00. It sets high the TF (timer flag). If we are using timer 0, TF0 goes high; if using TF1 then TF1 is raised.
When Tl register rolls from FFH to 00 and TF is set to 1, TL is reloaded automatically with the original value kept by the TH register. To repeat the process, we must simply clear TF and let it go without any need by the programmer to reload the original value. This makes mode 2 auto reload, in contrast in mode 1 in which programmer has to reload TH and TL.
Mode3-
Mode 3 is also known as a split timer mode. Timer 0 and 1 may be programmed to be in mode 0, 1 and 2 independently of similar mode for other timer. This is not true for mode 3; timers do not operate independently if mode 3 is chosen for timer 0. Placing timer 1 in mode 3 causes it to stop counting; the control bit TR1 and the timer 1 flag TF1 are then used by timer0.
TCON Register
Bits and symbol and functions of every bits of TCON are as follows:
TF1 | TR1 | TF0 | TR0 | IE1 | IT1 | IE0 | IT0 |
Figure 11.TCON Register
Bit | Symbol | Functions |
7 | TF1 | Timer1 overflow flag. Set when timer rolls from all 1’s to 0 |
6 | TR1 | Timer 1 run control bit. Set to 1 by programmer to enable timer to count. |
5 | TF0 | Timer 0 overflow flag |
4 | TR0 | Timer 0 run control bit |
3 | IE1 | External Interrupt 1 edge flag |
2 | IT1 | External Interrupt 1 signal type control bit |
1 | IE0 | External Interrupt 0 edge flag |
0 | IT0 | External Interrupt 0 signal type control bit |
The timer registers in 8051 are upward counters, increasing the value stored in them on each input clock signal. In the case of timers, this clock frequency is given by the pre-scaler (making it count upwards every 1.08 millisecond). Whereas in the case of a counter, this signal is given by any input value.
Now that the clock signal to the register is given by an external stimulus, it counts upwards for each clock pulse given by the external peripheral. making it act as a counter
In Intel 8051, there are two 16-bit timer registers. These registers are known as Timer0 andTimer1. The timer registers can be used in two modes. These modes areTimer mode and the Counter mode. The only difference between these two modes is the source for incrementing the timer registers.
In the timer mode, the internal machine cycles are counted. So this register is incremented in each machine cycle. So when the clock frequency is 12MHz, then the timer register is incremented in each millisecond. In this mode it ignores the
external timer input pin.
- In the counter mode, the external events are counted. In this mode, the timer register is incremented for each 1 to 0 transition of the external input pin. This type of transitions is treated as events. The external input pins are sampled once in each machine cycle, and to determine the 1or 0 transitions, another machine cycle will be needed. So in this mode, at least two machine cycles are needed. When the frequency is12MHz, then the maximum count frequency will be 12MHz/24 = 500KHz. So for event counting the time duration is 2 µs.
- There are four different modes of the Timer or Counter. The Mode 0 to Mode 2 are for both of the Timer/Counter. Mode 3 has a different meaning for each timer register. There is a register called TMOD. This register can be programmed to configure these timers or counters.
- The Serial port is used for serial communication in mode 1 and 3. Timer1 is used for generating the baud rate. So only Timer0 is available for timer or counter operations.
TMOD(Timer Mode) is an SFR. The address of this register is 89H. This is not bit-addressable.
Timer | Timer1 Mode | Timer0 Mode | ||||||
Bit Details | Gate (G) | C/T | M1 | M0 | Gate (G) | C/T | M1 | M0 |
The circuit that controls the running of the timers is shown in the figure.
Figure 12. TMOD register
Bit Details | High Value(1) | Low Value(0) |
C/T | Configure for the Counter operations | Configure for the Timer operations |
Gate (G) | Timer0 or Timer1 will be in RunMode when TRX bit of TCON register is high. | Timer0 or Timer1 will be in RunMode when TRX bit of TCON register is high and INT0 or INT1 is high. |
Bit Details | 00 | 01 | 10 | 11 |
M1 M0 | This is for Mode 0. (8-bit timer/counter, with 5-bit pre-scaler) | This is Mode 1. (16-bit timer/counter) | This is Mode 3 (8-bit auto reload-timer/counter) | This is Mode 3 (The function depends on Timer0 or Timer1) |
The Gate bit will be high when the timer or counter is in mode 0 to 2.
To configure Timer0 as 16-bit event counter and Timer1 as 8-bit auto reload counter, use the bit pattern as 0 0 1 0 0 1 0 1. It is equivalent to 25H. Then if we want to program the TMOD register with this bit pattern, we can use this instruction:
MOVTMOD,#25H
The above instruction is executed, then the timer/counter will be controlled by the software. To configure the system as hardware controlled mode, then the gate bits will be 1. So the bit patterns will be 1 0 1 0 1 1 0 1 = ADH
Then use this instruction:
MOVTMOD,#0ADH
The Mode 0 operation is the 8-bit timer or counter with a 5-bit pre-scaler. So it is a 13-bit timer/counter. It uses 5 bits of TL0 or TL1 and all of the 8-bits of TH0 or TH1.
Figure 13 . Mode 0 timer
In this example the Timer1is selected, in this case, every 32 (25)event for counter operations or 32 machine cycles for timer operation, the TH1 register will be incremented by 1. When the TH1overflows from FFH to 00H, then the TF1 of TCON register will be high, and it stops the timer/counter.
Example, we can say that if the TH1 is holding F0H, and it is in timer mode, then T
F1will be high after 10H * 32 = 512 machine cycles.
MOVTMOD,#00H
MOV TH1,#0F0H
MOV IE,#88H
SETB TR1
In the above program, the Timer1 is configured as timer mode 0. In this case Gate = 0. Then the TH1 will be loaded with F0H, then enable the Timer1 interrupt. At last set the TR1 of TCON register, and start the timer.
The Mode 1 operation is the 16-bit timer or counter. In the following diagram, we are using Mode 1 for Timer0.
Figure 14. Mode 1 of Timer
In this case every event for counter operations or machine cycles for timer operation, the TH0– TL0 register-pair will be incremented by 1. When the register pair overflows from FFFFH to 0000H, then the TF0 of TCON register will be high, and it stops the timer/counter.
Example, we can say that if the TH0 – TL0 register pair is holding FFF0H, and it is in timer mode, then TF0 will be high after 10H = 16 machine cycles. When the clock frequency is 12MHz, then the following instructions generate an interrupt 16 µs after Timer0 starts running.
MOV TMOD,#01H
MOV TLO,#F0H
MOV TH0, #0FFH
MOV IE,#82H
SETB TR0
In the above program, the Timer0 is configured as timer mode 1. In this case Gate = 0. Then the TL0 will be loaded with F0H and TH0 is loaded with FFH, then enable the Timer0 interrupt. At last set the TR0 of TCON register, and start the timer.
The Mode 2 operation is the 8-bit auto reload timer or counter. In the following diagram, we are using Mode 2 for Timer1.
Figure 15. Mode 2
In this case every event for counter operations or machine cycles for timer operation, the TL1register will be incremented by 1. When the register pair overflows from FFH to 00H, then the TF1 of TCON register will be high, also theTL1 will be reloaded with the content of TH1 and starts the operation again.
Example, If the TH1 and TL1 register both are holding F0H and it is in timer mode, then TF1 will be high after 10H= 16 machine cycles. When the clock frequency is 12MHz this happens after 16 µs, then the following instructions generate an interrupt once every 16 µs after Timer1 starts running
MOV TMOD,#20H
MOV TL1,#0F0H
MOV TH1,#0F0H
MOV #88H
SETBTR1
In the above program, the Timer1 is configured as timer mode 2. In this case Gate = 0. Then the TL1 and TH1 are loaded with F0H. then enable the Timer1 interrupt. At last set the TR1 of TCON register, and start the timer.
Timer1 in mode 2 generates the desired baud rate when the serial port is working on Mode 1 or 3.
Programming of Counters and Timers:
In order to program 8051 timers, it is important to know the calculation of initial count value to be stored in the timer register.
The calculations are as follows. In any mode,
Timer Clock period = 1/Timer Clock Frequency. = 1/(Master Clock Frequency/12)
- Mode 1 (16 bit timer/counter) Value to be loaded in decimal = 65536 – (Delay Required/Timer clock period) .Convert the answer into hexadecimal and load onto THx and TLx register. (65536D = FFFFH+1).
b. Mode 0 (13 bit timer/counter) Value to be loaded in decimal = 8192 – (Delay Required/Timer clock period) Convert the answer into hexadecimal and load onto THx and TLx register. (8192D = 1FFFH+1)
c. Mode 2 (8 bit auto reload) Value to be loaded in decimal = 256 – (Delay Required/Timer clock period) Convert the answer into hexadecimal and load onto THx register.
Upon starting the timer this value from THx will be reloaded to TLx register. (256D = FFH+1)
Steps for programming timers in 8051
Mode 1:
Load the TMOD value register indicating which timer (0 or 1) is to be used and which timer mode is selected. Load registers TL and TH with initial count values.
Start the timer by the instruction “SETB TR0” for timer 0 and “SETB TR1” for timer 1.
Keep monitoring the timer flag (TF) with the “JNB TFx,target” instruction to see if it is raised. Get out of the loop when TF becomes high.
Stop the timer with the instructions “CLR TR0” or “CLR TR1”, for timer 0 and timer 1, respectively.
Clear the TF flag for the next round with the instruction “CLR TF0” or “CLR TF1”, for timer 0 and timer 1, respectively.
Go back to step 2 to load TH and TL again.
Mode 0: The programming techniques mentioned here are also applicable to counter/timer mode 0. The only difference is in the number of bits of the initialization value.
Mode 2:
Load the TMOD value register indicating which timer (0 or 1) is to be used; select timer mode
Load TH register with the initial count value. As it is an 8-bit timer, the valid range is from 00 to FFH.
Start the timer. Keep monitoring the timer flag (TFx) with the “JNB TFx,target” instruction to see if it is raised.
Get out of the loop when TFx goes high. Clear the TFx flag.
Go back to step 4, since mode 2 is auto-reload.
Write a program to continuously generate a square wave of 2 kHz frequency on pin P1.5 using timer 1. Assume the crystal oscillator frequency to be 12 MHz. s.s.
Each half pulse = 250 The period of the square wave is T = 1/(2 kHz) = 500 s = 250s /1 s is: 250
The value n for 250 65536 - 250 = FF06H. TL = 06H and TH = 0FFH.
MOV TMOD,#10 ;Timer 1, mode 1
AGAIN: MOV TL1,#06H ;TL0 = 06H
MOV TH1,#0FFH ;TH0 = FFH
SETB TR1 ;Start timer 1
BACK: JNB TF1,BACK ;Stay until timer rolls over
CLR TR1 ;Stop timer 1
CPL P1.5 ;Complement P1.5 to get Hi, Lo
CLR TF1 ;Clear timer flag 1
SJMP AGAIN ;Reload timer
Write a program segment that uses timer 1 in mode 2 to toggle P1.0 once whenever the counter reaches a count of 100. Assume the timer clock is taken from external source
P3.5 (T1). The TMOD value is 60H
The initialization value to be loaded into TH1 is 256 - 100 = 156 = 9CH
MOV TMOD,#60h ;Counter1, mode 2, C/T’= 1
MOV TH1,#9Ch ;Counting 100 pulses
SETB P3.5 ;Make T1 input
SETB TR1 ;Start timer 1
BACK: JNB TF1, BACK ;Keep doing it if TF = 0 CPL P1.0 ;Toggle port bit
CLR TF1 ;Clear timer overflow flag
SJMP BACK ;Keep doing it
C Language
Timer programming using Timer interrupts involves following algorithm.
1. Configure the Timer mode by passing a hex value to TMOD register.
2. Load the initial values in the Timer low TLx and high THx byte.
3. Enable the Timer interrupt by passing hex value to IE register or setting required bits of IE register. For example,
IE = 0x82; enables Timer 0 interrupt
IE = 0x88; enables Timer 1 interrupt
or
EA = 1;
ET0 = 1; enables Timer 0 interrupt
IE^7 = 1;
IE^3 = 1; enables Timer 1 interrupt
4. Start the Timer by setting TRx bit.
5. Write Interrupt Service Routine (ISR) for the Timer interrupt. For example,
ISR definition for Timer 0 :
void ISR_Timer0(void) interrupt 1
{
<Body of ISR>
}
ISR definition for Timer 1 :
void ISR_Timer1(void) interrupt 3
{
<Body of ISR>
}
6. If the Timer has to be stopped after once the interrupt has occurred, the ISR must contain the statement to stop the Timer.
For example,
void ISR_Timer1(void) interrupt 3
{
<Body of ISR>
TR1 =0;
}
7. If a routine written for Timer interrupt has to be repeated again and again, the Timer run bit need not be cleared. But it should be kept in mind that Timer will start updating from 0000H and not the initial values in case of mode 0 and 1. So the initial values must be reloaded in the interrupt service routine.
For example,
void ISR_Timer1(void) interrupt 3
{
<Body of ISR>
TH1 =0XFF; //load with initial values if in mode 0 or 1
TL1 = 0xFC;
}
Example:
Time delay in Mode1 using polling method
// Use of Timer mode 1 for blinking LED using polling method
// XTAL frequency 11.0592MHz
#include<reg51.h>
sbit led = P1^0; // LED connected to 1st pin of port P1
void delay();
main()
{
unsigned int i;
while(1)
{
led=~led; // Toggle LED
for(i=0;i<1000;i++)
delay(); // Call delay
}
}
void delay() // Delay generation using Timer 0 mode 1
{
TMOD = 0x01; // Mode1 of Timer0
TH0= 0xFC; // FC66 evaluated hex value for 1millisecond delay
TL0 = 0x66;
TR0 = 1; // Start Timer
while(TF0 == 0); // Using polling method
TR0 = 0; // Stop Timer
TF0 = 0; // Clear flag
}
Key Takeaways:
The 8051 has two counters/timers which can be used either as timer to generate a time delay or as counter to count events happening outside the microcontroller. The 8051 has two timers: timer0 and timer1. They can be used either as timers or as counters. Both timers are 16 bits wide.
References:
- The 8051 Microcontroller and Embedded Systems using Assembly and C by Muhammad Ali Mazidi.
- The 8051 Microcontroller by I. Scott Mackenzie, Raphael C.W Phan
3. 8051 Microcontrollers: Internals, Instructions, Programming, and Interfacing Book by Subrata Ghoshal