100MHz frequency counter with PIC16F628A – LCD Display

Hristo   xristost@yahoo.com   http://diyfan.blogspot.com  
31785
Project tested

FreqCounter(16F628A,LCD,1)

This project shows how to build a very simple yet very useful tool that every DIY enthusiast should have in his lab: a 100MHz+ frequency counter.

The schematic is fairly simple and straightforward and uses a PIC16F628A microcontroller for measuring frequency and a high speed comparator for signal amplification and conditioning.

The microcontroller uses its internal 4MHz oscillator for the CPU clock. Timer1 uses an external crystal resonator (watch crystal) with 32768Hz frequency for setting the 1 second time base.

Timer0 is used to count the input signal at pin RA4.

The max frequency of Timer0 is 1/4 of the CPU clock which is 1MHz, but there is internal prescaler and it can be set from 1 to 256. In theory this can allow the input signal to be up to 256MHz. On the other hand, in the datasheet of 16F628A there is a requirement for the input pulse at RA4 to be with minimum width of 10ns which is 100MHz frequency. So the maximum frequency can be between 100Mhz and 256MHz. I checked with two different PIC16F628A and they easily go over 200Mhz barrier.

In order to achieve the maximum possible resolution, the input signal is probed for 0.125 seconds and the prescaler value is computed accordingly. This way when input frequency is below 1Mhz the resolution will be 1Hz.

The most important part for the accuracy of the frequency counter is the time base setting circuit – crystal resonator X1 and capacitors C4 and C5. C4 and C5 values can be between 33pF and 62pF and the crystal frequency can be fine tuned with them.

The input of the schematic is feed through a high speed comparator. In order to switch with 100+ Mhz frequency the comparator must have propagation delay bellow 5ns. In this schematic I used Texas Instruments TLV3501 with 4.5ns delay. This was cheapest high speed comparator I was able to find (2.5 euro).

The two inputs of the comparator are set at about 1/2 of power supply voltage with 15-25mV difference between them so any AC signal with higher voltage will start switching the comparator.

If there isn’t input signal the output of the comparator stays low. If we connect a signal source to the positive input, when the signal goes over +20mV the comparator switches high (5V), when signal goes bellow +20mV comparator switches back to 0V. So whatever signal we fed to the input, the output is square wave 0V-5V with the same frequency as the original signal.

The output of the comparator is fed directly to the RA4 pin of the microcontroller.

The input is protected with 1k resistor and two diodes limiting the voltage to ±0.7 V. The input impedance for low frequencies is equal to R1 – 47k. For VHF range maybe it is good idea to replace it with 50 Ohm value.

The schematic can be powered by 9V battery or any other DC voltage from 7V to 15-20V. LM78L05 or LM2931-5.0 IC is used for regulating the voltage down to 5V. There is simple soft ON/OFF circuitry with a dual P- and N-MOS transistor. When button is pressed the P-MOS transistor is switched on and the microcontroller is powered and its first instruction is to set RB4 high which switch the N-MOS transistor on and the power stays on. If the button is pressed again RB5 goes low and the microcontroller sets the RB4 low and this way switch the power off. The microcontroller also auto switch the power off after a certain amount of time (3min 40sec).

The schematic have fairly low power consumption – with no input signal the supply current is 7-8mA and goes up to 20mA with 200+MHz input signal. If the display is too dark, the back light can be adjusted by decreasing the value of the R9 resistor. This of course will increase the current consumption.

The program for the microcontroller is written in C and is compiled with MikroC for PIC

Schematic

FreqCounter(16F628A,LCD,schematic)

Photos

FreqCounter(16F628A,LCD,2)
FreqCounter(16F628A,LCD,3)

FreqCounter(16F628A,LCD,4)

FreqCounter(16F628A,LCD,5)

FreqCounter(16F628A,LCD,6)

FreqCounter(16F628A,LCD,7)

FreqCounter(16F628A,LCD,8)

References:

PDF datasheets:

PCB

