Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

TIMERS AND COUNTERS INTERFACING IN 8051

Hammad Khan (191216)


_________________________

BACHELOR OF ELECTRICAL ENGINEERING


Fall-19

Submitted To:
Engr. Mariam Sabir
Lab Engineer
____________

Department of Electrical Engineering


FACULTY OF ENGINEERING
AIR UNIVERSITY,ISLAMABAD
AIR UNIVERSITY
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING

EXPERIMENT NO 6

Lab Title: TIMERS AND COUNTERS INTERFACING IN 8051

Student Name: Hammad Khan Reg. No: 191216

Objective: To get familiarized with timer T0 and time T1. And Getting introduced to interrupt
handling, Generating a square wave using timers and interrupts
LAB ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes (5) (4) (3) (2) (1)

Ability to Conduct
Experiment
Ability to assimilate the
results
Effective use of lab
equipment and follows
the lab safety rules

Total Marks: Obtained Marks:

LAB REPORT ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes
(5) (4) (3) (2) (1)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks:

Date: 24-11-2021 Signature:


LAB#02
TIMERS AND COUNTERS INTERFACING IN 8051
Objective:
• To get familiarized with timer T0 and time T1.
• Getting introduced to interrupt handling.
• Generating a square wave using timers and interrupts.

Equipment:
Hardware:
1. 89c51 microcontroller
2. LEDs
Software:
1. Keil u-vision
2. Smart-pro 5000u

Discussion:
TIMERS: The 8051 comes equipped with two timers, both of which may be controlled, set, read,
and configured individually. The 8051 timers have three general functions:
1) Keeping time and/or calculating the amount of time between events
2) Counting the events themselves, or 3) Generating baud rates for
the serial port
Timer SFRs:
As mentioned before, the 8051 has two timers which each function essentially the same way. One
timer is TIMER0 and the other is TIMER1. The two timers share two SFRs (TMOD and TCON)
which control the timers, and each timer also has two SFRs dedicated solely to itself (TH0/TL0
and TH1/TL1).

We have given SFRs names to make it easier to refer to them, but in reality an SFR has a numeric
address. It is often useful to know the numeric address that corresponds to an SFR name. The SFRs
relating to timers are:
When you enter the name of an SFR into an assembler, it internally converts it to a number.
For example, the command:
MOV TH0,#25h
moves the value 25h into the TH0 SFR. However, since TH0 is the same as SFR address
8Ch this command is equivalent to:
MOV 8Ch,#25h
The TMOD SFR
Let’s first talk about our first control SFR: TMOD (Timer Mode). The TMOD SFR is used to
control the mode of operation of both timers. Each bit of the SFR gives the microcontroller specific
information concerning how to run a timer. The high four bits (bits 4 through 7) relate to Timer 1
whereas the low four bits (bits 0 through 3) perform the exact same functions, but for timer 0. The
individual bits of TMOD have the following functions: TMOD (89h) SFR

THE TCON SFR


Finally, there’s one more SFR that controls the two timers and provides valuable information
about them. The TCON SFR has the following structure:

TCON (88h) SFR


LAB TASKS
Write a program to generate 1ms delay using 8051 timer.
Program Code
#include<reg51.h>
sbit on=P1^0; void
delay(){
TH0=0xFC;
TL0=0x66; TR0=1;
while(TF0==0);
TF0=0;
TR0=0;} void
main(){
TMOD=00000001;
while(1){
on=1; delay();
on=0;
delay();}
}

Proteus Circuit
Task#02
Generate a square wave with frequency 1kHz and 50%duty cycle by using Timer 0 in mode
1. (In Assembly Language)

Program Code
ORG 00H
MOV TMOD ,#00000001B MAIN:
SETB P1.0
LCALL DELAY
CLR P1.0
LCALL DELAY
SJMP MAIN DELAY:
MOV TH0 ,#0FCH
MOV TL0 ,#18H
CLR TF0
SETB TR0 HERE:
JNB TF0,HERE
CLR TR0
CLR TF0
RET
END
Proteus Circuit
Task#03
Generate a square wave with frequency 50Hz with 70%duty cycle by using Timer 1 in
mode 1. (In Assembly Language)

Code
ORG 00H
MOV TMOD ,#00010000B MAIN:
SETB P1.0
LCALL DELAY1
CLR P1.0
LCALL DELAY2
SJMP MAIN DELAY1:
MOV TH1 ,#0CDH
MOV TL1 ,#99H
CLR TF1
SETB TR1 HERE1:
JNB TF1,HERE1
CLR TR1
CLR TF1
DELAY2:
MOV TH1 ,#0EAH
MOV TL1 ,#66H
CLR TF1
SETB TR1 HERE2:
JNB TF1,HERE2
CLR TR1
CLR TF1
RET
END

Proteus Circuit
Learning Outcomes:
In this lab, we learnt about timers in 8051 microcontroller. The 8051 comes equipped with two
timers, both of which may be controlled, set, read, and configured individually. The 8051 timers
have three general functions:
• Keeping time and/or calculating the amount of time between events
• Counting the events themselves, or
• Generating baud rates for the serial port we also learnt about timers SFR’s such as
TMOD and TCON and how we can use them in micro controller and get desired results and
waveforms with certain delays.

You might also like