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

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING

EXPERIMENT NO#09

Lab Title: TIMERS AND COUNTERS INTERFACING IN 8051


Student Name: Osama Anees Mirza, Rayyn Munir Reg.No: 210286, 210301

Objective: Getting introduced to timer T0 , T1, interrupt handling and how to


Generate 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: Signature:

1
Department of Electrical and Computer Engineering
Air University Islamabad
5th Semester
Microprocessors & Microcontrollers
Lab Report#9
TIMERS AND COUNTERS INTERFACING IN 8051
Osama Anees Mirza Rayyan Munir
210286 210301
12 December 2023

2
Contents
1 Objectives: 4

2 Equipment: 4

3 Introduction: 4

4 Task#1 4
4.1 Code: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.2 Wave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

5 Task#2 5
5.1 Code: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5.2 Wave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

6 Task#3 6
6.1 Code: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
6.2 Wave: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

7 Task#4 8
7.1 Code: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
7.2 Wave: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

8 Conclusion 9

3
1 Objectives:
1. Getting introduced to the timer T0 and T1.
2. Getting introduced to interrupt handling.
3. Generating a square wave using timers and interrupts.

2 Equipment:
• Hardware:
– AT89C51 microcontroller
– AT89C51 development board
– Oscilloscope
– Digital Multi Meter (DMM)
• Software:
– Keil µVision
– Proteus
– Smart-pro 5000u
– A Working Computer.

3 Introduction:
The 8051 microcontroller features two timers, TIMER0 and TIMER1, controlled by shared Special Function Regis-
ters (SFRs) such as TMOD, TCON, and individual registers TH0/TL0 and TH1/TL1. TMOD (Timer Mode) SFR
configures the timers’ modes, with Timer 0 and Timer 1 having separate control bits. The high four bits of TMOD
correspond to Timer 1, while the low four bits control Timer 0. Timer modes include a 13-bit timer, a commonly used
16-bit timer, an 8-bit auto-reload mode, and a split timer mode where Timer 0 and Timer 1 function independently.
The TCON (Timer Control) SFR provides information about timer overflows and whether each timer is running,
controlled by bits such as TF1, TR1, TF0, and TR0. Initializing a timer involves setting the appropriate bits in
TMOD to select the desired mode and then activating the timer by setting the corresponding TRx bit.

For example, to initialize Timer 0 as a 16-bit timer in continuous mode, one would execute the instruction MOV
TMOD,#01h to set the necessary bits in TMOD. Subsequently, starting the timer involves executing SETB TR0,
which sets the TR0 bit and initiates Timer 0. This enables the timer to count continuously, incrementing once every
machine cycle. Understanding the various timer modes and control bits is crucial for efficient timer utilization in 8051
microcontroller programming, providing capabilities such as timekeeping, event counting, and baud rate generation.

4 Task#1
Write a program to generate 1ms delay using 8051 timer.

4.1 Code:

1 ; 0.5 High
2 ; 0.5 Low
3 ; 1mc = 12/(11.0592 Mhz) = 1.085069444 us
4 ; 1ms = 1000us
5 ; Machine Cycles Needed = (500/1.085069444) = 460.800000189
6 ; MC = 461
7 org 000h
8 main:
9 clr TR0
10 clr TF0
11 clr P1.0
12 mov THMOD,#00000001b
13 start:
14 mov TH0,#High(-416)

4
15 mov TL0,#Low(-416)
16 setb TR0
17 wait: jnb TF0,wait
18 clr TR0
19 cpl P1.0
20 clr TF0
21 sjmp start

Listing 1: 8051 Assembly Code Task1

4.2 Wave

Figure 1: Virtual Oscilloscope 1000KHz

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

5.1 Code:

1 ; T = ((1/(1*10^3))*10^6) = 1000us
2 ; High = 500us ; Low = 500us
3 ; 1mc = 12/(11.0592 Mhz) = 1.085069444 us
4 ; Machine Cycles Needed = (500/1.085069444) = 460.800000189
5 ; MC = 461
6 org 000h
7 SJMP main
8 org 00BH
9 JMP T0_ISR
10 org 0030h
11 main:
12 clr TR0

