LAB 10 8051 Timers Part 1
LAB 10 8051 Timers Part 1
8051 TIMER
PROGRAMMING
INTRODUCTION
The 8051 has two timers/ counters. They can be
controlled, set, read and configured individually.
The 8051 Timers have four general functions:
Keeping time (T), used to create delays (Normal
Clock)
Calculating the amount of time between events
(Gate) (stop-watch)
Counting the events (C) (Counter)
0 0 0 13 Bit Timer
0 1 1 16 Bit Timer
1 0 2 8 Bit Auto-Reload
wave of 2kHz
Time period = 1/2000 = 0.5milisecond =500 micro second
ORG 00H
MAIN: MOV TMOD, #02H ; use timer 0 in 8-bit auto reload mode
MOV TH0, #6 ; initialize timer 0 registers
MOV TL0, #6
SETB TR0 ; start timer 0
START:
CPL P0.0
LOOP: JNB TF0, LOOP
CLR TF0
JMP START
END
TIMERS PROGRAMMING-EXAMPLE
A program using timer 0 to create 1 kHz wave
of 30% duty cycle on P0.0
wave of 1kHz
Time period = 1/1000 = 1milisecond =1000 micro second
ORG 00H
MAIN: MOV TMOD, #01H ; use timer 0 in 16-bit mode
START: MOV TH0, #0FEH ; initialize timer 0 register
MOV TL0, #0D4H
SETB P0.0
SETB TR0 ; start timer 0
LOOP: JNB TF0, LOOP
CLR TF0
CLR TR0 ; stop timer 0
MOV TH0, #0FDH
MOV TL0, #044H
CLR P0.0
SETB TR0
LOOP2: JNB TF0, LOOP2
CLR TF0
CLR TR0
JMP START
END
TIMERS PROGRAMMING-EXAMPLE
A program using timer 1 to create 10 kHz square wave of 80% duty cycle
on P0.0
ORG 00H
MOV TMOD, #20H ;use timer 1 in 8-bit auto reload mode
START: MOV TH1, #0B0H ; initialize timer 1 register
MOV TL1, #0B0H
SETB P0.0
SETB TR1 ; start timer 1
LOOP: JNB TF1, LOOP
CLR TF1
CLR TR1 ; stop timer 1
MOV TH1, #0ECH
MOV TL1, #0ECH
CLR P0.0
SETB TR1
LOOP2: JNB TF1, LOOP2
CLR TF1
CLR TR1
JMP START
END
TIMERS PROGRAMMING-EXAMPLE
A program using timer 1 to toggle P1 after 1 ms delay continuously
ORG 00H
START:
MOV P1, #00
CALL DELAY
MOV P1, #0FFH
CALL DELAY
JMP START
DELAY:
MOV TMOD, #10H ; use timer 1 in 16-bit mode
MOV TH1, #0FCH ; initialize timer 1 register
MOV TL1, #18H
SETB TR1 ; start timer 1
LOOP:
JNB TF1, LOOP
CLR TF1
CLR TR1 ; stop timer 1
RET
END
TIMERS PROGRAMMING-EXERCISE
Write a program to toggle P1.1 after 150 micro second
delay using timer 0. Use crystal frequency 11.0592
MHz
TIMERS PROGRAMMING-EXERCISE
Take parallel input from port P1 convert it into serial
and send it via P0.0 with 1ms delay using timers
TIMERS PROGRAMMING-EXERCISE
Generate a PWM of 100 Hz with 70% duty cycle at pin
P1.0 of 8051 using timer. Use crystal frequency
11.0592 MHz