Frequency Counter using 8051 Microcontroller

Updated on 2017/08/11 17:08


  • Design of Frequency counter

Introduction to Frequency Counter

When the Timer/Counter is used as a timer,the 8051's crystal is used as the source of frequency. When it is used as a counter,However,it is the pilse outside the 8051 that increments the TH,TL registers. In counter mode,the TMOD,TH,TL registers are same as for the timer.

Timer Mode Control Register - TMOD

7 (MSB)6543210 (LSB)

M1 and M0 specify the mode as follows: If C/T = 1, the timers function as counters to count  the negative transitions at T0 or T1 pins.

  • If C/T = 0, the timers function as timers, that is, they basically count the number of machine cycles.
  • Gate = 0 means that the timer is controlled by TR1 or TR0 only, irrespective of INT0 or INT1.
  • Gate = 1 means that the timer control will depend on INT0 or INT1 and also on TR0 or TR1 bits
  • When data is written it gets latched.
  • TMOD is used for setting mode bits M1, M0
  • Gate bit and C/T for Timer 0 and Timer 1.
  • Bit 0 to 3 for Timer 0.
  • Bit 4 to 7 for Timer 1.
Port 3 Pins used for Timers 0 and 1
Pin NoPort PinFunctionDescription
14P3.4T0Timer/Counter 0 external Input
15P3.5T1Timer/Counter 1 external Input

Interfacing Diagram

Question: Design a counter for counting the pulses of an input signal. The pulses to be counted are fed to pin P3.4.XTAL Freq. = 22MHz


Solution: In this, Timer1 is used as base for timing 1 second. During this 1 second, Timer0 is run as a counter, with input pulses fed into pin P3.4. At the end of 1 second, the values in TL0 and TH0 give the number of pulses that were received at pin P3.4 during this 1 second. This gives frequency of the unknown signal,i .e., the number of pulses received in 1 second.

ALP for the frequency counter

Start: MOV TMOD,#15H   ;timer 1 as timer and Timer0 as counter
SETB P3.4              ;set P3.4 as input pin
MOV TL0,#00H           ;clear TL0
MOV THO,#00H           ;CLEAR TH0
SETB TR0                ;Start counter
MOV R0,#28              ;R0=28,for time=1 sec
Again:MOV TL1,#00H     ;
MOV TH1,#00H
SETB TR1                ;Start timer 1
Back:JNB TF1,Back       ;monitor timer1 overflow flag
CLR TF1             ;monitor timer1 overflow flag
DJNZ R0,Again          ;repeat the loop until R0=0
MOV A,TL0            ;1sec time period is finished now save count from TLO in A
MOV P2,A             ;send it to port2
MOV A,TH0            ;move TH0 into A
MOV P1,A          ;Send it to port P1
SJMP Start


  • Created and edited by Prof. S.M.Wagh, SKNCOE, Pune
  • WikiNote Foundation
Created by Sujit Wagh on 2017/08/03 17:00