5
13 clr TF0
14 clr P1.0
15 mov THMOD,#00000001b
16 mov IE,#10000010b
17 mov TH0,#High(-416)
18 mov TL0,#Low(-416)
19 setb TR0
20 wait: sjmp wait
21 T0_ISR:
22 clr TR0
23 clr TF0
24 cpl P1.0
25 mov TH0,#High(-416)
26 mov TL0,#Low(-416)
27 set TR0
28 reti

Listing 2: 8051 Assembly Code Task2

5.2 Wave

Figure 2: Virtual Oscilloscope 1000KHz

6 Task#3
Generate a square wave with frequency 50Hz with 75%duty cycle by using Timer 1 in mode 1. (In Assembly
Language)

6.1 Code:

1 ; T = ((1/(1*50))*10^6) = 20000us
2 ; High = 15000us ; Low = 5000us

6
3 ; 1mc = 12/(11.0592 Mhz) = 1.085069444 us
4 ; Machine Cycles Needed High = (15000/1.085069444) = 13824.000005662
5 ; Machine Cycles Needed Low = (5000/1.085069444) = 4608.000001887
6 ; High MC = 13825
7 ; Low MC = 4609
8 org 000h
9 main:
10 clr TR1
11 clr TF1
12 clr P1.0
13 mov THMOD,#00010000b
14 start:
15 mov TH1,#High(-13825)
16 mov TL1,#Low(-13825)
17 setb TR1
18 wait: jnb TF1,wait
19 clr TR1
20 cpl P1.0
21 clr TF1
22
23 mov TH1,#High(-4609)
24 mov TL1,#Low(-4609)
25 setb TR1
26 wait2: jnb TF1,wait2
27 clr TF1
28 clr TR1
29 sjmp start
30 END

Listing 3: 8051 Assembly Code Task3

6.2 Wave:

Figure 3: Proteus Simulation

7
7 Task#4
Generate Square wave with the frequency of 1 KHz with 50% duty cycle using timer 1 in mode 2.

7.1 Code:

1 ; T = ((1/(1*10^3))*10^6) = 1000us
2 ; High = 500us ; Low = 500us
3 ; 1mc = 12/(11.0592 Mhz) = 1.085069444 us
4 ; Machine Cycles Needed = (500/1.085069444) = 460.800000189
5 ; MC = 461
6 ; The Question Says To Use Mode 2 Which Is 8 bit Auto Reload But In Order To Use That I Need MC Less Than 255
7 ; Which Is The Maximum Value That An 8 Bit Number Can Have. So I Am Using 16 Bit Mode Which is Mode 01
8 org 000h
9 SJMP main
10 org 00BH
11 JMP T0_ISR
12 org 0030h
13 main:
14 clr TR1
15 clr TF1
16 clr P1.0
17 mov THMOD,#b
18 mov IE,#10001000b
19 mov TH1,#High(-416)
20 mov TL1,#Low(-416)
21 setb TR01
22 wait: sjmp wait
23 T0_ISR:
24 clr TR1
25 clr TF1
26 cpl P1.0
27 mov TH1,#High(-416)
28 mov TL1,#Low(-416)
29 set TR1
30 reti

Listing 4: 8051 Assembly Code Task4

8
7.2 Wave:

Figure 4: Virtual Oscilloscope 1000KHz

8 Conclusion
In this lab, we gained valuable insights into the intricacies of interfacing timers and counters with the 8051 micro-
controller. The primary objectives were successfully achieved, starting with an introduction to Timer 0 and Timer
1, delving into interrupt handling, and culminating in the practical application of generating a square wave.

We navigated the Timer Special Function Registers (SFRs) landscape, understanding the roles of TMOD, TCON,
TH0/TL0, and TH1/TL1 in controlling and configuring timers. The discussion provided a comprehensive overview
of different timer modes, emphasizing their distinct functionalities and applications. We explored 13-bit, 16-bit,
auto-reload, and split-timer modes, unraveling their significance in diverse scenarios.

The lab tasks further solidified our understanding, requiring the implementation of programs in Assembly Language.
We successfully created a 1ms delay, generated square waves with specific frequencies and duty cycles using Timer
0 and Timer 1, and explored the nuances of auto-reload mode.

In summary, this lab equipped us with practical skills in utilizing timers for diverse timing and counting applications,
laying a solid foundation for advanced microcontroller programming and embedded system development.

You might also like