6.timer Programming
6.timer Programming
Embedded Systems
CHAPTER 9
8051 TIMER
PROGRAMMING IN
ASSEMBLY
1
OBJECTIVES
2
SECTION 9.1: PROGRAMMING 8051
TIMERS
3
SECTION 9.1: PROGRAMMING 8051
TIMERS
Timer 0 registers
low byte register is called TL0 (Timer 0 low byte) and
the high byte register is referred to as TH0 (Timer 0 high
byte)
can be accessed like any other register, such as A, B,
R0, R1, R2, etc.
"MOV TL0, #4 FH" moves the value 4FH into TL0
"MOV R5, TH0" saves TH0 (high byte of Timer 0) in R5
4
SECTION 9.1: PROGRAMMING 8051
TIMERS
5
SECTION 9.1: PROGRAMMING 8051
TIMERS
Timer 1 registers
also 16 bits
split into two bytes TL1 (Timer 1 low byte) and
TH1 (Timer 1 high byte)
accessible in the same way as the registers of
Timer 0.
6
SECTION 9.1: PROGRAMMING 8051
TIMERS
7
SECTION 9.1: PROGRAMMING 8051
TIMERS
8
SECTION 9.1: PROGRAMMING 8051
TIMERS
10
13-bit Time Mode (mode 0)
Mode 1 programming
16-bit timer, values of 0000 to FFFFH
TH and TL are loaded with a 16-bit initial value
timer started by "SETB TR0" for Timer 0 and "SETB TR1"
for Timer l
timer count ups until it reaches its limit of FFFFH
rolls over from FFFFH to 0000H
sets TF (timer flag)
when this timer flag is raised, can stop the timer with "CLR
TR0" or "CLR TR1
after the timer reaches its limit and rolls over, the registers
TH and TL must be reloaded with the original value and TF
must be reset to 0
16
SECTION 9.1: PROGRAMMING 8051
TIMERS (not needed for quiz)
19
Example 9-4
In the following program, we are creating 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
20
Example 9-9
The following program generates a square wave on pin P 1.5 continuously
using Timer 1 for a time delay. Find the frequency of the square wave if
XTAL = 11.0592 MHz. In your calculation do not include the overhead due
to the timer setup instructions in the loop.
21
SECTION 9.1: PROGRAMMING 8051
TIMERS
22
Example 9-12
Assuming XTAL = 11.0592 MHz, write a program to generate a
square wave of 50 Hz frequency on pin P2.3.
T = 1/50 Hz = 20 ms
1/2 of it for the high and low portions of the
pulse = 10 ms
10 ms / 1.085 us = 9216
65536 - 9216 = 56320 in decimal = DC00H
TL = 00 and TH = DCH
The calculation for 12MHz crystal uses the
same steps
23
Example 9-12 (cont)
Assuming XTAL = 11.0592 MHz, write a program to generate a
square wave of 50 Hz frequency on pin P2.3.
24
SECTION 9.1: PROGRAMMING 8051
TIMERS
25
SECTION 9.1: PROGRAMMING 8051
TIMERS
26
Example 9-13
Examine the following program and find the time delay in seconds.
Exclude the time delay due to the instructions in the loop.
27
SECTION 9.1: PROGRAMMING 8051
TIMERS (for information only)
Mode 0
works like mode 1
13-bit timer instead of 16bit
13-bit counter hold values 0000 to 1FFFH
when the timer reaches its maximum of
1FFFH, it rolls over to 0000, and TF is set
28
SECTION 9.1: PROGRAMMING 8051
TIMERS
Mode 2 programming
8-bit timer, allows values of 00 to FFH
TH is loaded with the 8-bit value
a copy is given to TL
timer is started by ,"SETB TR0" or "SETB TR1
starts to count up by incrementing the TL register
counts up until it reaches its limit of FFH
when it rolls over from FFH to 00, it sets high TF
TL is reloaded automatically with the value in TH
To repeat, clear TF
mode 2 is an auto-reload mode
29
SECTION 9.1: PROGRAMMING 8051
TIMERS
30
Example 9-14
Assuming that XTAL = 11.0592 MHz, find (a) the frequency of the
square wave generated on pin P1.0 and (b) the smallest frequency
achievable in this program, and the TH value to do that.
31
SECTION 9.1: PROGRAMMING 8051
TIMERS
32
SECTION 9.2: COUNTER
PROGRAMMING (for information only)
33
SECTION 9.2: COUNTER
PROGRAMMING (for information only)
34
SECTION 9.2: COUNTER
PROGRAMMING
35
Example 9-18
Assuming that clock pulses are fed into pin T1, write a program for
counter 1 in mode 2 to count the pulses and display the state of the
TL1 count on P2. (for information only)
to LEDs
39
SECTION 9.2: COUNTER
PROGRAMMING
40
SECTION 9.2: COUNTER
PROGRAMMING
TCON register
TR0 and TR1 flags turn on or off the timers
bits are part of a register called TCON (timer control)
upper four bits are used to store the TF and TR bits of
both Timer 0 and Timer 1
lower four bits are set aside for controlling the interrupt
bits
"SETB TRl" and "CLR TRl
"SETB TCON. 6" and "CLR TCON. 6
42
SECTION 9.2: COUNTER
PROGRAMMING
44
SECTION 9.2: COUNTER
PROGRAMMING
Figure 98 Timer/Counter 0
45
SECTION 9.2: COUNTER
PROGRAMMING
Figure 99 Timer/Counter 1
46
Next
Proteus Exercise 8
Do as much of the Proteus exercise as you can
and submit via MeL before the end of the lecture.
47