0% found this document useful (0 votes)
66 views

Objectives:: Timers/Counters

This document provides code examples for using timers and counters on the 8051 microcontroller. It includes: 1) A program that generates a 500Hz square wave using Timer 0 by setting the timer reload value to 922 decimal. 2) A program that generates a 1.83kHz square wave using Timer 1 by loading the timer high value with 5. 3) A program that uses Counter 1 in mode 2 to count external clock pulses on pin T1 and display the current count on port P2 LEDs. It starts and stops the counter using the TR1 bit.

Uploaded by

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

Objectives:: Timers/Counters

This document provides code examples for using timers and counters on the 8051 microcontroller. It includes: 1) A program that generates a 500Hz square wave using Timer 0 by setting the timer reload value to 922 decimal. 2) A program that generates a 1.83kHz square wave using Timer 1 by loading the timer high value with 5. 3) A program that uses Counter 1 in mode 2 to count external clock pulses on pin T1 and display the current count on port P2 LEDs. It starts and stops the counter using the TR1 bit.

Uploaded by

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

Timers/Counters

OBJECTIVES:

 To generate a square wave using the 8051 timer.


 To count external events using counter
REFERENCE:
 Mazidi and McKinlay, “The 8051 Microcontroller and Embedded Systems,”
Chapter 9.

MATERIALS:
 8051 KEIL IDE

Program 1

Write a program using timer 0 to generate a 500 Hz square wave


frequency on one of the pins of P1. Then examine the frequency using the KEIL
IDE inbuilt Logic Analyzer.
Calculation
Given,
F = 500Hz
Step 1:
T = 1/F =1/500 =0.002 Sec
Step 2 :
Take T/2 (i.e) 0.001 Sec
Step 3:
0.001/1.085Micro Sec =921.6=> 922
Step 4:
65536 -922 =(64614)10 => (FC66)16

Code:

ORG 0000H
MOV TMOD,#01H ; Timer 0 Mode1
HERE:MOV TL0,#66H
MOV TH0,#0FCH TF0=0 TF0 =1
CPL P1.0
ACALL DELAY (FC66,FC67,FC68,….. FFFF) 0000
SJMP HERE
DELAY:SETB TR0
AGAIN:JNB TF0,AGAIN
CLR TR0
CLR TF0
RET
END

Program 2

Write a program using timer 1 to generate a 1.83 kHz square wave


frequency on one of the pins of P1. Then examine the frequency using the
oscilloscope. Use Mode2 Timer1

Code:
MOV TMOD,#20H
MOV TH1,#5
SETB TR1
BACK: JNB TF1,BACK
CPL P1.0
CLR TF1
SJMP BACK
END

Program 3

Assuming that clock pulses are fed into pin T1, write a program for counter 1 in
mode 2 to count the pulses and display the state of the TL1 count on P2, which
connects to 8 LEDs.
Code:

MOV TMOD,#60H
MOV TH1,#0 ;clear TH1
SETB P3.5 ; make T1 input
AGAIN: SETB TR1 ; start the counter
BACK: MOV A,TL1 ;get copy of TL
MOV P2,A ;display it on port 2
JNB TF1,Back ;keep doing, if TF = 0
CLR TR1 ; stop the counter 1
CLR TF1 ; make TF=0
SJMP AGAIN
END

You might also like