0% found this document useful (0 votes)
26 views17 pages

LAB 10 8051 Timers Part 1

Uploaded by

tanveer1111110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views17 pages

LAB 10 8051 Timers Part 1

Uploaded by

tanveer1111110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

LAB 10

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)

 Generating baud rate for serial port

Note: 8051 Timers always count up


SPECIAL FUNCTION REGISTERS (SFR)

SFR Name Description SFR


Address
TH1 Timer 1 High byte 8Dh
TL1 Timer 1 Low byte 8Bh
TH0 Timer 0 High byte 8Ch
TL0 Timer 0 Low byte 8Ah
TMOD Timer Mode 89h
TCON Timer Control 88h
MODES OF OPERATION TMOD

TxM1 TxM0 Timer Mode Description of Mode

0 0 0 13 Bit Timer

0 1 1 16 Bit Timer

1 0 2 8 Bit Auto-Reload

1 1 3 Split Timer Mode


TIMERS PROGRAMMING-EXAMPLE
A program using timer 0 to create 2 kHz wave
of 50% duty cycle on P0.0

wave of 2kHz
Time period = 1/2000 = 0.5milisecond =500 micro second

50% duty cycle => ON/OFF-Time =500/2 = 250


256 – 250 = 6
TH = TL=6
TIMERS PROGRAMMING-EXAMPLE
A program using timer 0 to create 2 kHz
square wave of 50% duty cycle on P0.0

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

30% duty cycle => ON-Time = 0.3 x 1000 = 300


65536 – 300 = 65236 = FED4h
TH = FEh, TL=D4h

70% duty cycle => Off-Time = 0.7 x 1000 = 700


65536 – 700 = 64836 = FD44h
TH = FDh, TL=44h
TIMERS PROGRAMMING-EXAMPLE
A program using timer 0 to create 1 kHz square wave of 30% duty cycle on
P0.0

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

You might also like