Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views9 pages

Timers and Counters

The document discusses using timers and counters in the 8051 microcontroller. It provides theory on how timers function by counting at a frequency determined by the attached crystal. Example programs are given to: (1) generate delays using timers; (2) generate square waves using timers; and (3) count switch presses using counters. C and assembly programs are presented to implement each operation using timers and counters in modes 1 and 2. The programs are simulated using the KEIL IDE to demonstrate the timer and counter functionality.

Uploaded by

Pragathish
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)
21 views9 pages

Timers and Counters

The document discusses using timers and counters in the 8051 microcontroller. It provides theory on how timers function by counting at a frequency determined by the attached crystal. Example programs are given to: (1) generate delays using timers; (2) generate square waves using timers; and (3) count switch presses using counters. C and assembly programs are presented to implement each operation using timers and counters in modes 1 and 2. The programs are simulated using the KEIL IDE to demonstrate the timer and counter functionality.

Uploaded by

Pragathish
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/ 9

EX.

NO:3
REALIZATION OF TIMER AND COUNTER
DATE:
AIM:

To understand the architecture and working of timers/counters in 8051


microcontroller using cross assembler KEIL by realizing the following operation

(i) Generating delays using timers


(ii) Generating square waves using timers
(iii) Counting number of switch presses using counters

REQUIREMENTS:

(i) KEIL µvision 2 IDE


(ii) EDSIM51-DI- 8051 simulator

THEORY:

FUNCTION OF A TIMER:

The registers of Timers are loaded with some initial value. The value of a Timer register
increases by one after every machine cycle. One machine cycle duration is the 1/12th of the
frequency of the crystal attached to the controller.

For example, if the frequency of the crystal is 12 MHz, then the frequency for Timer will
be 1MHz (1/12 of crystal frequency) and hence the time (T = 1/f) taken by the Timer to count
by one is 1µs (1/1MHz). Similarly if an 11.0592 MHz crystal is used, operating frequency of
Timer is 921.6 KHz and the time period is 1.085 µs.

If no value is loaded into the Timer, it starts counting from 0000H.

When the Timer reaches FFFFH, it reloads to 0000H. This roll over is communicated to
the controller by rising a flag corresponding to that Timer, i.e., a flag bit is raised (set high)
when the timer starts counting from 0000H again. TF0 and TF1 are the Timer flags
corresponding to Timers 0 and 1. These flags must be cleared (set low) by software every time
they are raised. The Timer may terminate updating register values after a roll over or continue
with its operation.

TR0 and TR1 are the control bits for Timers 0 and 1 respectively. Setting the control bit
would start the Timer.

19
ASSEMBLY PROGRAM:

ORG 0000H

0000| LJMP 0821H

ORG 0800H

0800| MOV 89H, #01H

0803| MOV 8AH, #0FDH

0806| MOV 8CH, #4BH

0809| SETB 8CH

080B| JNB 8DH, 0FDH

080E| CLR 8CH

0810| CLR 8DH

0812| RET

0813| MOV 90H, #55H

0816| LCALL 0800H

0819| MOV 90H, #0AAH

081C| LCALL 0800H

081F| SJMP 0F2H

0821| MOV R0, #7FH

0823| CLR A

0824| MOV @R0, A

0825| DJNZ R0, 0FDH

0827| MOV 81H,#07H

082A| LJMP 0813H

END

20
(i) Write an 8051 C-program to toggle all the bits of P1 continuously for every 50 ms

ALGORITHM:

Step1: Start

Step2: Initialize port 1 for displaying data

Step3: Display the first data

Step4: Call Delay operation

Step5: Display the second data

Step6: Call the delay operation

Step7: Repeat again from Step2 continuously.

Step8: Stop

C-PROGRAM:

#include<reg51.h>
void T0Delay(void);
void main(void)
{
while(1)
{
P1=0x55;
T0Delay();
P1=0xAA;
T0Delay();
}
}
void T0Delay()
{
TMOD=0x01;
TL0=0xfd;
TH0=0X4b;
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}

21
ASSEMBLY PROGRAM:

ORG 0000H

0000| LJMP 0810H

ORG 0800H

0800| MOV 89H, #20H

0803| MOV 8DH, #48H

0806| SETB 8EH

0808| JNB 8FH, 0FDH

080B| CLR 8EH

080D| CLR 8FH

080F| RET

0810| MOV R0, #7FH

0812| CLR A

0813| MOV @R0, A

0814| DJNZ R0, 0FDH

0816| MOV 81H, #08H

0819| LJMP 081CH

081C| CPL 97H

081E| LCALL 0800H

0821| SJMP 0F9H

END

22
(ii) Write an 8051 C-program to create a frequency of 2500 Hz on pin P1.5.use timer 1
mode 2 to create the delay

ALGORITHM:

Step1: Start

Step2: Set port 1.7 for the operation

Step3: Set the timer 1 to autoreload mode

Step4: When the timer overflows, Timer flag is set

Step5: Toggle port 1.7

Step6: From Step 2 the process repeats in a loop

Step7: Stop.

C-PROGRAM:

#include <reg51.h>
void T1M2delay(void);
sbit outpin=P1^7;
void main(void)
{
unsigned char x;
while(1)
{
outpin=~outpin; //toggle P1.7
T1M2delay();
}
}
void T1M2delay()
{
TMOD=0X20;
TH1=-184;
TR1=1;
while(TF1==0);
TR1=0;
TF1=0;
}

23
ASSEMBLY PROGRAM:

ORG 0000H

0000| LJMP 0818H

ORG 0800H

0800| SETB 0B5H

0802| MOV 89H, #60H

0805| CLR A

0806| MOV 8DH, A

0808| SETB 8EH

080A| MOV A, 8BH

080C| CPL A

080D| MOV 90H, A

080F| JNB 8FH, 0F6H

0812| CLR 8EH

0814| CLR 8FH

0816| SJMP 0F0H

0818| MOV R0, #7FH

081A| CLR A

081B| MOV @R0, A

081C| DJNZ R0, 0FDH

081E| MOV 81H, #07H

0821| LJMP 0800H

END

24
(iii) Assume that a button being connected to the pin TI (P3.5).write a C-program for
counter 1 in mode 2 to count up and display the state of TL1 count on P1.Start the
count at 0H.

ALGORITHM:

Step1: Start

Step2: Set port P3.5 as INPUT port

Step3: Initialize timer1 to count

Step4: Count the number of times button pressed

Step5: Display result in P1

Step6: End

C-PROGRAM:

#include<reg51.h>
sbit switc=P3^5;
void main (void)
{
switc=1;
TMOD=0x60;
TH1=0;

while(1)
{
do
{
TR1=1;
P1=~TL1;
}
while (TF1==0);
TR1=0;
TF1=0;
}
}

25
OUTPUT:

DATA 55H IS PORTED TO PORT1

TF0 IS SET AFTER TIMER OVERFLOWS (i.e. 50ms delay)

26
AFTER 50ms THE NEXT DATA 0AAH IS PORTED TO PORT1

Result:

Thus several C-programs have been written and simulated using the KEIL software to
understand the working and architecture of TIMERS/COUNTERS in 8051

27

You might also like