8051 Timer Programming and Interrupts
8051 Timer Programming and Interrupts
Both timers 0 and 1 use the same register, called TMOD (timer
mode), to set the various timer operation modes.
Timer ON/OFF control bits Trigger mode of external interrupt control bits
TR1 –Timer 1 run control bit. Set = Timer ON and Clear = Timer OFF
TF1 – Timer flag which is set when the Timer 1 rolls over from FFFFH to 0000H
Cleared the bit in software !.
TR0 – Timer 0 run control bit. Set = Timer ON and Clear = Timer OFF
TF0 – Timer flag which is set when Timer 0 rolls over from FFFFH to 0000H.
Cleared the bit in software !
IE1 and IT1 – Set the trigger mode of external interrupt 1
IE0 and IT0 – Set the trigger mode of external interrupt 0
TIMER MODE REGISTER (TMOD)
Timer 1 Timer 0
GATE – Gating control when set the timer/counter is enabled only while the INTx
pin is high and the TRx control pin is set. When cleared, the timer is enabled
whenever the TRx control bit is set.
C/T – Timer or counter selected. Cleared for timer operation (input from internal
system clock) and Set for counter operation (input from Tx input pin)
M1 & M0 – Mode bits
TMOD REGISTER
M1 & M0 – Mode bits
M1 M0 Mode Operating Mode
Gating control when set Timer/counter is enabled only while the INTx pin is high and the TRx
control pin is set. When cleared, the timer is enabled whenever the TRx control bit is set
Timer or counter selected Cleared for timer operation (input from internal system clock)
Set for counter operation (input from Tx input pin)
Software control:
• GATE=0 in the TMOD register
• The start and stop of the timer are controlled by the TR (timer
start) bits TR0 and TR1
• SETB instruction starts it
• CLR instruction stops it as long as GATE=0 in the TMOD register
• Hardware control:
• GATE=1 in the TMOD register
Example
# Find the timer’s clock frequency and its period for various 8051-based
system, with the crystal frequency 11.0592 MHz when C/T bit of TMOD is 0.
Solution:
1/12 × 11.0529 MHz = 921.6 MHz;
T = 1/921.6 kHz = 1.085 us
# Indicate which mode and which timer are selected for each of the following.
(a) MOV TMOD, #01H (b) MOV TMOD, #20H (c) MOV TMOD, #12H
Solution:
We convert the value from hex to binary.
(a) TMOD = 00000001, mode 1 of timer 0 is selected.
(b) TMOD = 00100000, mode 2 of timer 1 is selected.
(c) TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are selected.
TIMER/COUNTER MODE 0 :13 BIT COUNTER USING TIMER 1
TIMER OPERATION IN MODE1
The following are the characteristics and operations of mode1:
1. It is a 16-bit timer; therefore, it allows value of 0000 to FFFFH
to be loaded into the timer’s register TL and TH.
2. After TH and TL are loaded with a 16-bit initial value, the timer
must be started by SETB TR0 for timer 0 and SETB TR1 for
timer 1.
3. After the timer is started, it starts to count up until it reaches its
limit of FFFFH.
4. When it rolls over from FFFFH to 0000, it sets high a flag bit
called TF (timer flag) : TF0 for timer 0, and TF1 for timer 1
5. When this timer flag is raised, one option would be to stop the
timer with the instructions CLR TR0 or CLR TR1, for timer 0
and timer 1, respectively.
6. To repeat the process TH and TL must be reloaded with the
original value, and TF must be reloaded to 0
TIMER / COUNTER MODE 1 :
16 BIT COUNTER USING TIMER 1
TL1
(8 Bits)
Indicate which mode and which timer are selected for each of the following.
(a) MOV TMOD, #01H
(b) MOV TMOD, #20H
(c) MOV TMOD, #12H
Solution:
Solution:
(1)
MOV TMOD, #01H ; Timer 0 and mode 1 set, C/T = 0 to use XTAL
; clock source, Gate = 0 to use software ON/OFF
MOV TL0, #0ABH ; TL0 =ABH
MOV TH0, #0F0H ; TH0 =F0H
SETB TR0 ; Start Timer 0
Timers Programming
THx TLx # of count to set TFx
FFH FFH 1
Solution: FFH FEH 2
(2)
Assume 12 MHz clock :
1
Time for 1 Timer clock = 1 machine cycle = 6
x12 s = 1s
12x10
Example 3
In the following program, we create a square wave of 50% duty cycle (with equal portions
high and low) on the P1.5 bit. Timer 0 is used to generate the time delay. Analyze the program
Solution :
Timer value = 0108H = 264 in decimal, counts to rolls over = 65536 – 264 = 65272 in decimal
Timer cycle of Timer = 65272
Total machine cycles of the subroutine = [2 + 1 + (5 + 65272 + 4) x 200 + 2] = 13056205
Total delay time 13056205 x 1s = 13056.2 ms
INTERRUPT & POLLING
A single microcontroller always connects to
serve several peripheral devices through its
I/O ports
There are two ways for the peripheral devices
to request service from microcontroller
POLLING
INTERRUPT
PROGRAMMED I/O (POLLING)
INTERRUPT POLLING
Method Devices notify MCU MCU continuously
by sending it an monitors devices to
interrupt signal determine whether
while the MCU is they need service
doing another work
Response time Faster Slower
Need of MCU time Less More
Priority setting Yes No
STEPS IN HANDLING AN INTERRUPT REQUEST
When an interrupt is activated and is accepted by the MCU, the
main program is interrupted. The following actions occur:
PC saved PC popped
on stack from stack
stack
Solution:
(a) MOV IE, #10010110B ; enable serial, Timer 0, EX1 interrupts
or
SETB IE.7 ; EA=1, Global enable
SETB IE.4 ; enable serial interrupt
SETB IE.1 ; enable Timer 0 interrupt
SETB IE.2 ; enable EX1 interrupt
(b) CLR IE.1 ; disable Timer 0 interrupt
(c) CLR IE.7 ; disable all interrupts
TIMER INTERRUPT
When timer rolls over, its timer flag (TF) is set.
If the timer interrupt in the IE register is enabled, whenever the TF is
set, the microcontroller is interrupted and jumps to the interrupt
vector table to service the ISR.
With timer interrupt enabled, microcontroller can do other things and
no need to monitor the TF for rolling over.
Priority bit = 1 (assign high priority) Priority bit = 0 (assign low priority)
PT2,– Timer 2 (8052 only), PT1 & PT0 -- Timer 1 & Timer 0 interrupts
PX1 & PX0– External interrupts 1 & 0, PS – Serial port interrupt
Setting Interrupt Priority with the IP register
Example 8.7 :
(a) Program the IP register to assign the highest priority to INT1,
(b) Discuss what happens if INT0, INT1 and TF0 are activated at the
same time.
Solution :
(a) MOV IP, #00000100B ; set IP.2=1 INT1 has the highest priority
or SETB IP.2
(b) Priority of interrupt will be changed to INT1 > INT0 > TF0
The 8051 will services INT1 first and then INT0 and TF0.
(As the INT0 and TF0 bits in IP register are 0, their priorities follow the
sequence in Table 8.1)
Setting Interrupt Priority with the IP register
Example 8.8 :
The IP register is set by the instruction “MOV IP, #00001100B”
after reset. Discuss the sequence in which the interrupts are serviced.
Solution :
MOV IP, #00001100B instruction sets INT1 & TF1 to a higher
priority level compared with the rest of the interrupts.
POWER UP DEFAULT PRIORITY: INT0 > TF0 > INT1 > TF1 > SERIAL(RI+TI)
Interrupt inside an interrupt
Higher-priority interrupt can interrupt a low-priority interrupt.
An interrupt cannot be interrupted by a low-priority interrupt
No low priority interrupt can get the immediate attention of the
CPU until the 8051 has finished servicing the high-priority
interrupts