FREQUENCY COUNTER USING PIC MICROCONTROLLER ABSTRACT The multiplexed seven segment display PIC frequency counter uses the PIC microcontrollers for operation. This PIC frequency counter circuit uses a multiplexed seven segment display and uses timer 1 to count edges of the input signal. It uses the simpler method of direct frequency measurement which is easy to do but means that the number of digits displayed depends on the input frequency. This frequency counter circuit uses TMR1 in 16 bit counter mode to count the input signal edges. Counter overflows are accumulated to give the total count in multiples of 65536. Adding the current value of the counter at the end gives the total count. Since the measurement time is 1 second the final count is actually the frequency of the input signal. Using the 1 second measurement time also gives a frequency resolution of 1 Hz. The microcontroller used is PIC16F877A. 1 | Page
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
FREQUENCY COUNTER USING PIC MICROCONTROLLER
ABSTRACT
The multiplexed seven segment display PIC frequency counter uses the PIC microcontrollers for operation.
This PIC frequency counter circuit uses a multiplexed seven segment display and uses timer 1 to count edges of the input signal.
It uses the simpler method of direct frequency measurement which is easy to do but means that the number of digits displayed depends on the input frequency.
This frequency counter circuit uses TMR1 in 16 bit counter mode to count the input signal edges. Counter overflows are accumulated to give the total count in multiples of 65536. Adding the current value of the counter at the end gives the total count.
Since the measurement time is 1 second the final count is actually the frequency of the input signal.
Using the 1 second measurement time also gives a frequency resolution of 1 Hz.
The microcontroller used is PIC16F877A.
1 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
Frequency counter
A simple frequency counter measures frequency by counting the number of edges
of an input signal over a defined period of time (T).
A more complex method is reciprocal counting.
Frequency is defined as (Number of events) / (time in seconds) and measured in
Hz.
To make calculations trivial using a 1 second gate time (T) gives a direct reading
of frequency from the edge counter.
Making a frequency counter for frequencies up to 65.536kHz is easy as the
counters in a PIC chip can count up to 65535 without overflowing.
Up to 65.535kHz all you do is wait for 1 second while the count accumulates, read
the value and display it. It will be the frequency in Hertz. Above 65.536kHz you
have to monitor the overflow value while at the same time making an accurate
delay time (T).
Note: Using a 1 second measurement period results in the frequency counter count
value being a direct measurement of frequency requiring no further processing. It
2 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
also means that the measurement is resolved to 1Hz. (Increasing T to 10s resolves
to 0.1Hz while using T=0.1s gives a resolution of 10Hz).
Crystal oscillator
For the following projects the crystal oscillator (of the microcontroller) is used as
the timebase. In these projects measurement of T (set at one second) is made by
executing a delay that takes a set number of machine cycles.
Using a 4MHz oscillator gives a machine cycle of 1MHz (a period of 1us) which
makes calculating and setting time delays fairly easy since most PIC instructions
execute in one machine cycle. So executing 1,000,000 of these cycles gives a delay
of 1 second.
Frequency counter accuracy
The accuracy of the frequency counter depends on the accuracy of the crystal
driving the microcontroller.
ppm calculation
This is specified in ppm or parts per million. Its actually quite simple: taking an
example of ±50ppm for a 4Mhz crystal. The error that the crystal could have
(assuming that the crystal is loaded with the correct capacitance) will be in the
range :
Maximum possible error 4MHz + (4MHz x 50 x 1e-6) = 4.0002e6
Maximum possible error 4MHz - (4MHz x 50 x 1e-6) = 39998e6
3 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
So the crystal could oscillate at any frequency between 4000200Hz and
3999800Hz. Note that this frequency can be changed by changing the loading
capacitance on the crystal.
The delay time is the important measurement so for the above crystal at fosc/4) for
the cycle time of the PIC chip (nominally 1MHz) we have:
Max cycle time : 1/(1.00005e6) or 1/(1MHz + 50ppm)
Min cycle time : 1/(0.99995e6) or 1/(1MHz - 50ppm)
Multiply by 1e6 to give a 1 second period gives the delay time
Min delay time : 1e6/(1.00005e6) 0.99995s or (1s - 1s x 50ppm) seconds.
Max delay time : 1e6/(9.9995e6) 1.00005s or (1s + 1s x 50ppm) seconds.
So you don't have to calculate all the intermediate steps just use the ppm value
directly.
Note: If you have a reference oscillator that is more accurate than the crystal used
in the frequency counter project then you can calibrate the project crystal. You can
do this by adjusting a variable capacitor on one side of the crystal oscillator
circuit while reading the output frequency displayed. If you don't have a reference
then just use a fixed capacitor to give the correct parallel load capacitance for the
crystal you use.
4 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
Common crystals
Commonly available crystals have a ppm specification of ±30ppm to ±50ppm (part
per million error) but you can buy crystals with a ppm of ±20ppm. The smaller the
ppm value (the smaller the error) the more accurately you can measure frequency.
5 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
PIC MICROCONTROLLERS
A PIC microcontroller is a processor with built in memory and RAM and you can
use it to control your projects (or build projects around it). So it saves you building
a circuit that has separate external RAM, ROM and peripheral chips.
What this really means for you is that you have a very powerful device that has
many useful built in modules e.g.
EEPROM.
Timers.
Analogue
comparators.
UART.
Even with just these four modules (note these are just example modules - there are
more) you can make up many projects e.g.:
* Frequency counter - using the internal timers and reporting through UART
(RS232) or output to LCD.
* Capacitance meter - analogue comparator oscillator.
* Event timer - using internal timers.
* Event data logger -capturing analogue data using an internal ADC and using the 6 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
internal EEPROM for storing data (using an external I2C for high data storage
capacity.
* Servo controller (Control through UART) - using the internal PWM module or
using a software created PWM.
The PIC Micro is one of the most popular microcontrollers and in case you were
wondering the difference between a microprocessor and a microcontroller is that a
microcontroller has an internal bus within built memory and peripherals.
In fact the 8 pin (DIL) version of the 12F675 has an amazing number of internal
peripherals. These are:
Two timers.
One 10bit ADC with 4 selectable inputs.
An internal oscillator (or you can use an external crystal).
An analogue comparator.
1024 words of program memory
64 Bytes of RAM.
128 Bytes of EEPROM memory.
External interrupt (as well as interrupts from internal peripherals).
External crystal can go up to 20MHz.
ICSP : PIC standard programming interface.
7 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
And all of these work from within an 8 pin DIL package!
In the mid-range devices the memory space ranges from 1k to 8k (18F parts have
more) - this does not sound like a lot but the processor has an efficient instruction
set and you can make useful projects even with 1k e.g. LM35 temperature sensing
project that reports data to the serial port easily fits within 1k.
Features
In fact a PIC microcontroller is an amazingly powerful fully featured processor
with internal RAM, EEROM FLASH memory and peripherals. One of the
smallest ones occupies the space of a 555 timer but has a 10bit ADC, 1k of
memory, 2 timers, high current I/O ports a comparator a watch dog timer... I could
go on as there is more!
Programming
One of the most useful features of a PIC Microcontroller is that you can re-
program them as they use flash memory (if you choose a part with an F in the part
number e.g. 12F675 not 12C509). You can also use the ICSP serial interface built
into each PIC Microcontroller for programming and even do programming while
it's still plugged into the circuit!
You can either program a PIC microcontroller using assembler or a high level
language and I recommend using a high level language such as C as it is much
easier to use (after an initial learning curve). Once you have learned the high level
language you are not forced to use the same processor e.g. you could go to an AVR
or Dallas microcontroller and still use the same high level language.8 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
Input / Output - I/O
A PIC Microcontroller can control outputs and react to inputs e.g. you could drive
a relay or read input buttons.
With the larger devices it's possible to drive LCDs or seven segment displays with
very few control lines as all the work is done inside the PIC Micro.
Comparing a frequency counter to discrete web designs you'll find two or three
chips for the microcontroller design and ten or more for a discrete design. So using
them saves prototype design effort as you can use built in peripherals to take care
of lots of the circuit operation.
Many now have a built in ADC so you can read analogue signal levels so you don't
need to add an external devices e.g. you can read an LM35 temperature sensor
directly with no interface logic.
Peripherals
The PIC microcontroller has many built in peripherals and this can make using
them quite daunting at first which is why I have made this introductory page with a
summary of each major peripheral block.
At the end is a short summary of the main devices used in projects shown on this
site.
9 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
The best way to start is to learn about the main features of a chip and then begin to
use each peripheral in a project. I think learning by doing is the best way.
PIC microcontroller
Feature
PIC microcontroller
feature description
Flash memory Re-programmable program storage.
RAM Memory storage for variables.
EEPROMLong term stable memory : Electrically Erasable
Programmable Read Only Memory.
I/O ports High current Input/Output ports (with pin direction change).
Timers/Counters Typically 3.
USART Built in RS232 protocol (only needs level translator chip).
CCP Capture/Compare/PWM module.
SSP I2C and SPI Interfaces.
Comparator An analogue comparator and internal voltage reference.
ADC Analogue to digital converter.
PSP Parallel Slave Port (for 8 bit microprocessor systems).
LCD LCD interface.
Special features ICSP,WDT,BOR,POR,PWRT,OST,SLEEP
ICSP Simple programming using In Circuit Serial Programming.
Power On Reset starts PIC microcontroller initialization when it detects a rising
edge on MCLR.
PWRT
If you enable this then 72ms after a POR the PIC microcontroller is started.
OST
Oscillator Startup Timer delays for 1024 oscillator cycles after PWRT (if PWRT is
enabled) ensuring that the oscillator has started and is stable. It is automatic and
only used for crystal oscillator modes and is active after POR or wake from sleep.
SLEEP
Sleep mode (or low power consumption mode) is entered by executing the 'SLEEP'
command. The device can wake from sleep caused by an external reset, Watch
Dog Timer timeout, INT pin RB port change or peripheral interrupt.
Project device overview
This site mainly uses three PIC devices out of the hundreds of different chips that
microchip produces. This does not sound like a lot but you can use the devices in
almost any project and they have so many built in peripherals that you can make
hundreds of projects with them.
The other microchip devices are all useful in different situations - perhaps they
have more memory or different peripherals - this is useful if you want to tailor your 21 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
designs to the system you build - but probably more useful in a commercial
environment where every cent counts in a production run.
All three devices are extremely powerful and the main difference is that they have
different numbers of pins and memory size.
Note: There are differences in using the devices i.e. there are some registers that
are different but in the generally you can interchange them - this is made easier
using a high level language.
The devices used in this site are:
PIC
microcontroller
Device
PIC
microcontroller
No. Pins
PIC
microcontroller
Flash memory
WORDS
12F675 8 1k
16F88 18 4k
16F877A 40 8k
Note : When looking at the microchip site the memory size is kwords - ignore
kbytes - you need the kword size as this is what each instruction occupies - the
kbyte size is for comparison to other types of micros (probably). But the
microcontroller data bus is 8 bits wide so it is an 8 bit microcontroller (different
program memory and data memory due to using Harvard architecture).
22 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
(Note: that all of them have the letter F in - this means it is a Flash re-
programmable part - don't go and buy a part with O in as its OTP - programmable
only once! - only do that if you are really really sure it's the final design).
PIC Microcontroller Flash Memory size
You may think that 1k or even 8k is so tiny that it won't be useful but each PIC
microcontroller uses RISC (Reduced Instruction Set Computing) which simply
means that it has a cleverly arranged instruction set that only has a few
instructions. The mid range parts have 35 instructions.
If you use the high level language as recommended in this site then you won't need
to be too aware of the instruction set it just means you can do a lot with a small
amount of memory. Most of the projects on this site although they are fully
working projects fit within 2k words!
Note: If you need more memory you can always move to the 18F series of PIC
microcontrollers. Another option is to add an I2C serial eprom.
PIC microcontroller RAM and EEPROM size
The PIC microcontroller RAM size is also important as it stores all your variables
and intermediate data.
Note: You can usually alter the program to use less RAM by choosing the right
variable sizes or changing how your program works
23 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
For example don't use floating point alter it to use a different variable type e.g.
you can use long integers with fixed point operation to avoid floating point.
PIC microcontroller EEROM : Electrically Erasable ROM is used to store data
that must be saved between power up and power down.
This area is readable and writable and has a much longer life than the main
program store i.e. it has been designed for more frequent use.
24 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
PIC16F877A
The 40 pins make it easier to use the peripherals as the functions are spread out over the pins. This makes it easier to decide what external devices to attach without worrying too much if there enough pins to do the job.
One of the main advantages is that each pin is only shared between two or three functions so its easier to decide what the pin function (other devices have up to 5 functions for a pin).
Note: A disadvantage of the device is that it has no internal oscillator so you will need an external crystal of other clock source.
PIC16F877A pinout25 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
PIC16F877A
26 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
As we can see the PIC16F877A is rich in peripherals so you can use it for many different projects.
27 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
40 MHZ FREQUENCY COUNTER USING PIC MICROCONTROLLER
This PIC frequency counter project uses an LCD to display the frequency and PIC
timer 1 (TMR1) to measure the input signal.
It uses TMR1 in 16 bit counter mode to count the input signal edges and overflows
of the counter are accumulated to give the total count in multiples of 65536.
Adding the current value of the counter at the end gives the total count.
Since the measurement time is 1 second the final count is actually the frequency of
the input signal.
Using the 1 second measurement time also gives a frequency resolution of 1 Hz.
Specification
Min frequency 1Hz
Max frequency ~50MHz (limited by input pin characteristics).
Input signal level TTL
28 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
PIC frequency counter schematic using LCD and TMR1.
The hardware is simple and the main blocks are shown in the diagram below.
The LCD is used in 4 bit mode interface so you only need 4 data lines and three
control lines and it then fits into a single 8 bit port.
The crystal oscillator is simply a crystal and two capacitors connected to the PIC
oscillator port at OSC1 and OSC2. The capacitors can both be fixed at the same
value unless you want to tune it using a frequency reference. If you don't have an
accurate reference then use fixed capacitors.
The PIC micro can be any type that has a Timer 1 hardware and and has enough
memory to hold the program.
The LED is toggled at the end of every gate time to indicate that the processor is
alive - so if there is no input signal you can tell that the software is working.
You can program the PIC in circuit through the ICSP connector.
30 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
Project files for the PIC frequency counter
Compiler project files
Frequency_counter_4MHz_LCD_TMR1.ppc
C Source files.
Frequency_counter_4MHz_7seg_tmr1.c
bcd.c
delay.c
seven_segment.c
gate.c
Header files.
def.h
bit.h
bcd.h
delay.h
seven_segment.h
gate.h
Output files
Frequency_counter_4MHz_LCD_TMR1.hex
31 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
Brief description
frequency_counter...c : contains the code start point (in routine 'main') and
the 1 second delay (measurement time) - gate_loop.
bcd.c : contains machine code to convert a long to a bcd.
delay.c : contains code to create fixed delay times.
bit.h : contains macros for bit manipulation.
All other header files contain prototypes.
PIC frequency counter code operation.
The code uses the built in LCD driver routines which are automatically included by
the compiler. Note automatic include is unusual but it seems to work well in
mikroC.
Interrupts are not used only the flags that can be polled (timer overflow) are
activated.
frequency_counter_4MHz_LCD_TMR1.c
This file contains the port initialization, gate loop and main routine.
After initialization the code enters an endless loop where it continuously performs
a measurement and display operation. After an accurate 1 second delay the counter
result is processed and displayed on the LCD.
32 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
The gate loop is tuned to just below a millisecond so that the caller (in main) can
adjust the exact delay taking account of any delays caused by calling the gate loop
routine itself.
Note that the timer overflow is polled within the gate loop - the extra statements in
the else part of the if statement allow constant execution time whether the
condition was true or false. This allows the loop time to be accurately calculated
since it always has the same execution time.
delay.c
Delay routines were created using machine code so that they have a fixed
execution time i.e. they do not change as the compiler re-optimizes the code. They
are also fixed in memory location to avoid bank change problems.
bcd.c
This routine was created in machine code to save space on the smaller chips and it
also results in faster code than using the built in routines for long multiply and
divide.
It uses the Add 3 method to convert the unsigned long into an ASCII value that can
be displayed on the LCD.
gate.c
This file contains the gate loop time measurement routines - the loop time is tuned to 999us so that the caller can calibrate the 1 second delay time (accounting for compiler optimisation and return from call instructions).
33 | P a g e
FREQUENCY COUNTER USING PIC MICROCONTROLLER
The gate_loop routine calls the constant time seven segment display update and also checks (in constant time) the timer 1 overflow counter.
sevensegment.c
This file has the 8 digit seven segment display driver.
The first output from the 4017 is not connected so this acts as the reset state. At every call the next digit is output on port D and the 4017 is advanced one bit by strobing the clock. In this way after each call the next digit is displayed.
All the routines here are made into constant time routines so that the gate loop (the calling routine) can make an accurate 1 second time measurement.
bit.h
This contains macros for bit manipulation which should be compiler independent.
def.h
Contains a control to let the simulator update variables where needed.