Sie sind auf Seite 1von 4

World Academy of Science, Engineering and Technology 42 2008

Implementation of PIC based Digital

Frequency Counter
Wai Phyo Aung

accurately measure low frequencies instead of frequency

Digital frequency counters are the most accurate and
flexible instruments available for measuring unknown
frequencies. Frequencies from dc to the giga-hertz range can
be measured with digital frequency counters. In this thesis, the
author used the most advanced microcontroller to build the
digital frequency counter. Most modern microcontrollers have
the following built-in features:
1. Built-in monitors /debugger program
2. Built-in program memory program-ming from a direct host
3. Interrupt capability (from a variety of sources)
4. Analog input and output
5. Serial input and output
6. Bus / external memory interfaces (for RAM and ROM)
PIC microcontroller has become a real powerhouse with
regards to available support. In many ways, the PIC
microcontroller has more support available than other
electronics device. In this research, the PIC 16F84A is used to
construct a high resolution frequency counters which can
measure up to 30 MHz.

AbstractThe main process of this research is to display the

frequency ranges of input frequency. Any desired input frequency
from 10Hz to 30 MHz can be counted and displayed using Peripheral
Interfacing Controller (PIC) 16f84A and seven digits of Seven
Segment Light emitting diodes (LED) display. The input frequency is
counted by PIC16F84A and the output from this PIC pins are
decoded by analog multiplexer, 4051 Integrated Circuit (IC). Seven
digits are displayed by passing seven NPN Bipolar Junction
transistors (BJT), BC547.The main desired output is the high
resolution display in MHz range of frequency. Studying the PIC
Microcontroller, seven segments LED display and the decoders are
included in this research. PIC Assembler Software Techniques are
also implemented with this research. In this research, the complete
design of the advanced frequency Counter is provided. By the help of
components, such as, transistors, diodes, the hardware and software
technology are combined and developed in this project.

KeywordsInput frequency, Peripheral Interfacing Controller

(PIC), Seven Segment Light emitting diodes (LED), 4051 Integrated
Circuit (IC), BC547 BJT, Assembler Software Techniques, hardware
and software technology.

N several occasions counting is required, but manual

counting becomes time-consuming and inaccurate when
objects to be connected are very large in number or they pass
through in quick succession. In such situations, counter using
electronic circuits are more reliable then manual counting. A
counter with suitable connectivity can work as a stopwatch, or
as frequency counter.
Various types of frequency counter may have analogue or
digital forms. Some of the instruments commonly used for
measuring various frequency ranges are, Oscilloscopes, Wein
Bridge Frequency Meters, Zero-beat Frequency Meters, Direct
reading Frequency Meters, Binary Frequency Counters,
Digital Frequency Counters and University time-counters.
The oscilloscope can measure a wide range of frequencies.
However, the accuracy of oscilloscope is somewhat limited.
The Wein Bridge Frequency Meter is a device designed only
to produce accurately known audio-frequency signals. Zerobeat Frequency Meter is used to measure the radio-frequency
range. The University time-counter can be used to measure
both frequency and time intervals. But it can be only used to


The frequency counter to be designed in this research is the
PIC 16F84A based 10 Hz-30 MHz range frequency counter.
The most advanced technique of microcontroller is used to
design the HF frequency counter. There are many PIC
microcontrollers such as PIC 16C54, PIC 16F84, PIC 16F628
and PIC 16F877 that can operate as frequency counter. In this
research, the frequency counter using PIC 16F84A is used.
The block diagram of this frequency counter is shown in Fig.

