Professional Documents
Culture Documents
Important NOTE
•TAKE NOTES IN RECORD (Can strike
also, no marks will be reduced)
•Enter your registration number and
Full Name next to
1) all the plots and
2) all the programs.
W4.E4 8051 Keil –Assembly programming with Timers/Counters
ASSEMBLY
PROGRAMMING WITH
Timers/Counters OF
8051
W4.E4 8051 Keil –Assembly programming with Timers/Counters
Timers/Counters
The 8051 has two timers/counters (T0 & T1) , they can be used
either as
To generate waveform signals with specific frequency
Timers to generate a time delay
Event counters to count events happening outside the
microcontroller
‰
Both Timer 0 and Timer 1 are 16 bits wide
W4.E4 8051 Keil –Assembly programming with Timers/Counters
Timers/Counters
How timer works?
W4.E4 8051 Keil –Assembly programming with Timers/Counters
Timers/Counters
Registers associated with 8051 timers are:
Timers/Counters
Timer registers (T0 & T1)
W4.E4 8051 Keil –Assembly programming with Timers/Counters
Timers/Counters
To calculate the values to be loaded into the TL and TH registers
(Assume XTAL = 11.0592 MHz)
1. Divide the desired time delay by 1.085 us
2. Perform 65536 – n, where n is the decimal value we got in
Step1
3. Convert the result of Step2 to hex, where yyxx is the initial hex
value to be loaded into the timer’s register
4. Set TL = xx and TH = yy To generate a time delay
W4.E4 8051 Keil –Assembly programming with Timers/Counters
Timers/Counters
Example: 250us time delay
Step2: P=65536-230=65306
Timers/Counters
TMOD Register
W4.E4 8051 Keil –Assembly programming with Timers/Counters
Timers/Counters
TCON Register
W4.E4 8051 Keil –Assembly programming with Timers/Counters
Timers/Counters
To generate a time delay
1. Load the TMOD value register indicating which timer (timer 0
or timer 1) and timer mode (0 or 1) is selected
2. Load registers TL and TH with initial count value
3. Start the timer using SETB TRx
4. Keep monitoring the timer flag (TF) with the JNB TFx,target
instruction
5. Stop the timer using CLR TRx
6. Clear the TF flag for the next round
7. Go back to Step 2 to load TH and TL again
W4.E4 8051 Keil –Assembly programming with Timers/Counters
W4.E4 8051 Keil –Assembly programming with Timers/Counters
W4.E4 8051 Keil –Assembly programming with Timers/Counters
MODE 1
MODE 2
W4.E4 8051 Keil –Assembly programming with Timers/Counters
where X represents the number of times the whole range from 0000
to FFFF needs to be repeated to generated an overall delay of 1
second (1000 ms).
X = 1000/71.1065 = 14
W4.E4 8051 Keil –Assembly programming with Timers/Counters
Challenging Tasks - 1
Write an 8051 assembly language program using timers to generate
a frequency of 10kHz on pin port pin P2.7. Assume the crystal
frequency as 11.0592 MHz.
W4.E4 8051 Keil –Assembly programming with Timers/Counters
Challenging task - 2
Write an 8051 assembly language program using timers to blink the
LED connected at P1.7 TON of 0.75 seconds and TOFF of 0.25
seconds. Assume the crystal frequency as 11.0592 MHz.
W4.E4 8051 Keil –Assembly programming with Timers/Counters
Challenging Tasks - 3
Write an 8051 assembly language program to
implement a counter for counting pulses of an input signals.
Assume the crystal frequency as 11.0592 MHz. Configure TIMER 1 to
generate a clock pulse for every one seconds and TIMER 0 as a
counter which receives input pulses at P3.4 from P3.5. Display final
count values in port P1 (TL0) & P2(TH0).
W4.E4 8051 Keil –Assembly programming with Timers/Counters