[go: up one dir, main page]

0% found this document useful (0 votes)
101 views12 pages

8051 Counter Programming Guide

This document discusses using timers as counters on the 8051 microcontroller. It explains that timers can count external pulses by incrementing the TH and TL registers. The C/T bit in the TMOD register determines whether the timer is used as a timer or counter, with C/T=1 designating counter mode. When in counter mode, the timer counts pulses from the T0 and T1 pins. Programming is similar to timer mode except the clock source is external. Examples are provided to count pulses on P3.4 and display the count on ports 1 and 2.

Uploaded by

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

8051 Counter Programming Guide

This document discusses using timers as counters on the 8051 microcontroller. It explains that timers can count external pulses by incrementing the TH and TL registers. The C/T bit in the TMOD register determines whether the timer is used as a timer or counter, with C/T=1 designating counter mode. When in counter mode, the timer counts pulses from the T0 and T1 pins. Programming is similar to timer mode except the clock source is external. Examples are provided to count pulses on P3.4 and display the count on ports 1 and 2.

Uploaded by

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

COUNTER PROGRAMMING

❑ Timers can also be used as counters counting


events happening outside the 8051
➢ When it is used as a counter, it is a pulse outside of
the 8051 that increments the TH, TL registers
➢ TMOD and TH, TL registers are the same as for the
timer discussed previously
❑ Programming the timer in the last section also
applies to programming it as a counter
➢ Except the source of the frequency
❑ The C/T bit in the TMOD register decides the
source of the clock for the timer
➢ When C/T = 1, the timer is used as a
counter and gets its pulses from outside the
8051
▪ The counter counts up as pulses are fed
from pins 14 and 15, these pins are called
T0 (timer 0 input) and T1 (timer 1 input)
• One of the pins (if needed)P3.4(Timer0) or
P3.5 (Timer1) will be kept as Timer and the
other as Counter in either Mode0,1, or 2.
• The Steps followed is the same for Timer and
Counter. (If not specified set in Mode 1)
• The steps for programming Counter is same,
with initial value as 0000H.
Design a counter for counting the pulses of an input
signal every second on P3.4 and display the count value
on Port 1 and 2, XTAL =22MHz
• P3.4 – Timer0 (as Counter)
• Assume mode1
• P3.4-set as input pin
• We need a 1 sec timing to be generated and
this is done by using Timer1 in mode1.
• Max delay by Timer in Mode1=(FFFF+1)x
(12/XTAL freq)
• =0.0357 sec
• 0.0357x =1s
• x=28
• This is repeated 28 times to get 1 sec delay
• TMOD
• 00010101=15H
• Initial Value
• In counter =0000 (Timer0)
• In Timer =0000 (Timer1)
• ORG 0000H
• MOV TMOD,#15H
• SET B P3.4 - set P3.4 as input pin
• REP MOV TL0,#00H
• MOV TH0,#00H
• SETB TR0 -start counter
• MOV R0,#28
• AGAIN MOV TL1,#00H
• MOV TH1,#00H
• SETB TR1 - Start Timer
• WAIT JNB TF1,WAIT
• CLR TF1
• CLR TR1
• DJNZ R0,AGAIN
• CLR TR0
• MOV A,TL0
• MOV P2,A
• MOV A,TH0
• MOV P1,A
• SJMP REP
• END
• ORG 0000H • #include<reg51.h>
• sbit inpin=P3^4;
• MOV TMOD,#15H • void main()
• SET B P3.4 • {
• REP MOV TL0,#00H • unsigned char i;
• TMOD=0X15;
• MOV TH0,#00H • inpin=1;
• SETB TR0 • while(1)
• MOV R0,#28 • {
• TL0=0x00;
• AGAIN MOV TL1,#00H • TH0=0x00;
• MOV TH1,#00H • TR0=1;
• SETB TR1 • for(i=0;i<28;i++)
• {
• WAIT JNB TF1,WAIT • TL1=0x00;
• CLR TF1 • TH1=0x00;
• CLR TR1 • TR1=1;
• while(TF1==0);
• DJNZ R0,AGAIN • TR1=0;
• CLR TR0 • TF1=0;
• MOV A,TL0 • }
• TR0=0;
• MOV P2,A • P1=TH0;
• MOV A,TH0 • P2=TL0;
• MOV P1,A • }
• }
• SJMP REP
• END
Consider that a 1Hz external clock is connected to
8051. Write a program for counter0 in Mode1 to count
pulses and display the count value continuously on P2
and P1
• TMOD
• 00000101=05H
• MOV TMOD,#05H
• SETB P3.4
• MOV TL0,#00H
• MOV TH0,#00H
• SETB TR0
• AGAIN MOV A,TL0
• MOV P1,A
• MOV A,TH0
• MOV P2,A
• SJMP AGAIN
• MOV TMOD,#05H • #include<reg51.h>
• SETB P3.4 • sbit inpin=P3^4;
• MOV TL0,#00H • void main()
• MOV TH0,#00H • {
• SETB TR0 • inpin=1;
• AGAIN MOV A,TL0 • TL0=0x00;
• MOV P1,A • TH0=0x00;
• MOV A,TH0 • TR0=1;
• MOV P2,A • while(1)
• SJMP AGAIN • {
• P1=TL0;
• P2=TH0;
• }
• }

You might also like