Manuscript received August 15, 2008. This work was supported in part by
the Ministry of Science and Technology, Union of Myanmar.
Wai Phyo Aung is with the Mandalay Technological University, Mandalay,
Myanmar (phone: 095-2-88704(Electronic Engineering Department), fax:
095-2-88702 (Office,MTU), e-mail:

Fig. 1 Block Diagram of PIC 16F84A based Frequency Counter


World Academy of Science, Engineering and Technology 42 2008

+5V is to it at pin 3, 4 and the desired requirement of 7 digits

can get through 7 transistors of BC547. The circuit of this IC
is shown in Fig 3.
Three PORTA pins, RA0 to RA2 are connected to 4051
Decoder and these 3-bytes outputs are multiplexed in this
Decoder. The seven output pins of the 4051 Decoder are fed
into 7 x Common Cathode 7 segment LED display at common
pin. The main process of 4051 Decoder is that to convert the
incoming 3- bytes from PIC to Digit Numbers. This IC is the
analog 8-channel multiplexer type decoder.

The HF input is fed into the PIC at pin 2 and pin 3 (RA3
and RA 4) by passing through the 470 resistor and this input
signal is amplified by a BC 547 Transistor, an inductor, etc.
The supply voltage for two circuits: PIC and 4051 Decoder
are +5 V DC. The incoming signal is measured by PIC
16F84A with the help of software. Three PORTA pins, RA0
to RA2 are connected to 4051 Decoder and these 3-bytes
outputs are multiplexed in this Decoder. The seven output pins
of the 4051 Decoder are fed into seven number of Common
Cathode 7 segments LED display at common pin. The main
process of 4051 Decoder is that to convert the incoming 3bytes from PIC to Digit Numbers. The anode pins of 7 x
Common Cathode 7 segment LED display are connected with
the PORTB pins (RB7:RB0) of PIC microcontroller. The
cathode pins are grounded and connected to 4051 Decoder
passing through 7 x NPN Si-Transistors.
The power supply circuit is built to convert the AC 220 V
to the required +5 V DC, the actual requirement is between
+2.5 and +5 V DC. The Transformer converts 220 V to 12 V
AC. This 12 V is changed to the required +5 V by using 7805
IC with the help of two capacitors.
In this application, the PIC 16F84A operated as a frequency
counter which can read frequencies from 10 Hz to 30 MHz. It
is used the method of measuring the 24-bits (3-bytes) counter
value from the prescaler, TMR0 (timer 0 module) and some
other registers, such as Option Register. The basic hardware
for the measurement circuit is depicted in Fig. 2. It consists of
the frequency input at TMR0 or TOCKI (pin3 in a PIC
16F84A). This pin is connected to RA3 and the input
frequency is connected to TOCKI through a 470 resistor.
TMR0 is configured to measured the input frequency, at
RA4 of the PIC 16F84A. The input frequency is gated for a
precise duration of time. Before starting this precise gate,
TMR0 is cleared and the RA3 pin is configured as an input.
The precise gate is implemented in software as an accurate
delay. A 24-bit value of the input frequency is now saved in
TMR0, Registers and 8-bit prescaler. By concatenating the
calculated value and the original value from TMR0 (256-N),
the 24-bit value for the frequency is determined.

Fig. 3 Connections Diagram of the 4051 IC


The features of the frequency counter in this project are:
1. 7-digits Frequency Counter counting the frequency in
the range of minimum 10 Hz to maximum 30 MHz.
2. In actual, it can be counted up to 35 MHz.
3. Decimal point is after MHz digit.
4. Power consumption of 7-segment display is 2.5V/9mA
or 3V/13mA or 5V/35mA.
5. Measuring period is assigned with 0.1 second (100
000 sec).
6. Number of processor cycle is 25 000/fx; fx = crystal
7. Three timing loops, T1 = 7, T2 = 3 and T3 = 20, rough
timing loop, timing loop and fine timing loop.
8. Total timing formula is:
N = 60 x [(36+3 x T1+X) x 7+2+3 x T2+Y] +19+3 x T3+Z
(Where X, Y and Z are the additional tuning NOPs).
9. Only one display range of steady decimal point MHz
rating is used.
This is 7-digit counter counting up to 35 MHz. The decimal
point is after MHz digit. Power consumption with seven
segment display is 2.5V/9mA or 3V/13mA or 5 V/35mA.
Hardware is very simple, it contains:
1. PIC 16F84
2. 4051 (BCD -> 1 of 8 decoder)
3. 8 NPN low power Si transistors
4. 7-digit seven segment display
5. Some resistors, some capacitors and two switching diodes.
Both common cathode and common anode can be used.
Software is written for common cathode display. For common
anode displays it requires very slight software and hardware

Fig. 2 PIC 16F84A operated as a Frequency Counter


This circuit is used in frequency counter as a decoder or
multiplexer/de-multiplexer IC. In this research, this IC outputs
the required BCD to 1 of 8 decoded values. The supply of


World Academy of Science, Engineering and Technology 42 2008

modification. The counter uses internal prescaler of PIC as

low byte of counter, TMR0 as middle byte and some register
as high byte of counter. Timing loop values must be from 1 to
-Values for 4 194 kHz:
T1 = 7 (rough timing loop),
T2 = 3(timing loop),
T3 = 2(fine timing loop).
-Measuring period is 100000 us.
[MHz], (fX is Crystal
-Processor cycle is T = 4/ fX us
-Number of processor cycles per measuring period:
N = 100 000/T processor cycles
N = fX x 100 000/4 = 25 000 x fX
The program of the whole process is done as
following steps:
1. Start measurement,
2. Pre-code decimal value of digit to segments,
3. If the 5th digit set decimal point,
4. Output to Port B,
5. Output digit number to Port A (numbers from left to right
be 6543210),
6. Test TMR0 overflow bite, if YES increase Timer H,
7. Leave digit to light,
8. Increase digit number,
9. If <7, goto 2,
10. Else zero digit number, decrease counter and goto 2,
11. Stop measurement,
12. Shift out pre-counter content,
13. Pre-code 3-byte value into 7 decimal numbers,
14. goto 1
The total timing formula of this program is assigned as
following formula, which is suitable to the required process.
N = 25 000 x fX = 60 x [(36 + 3xT1 + X) x 7 + 2 + 3xT2 +
Y] + 19 + 3xT3 + Z; (Where T1, T2, T3 are initial values of
timing loops, X, Y, Z are additional tuning for no operation
instructions (NOPs)).
It can be calculated the values of three timing loops, T1, T2
and T3 using the program shown in Fig. 4. This program
creates so called include files necessary to compile source
code. It is assumed, that it can be run the calculation program
in the same directory, where source code have created.
The main flowchart of programming to measure the HF
input signal is shown in Fig 5. Before this chart, the initial
values of Index, LED Index, LED0- LED6 and I/O port
assignment are assigned and after this chart, the returning of
the initial all clear state is done. This flowchart mentions only
the process after the input frequency is passed in.


Fig. 4 The Measuring Flowchart of the overall Process


The frequency counter in this research operates and
displays only when the input frequency is incoming, there is
no operation of only +5V DC supply. This result is affected by


World Academy of Science, Engineering and Technology 42 2008

the program that runs the device. The photographs of testing

project are shown in Fig. 5 to Fig. 7. Since the counter used
the seven digits to display the incoming signal, it can operate
as 5-decimal point MHz range frequency counter. Some
examples of frequency values are given as follow:

components, the 10 Hz to 30 MHz range frequency counter is

built. This research is one of the applications of multiplexer
and display technologies. In this research, the comparison
between all kinds of frequency measuring instruments and all
kinds of PIC microcontrollers can be clearly proved. Also,
software programming is developed in this research project,
Design and Construction of PIC based 10 Hz to 30 MHz
Range Frequency Counter.

10 HZ = 00.00001,
10 KHZ = 00.01000,
18 MHZ = 18.00000

Firstly the author would like to thank his parents: U Hla
Win and Daw Saw Shwe from Mawlamyine, Myanmar for
their best wishes to join the PhD research. His Excellency
Minister U Thaung, Ministry of Science and Technology will
also get the authors great thanks for his special guidance to
pay chances. Special thanks are due to WASET for giving
chance for this paper. As this research paper is the authors
M.E research, he would like to thanks all the persons guided
in that time. The author greatly expresses his thanks to all
persons whom will concern to support in preparing this paper.
Fig. 5 Testing result of LED segments Activation


Fig. 6 Complete circuit layout

Fig. 7 Complete Frequency with 7 digits Value

This research project develops the Frequency Counter using
the advanced microcontroller PIC technology. It includes the
hardware design and software implementations. Firstly, the
theory and architecture of PIC microcontrollers are studied.
Then, apply these techniques to design and construct the
frequency counter. Also, the various operations of frequency
measuring instruments of analog and digital types are studied.
With all the backgrounds of PIC microcontroller, 4051
decoder, seven segment LED display and other electronics


Microchip Technology, Inc.2001, PIC16F84A Data Sheet,
Microchip Technology, Inc.2001, PIC based Frequency counter,