31 Response on “100MHz frequency counter with PIC16F628A – LCD Display

    1. This is a simple ferrite bead for limiting the noise injected to power net. If you don’t have a ferrite bead use a simple wire.

  1. Hello I want to make this, but have a problem!
    I can’t find AP2530GY mosfet available. Market says it is not available anymore. Should I change the model and edit the PCB work?
    Also, is there any trade offs between this version and http://www.electronics-lab.com/project/100mhz-frequency-counter-with-pic16f628a/ (LED) version?
    LED version seems easier to make, and have same frequency resolution. If the spec is similar, I am thinking to work on LED version.

    Thank you.

    1. Hello, I am also going on this project and are in troubles.
      Which errors are you getting? I am currently using MPLAB X IDE v.345 with xc8(v1.38)
      I am getting errors such like (Unexpected tokens at sbit assignation).

      Could we help each other?

        1. I finally managed to make some small alterations to the program using mikroC pro for PIC which is easier to use than MPLAB. I didnt get any compile errors using the given C program and I even got the hex code to program on a chinese K150 programmer! The hex code of original and modified versions works fine. Hope this helps.

  2. Hello,
    I need to add some features in the existing code. Can you please share the complete code (along with the LCD driver). Currently I see only the main c file which can’t be compiled without the LCD driver. I already have made the PCB and waiting for updating the code.
    Thanks a lot.

    1. MikroC Pro provides built in libraries for interfacing LCDs with HD44780 compliant controllers, so you don’t need an external library.

      1. Hi,
        Thanks for your reply.
        Can you please clarify if you are using the INTERNAL OSCILLATOR (4MHz) of PIC and what purpose you are using 32kHz oscillator for?
        Moreover, what config settings we need to set in the MikroC especially for the oscillator.

        Thanks

  3. Hi,
    I am able to compile using MikroC. I am programming the chip using PICkit3 using 5 pin header and MPLAB IPE. The problem is that, the chip gets programmed well for the first time but gives communication error and does not get programmed in later attempts.
    This problem does not happen when I program any other code (like LED blinking) over and over again.
    So I felt as if there is some kinda code protection enabled which does not allow me to program for the second time.

    But I have also checked the config bits, the code/data/flash protection is OFF.

    Can you please help me in identifying the problem?

  4. Hi,
    I am able to fix the issues and can reprogram the chip as many times as I want. This topic helped me figuring out the problem
    http://www.microchip.com/forums/tm.aspx?m=303686&mpage=2&key=&#308594

    Now, the circuit is working and counting frequency TILL 87MHz ONLY as soon as I give 88MHz to its input, the measured value drops and starts giving wrong frequency.

    Can you suggest why I am into this limit while you were able to measure till 200MHz? Actually my frequency range of interest is 100MHz to 200MHz.
    I have made some changes in my circuit compared to yours, these changes are as follows:

    1. No ON/OFF switch and its corresponding circuitry, directly feeding the power through 7805 chip with caps
    2. Using SMD version of all components including PIC, caps and xtal etc. No through hole so my PCB is single sided only
    3. Not using 4148 Diodes (keeping input amplitude = 300mV)
    4. Using R1=50 ohm instead of 47k as my oscillating circuit has O/P impedance of 50 ohm
    5. Using 12pF caps instead of 47pF (I just noticed it but I can replace them)

    What do you think, can anyone of above changes cause any trouble in frequency range?

    Thanks

  5. Regarding frequencies over 80Mhz I am suspicious that only some pics will respond. I intend to use an external prescaler divide by 10 which works to more than 1Ghz. I chose MC12080(ebay). In this case little or no modification will be required to the code since you can figure out the reading easily. this prescaler only needs a few capacitors and one resistor to work. A small circuit diagram is available if you need it.
    Regarding the power off circuitry I have not included it as the whole thing works from a small 5v power adapter.

    1. “So the maximum frequency can be between 100Mhz and 256MHz.” That statement seems incorrect, but hey, I don’t know much about the PICs. So who am i to argue.

  6. Dear Terry,
    Thanks for your response. I have looked into datasheet of MC12080, as it uses D flip flop to divide the frequency, so it should work on square wave, right? but the datasheet also shows the input signal source as sinusoidal, can you please clarify if I can use this chip fed directly with sinusoidal signal?

    If this is the case, then I may use the comparator at the output of MC12080 (Divided by 10) before feeding to controller.
    Right?

    Your suggested will highly be appreciated.
    Thanks

    1. Muhammad,
      I cant answer your question about sine and square wave with the MC12080 as I have ordered two but they have not arrived yet. However from experiments I have conducted I find that the input circuitry design of a frequency counter is very critical so that if there is any noise or whatever on the input it shows up as unstable readings. Square waves are sometimes problematic as the edges are not always clean. The input needs a good filter for the frequency range you are interested in and control over the upper and lower limits as per comparator IC or schmidt trigger and that TLV3501 may need some feedback. Anyway thats what experimentation is about so good luck with your project.

  7. Hello ,

    Im relative new to electronics and im trying to understand the schematic, but what purpose does the capacitor c1 serve? is it for filtering or something else?

      1. Ah i see. I have another question is iτ possible to swap the PIC16F628A for an arduino? if so do i need some kind of prescaler circuit before the input of the arduino?

Leave a Reply