Proton BASIC Compiler - A Frequency Counter with the PIC16F628A

  • Pic® Basic

  • A Frequency Counter with the PIC16F628A

    I have started work on an original program based on the AN592 Microchip application using a 16F84. I use a later device, the PIC16F628A microprocessor PIC® ,this chip is easy to buy and uses a fully compatible schematic to most PIC16F84 based frequency counter projects.

    The 4 MHz Xtal has been chosen for compatibility too although I think a 20 MHz Xtal would be better (See version 2 below).

    The PIC16F628A has one 8-bit timer (Timer0), which can be used with an 8-bit prescaler to read directly up to 50Mhz when used with other counters as needed. (Timer0Hi as Word) is increased by the overflow of the TMR0 counter. The prescaler runs asynchronously, hence it can count to a very high frequency.

    The minimum rise and fall times of the input frequency are specified to be 10 ns, so the fastest clock rate the TMR0 can count is 50 MHz. The prescaler must be used when measuring higher frequencies. Since the prescaler can be configured as a divide by 256 counter, the maximum resolution at which the input frequency can be measured is 16-bits. However, the prescaler cannot be directly read like a file register. The application note depicts a unique method by which the user can “extract” the 8-bit value in the prescaler, whereby the resolution of the measurement is 32-bits with the Timer0Hi high 16-bits TMR0 counter, the medium 8-bits in TMR0 and the low 8-bits in the prescaler.

    A frequency counter which can read frequencies from 50 Hz to 50 MHz was implemented in the application note in order to demonstrate this method of measuring the 32-bit counter value from the prescaler, TMR0 and Timer0Hi. It consists of the frequency input at TMR0 or T0CKI (pin 3 in a PIC16F628A). T0CKI is connected to RA2. The input frequency is connected to T0CKI through a 470 ohms resistor.

    TMR0 is configured to measure the input frequency at T0CKI of the PIC16F628A. The input frequency is “gated” for a precise duration of time. Before starting this precise “gate”, TMR0 is cleared (which also clears the prescaler), and the RA2 pin is configured as an input. The precise “gate” is implemented in software as an accurate delay. At the end of the delay, the RA2 pin is configured as an output going High. This will cause the input to TMR0 to be “halted” or “stopped”. A 32-bit value of the input frequency is now saved in Timer0Hi, TMR0 and the 8-bit prescaler. The high 8 bits are in TMR0 and can be easily read. The low 8 bits have to be "shifted out". The 8 bits in the prescaler are “shifted out” by toggling RA2. After every toggle, the value in TMR0 is checked to see if TMR0 has incremented. If the number of toggles required to cause TMR0 to increment by 1 is N, then the 8-bit value in the prescaler can be calculated to be = (256 - N). By concatenating the calculated value in the prescaler, the original value from TMR0 and the high Timer0Hi counter the 32-bit value for the frequency is determined.

    For this project I wrote a program to make a standard 10Hz to 50MHz frequency counter with a resolution of +/ -1Hz and with 8 digit display that can be used for our small laboratory at home. Since it is a very simple design and gives +/-1Hz resolution this frequency counter cannot be compared with a professional Laboratory Frequency Counter.

    Power supply: External 9V (better be regulated). The + 5V regulator is not drawn in the schematic.
    Consumption: ~ 15 mA including LCD. The LCD Backlight output is NOT included.
    Frequency range: 10Hz - 50MHz The sensitivity of the RF input depends on the preamplifier used. It is another matter.
    Accuracy and stability over time: not measured. At the temperature of 20 °C after 30 minutes stabilization is estimated to be within + -25ppm. In the temperature range important variation may be much higher.
    Resolution: + -1Hz
    Time window for measurement: 1 Second (could be divided for 4 windows of 1/4 Second or 10 windows of 1/10 Second).
    Frequency is displayed in 8 digits.

    The 4MHz crystal quartz for the microcontroller and associated capacitors must be of the greatest possible stability (NPO type). Low quality components can cause variations in the frequency reading a few tens of Hertz by the changes of ambient temperature. In particular I suggest that the variable capacitor should be ceramic or air to adjust the crystal frequency. Do not forget that the result depends on the design and quality of the components used. The crystal could be + -20ppm (better + -10ppm but hard to find). You may need to change the value of the capacitors depending on the available crystal.

    Adjusting the frequency counter for precise reading, it is necessary to use a highly stable and high quality frequency generator and counter, for example at 30MHz. Before adjusting the crystal frequency via trimmer capacitor, let the board components stabilize to ambient temperature and the professional generator and frequency counter. Let them run for at least 30 minutes. Be careful if you connect the frequency counter to a transmitter, use minimum power output. Connect the transmitter output to a 50 ohm dummy load with suitable power capacity and then mount a resistor attenuator to the frequency counter PCB input.
    The frequency voltage input shall not exceed 800mV. To connect a higher voltage is unwise. The two 1N4148 diodes are to protect the transistor input. Direct connection to the transmitter could burn the diodes and input transistors could cause damage to the transmitter itself.

    External Switch:
    An input is available in PORTA.1 for an external Switch or Push Button for the PDS user.

    The different languages presented on the LCD are in Spanish, French or English according to the recorded version.

    I think it is possible that a PDS user could add some code between frequency readings. Normally a 1 second window seems too long. Three time windows are available: 1 second / 0.25 second / 0.1 second. It is only an illusion because the measurement window is still 1 second.
    For 1/4 second window 4 measurements are added and for 1/10 of a second window 10 measurements are added. The Frequency value is printed after every window except for 1/10 because it is too fast. In fact the frequency is printed every 1 second or every 1/4 second.
    In each measurement (window) the accuracy may be within + - 1 Hz, to take several measures the deviations could be cancelled. The duration of the window must be chosen at compile time.
    The PDS user could add a code after each frequency measurement. If the code is too large it would be useful to divide it into several parts with the aid of a state machine. If you need some help, let me know.

    13/07/2015: Do not use the "Optimizer" and the "Dead_Code_remove", some errors could occur.

    Version 1.0

    New features: Version 2.0
    New pin:
    I used the reset pin for a new switch.
    Now two switches are available to the user.

    New Xtal: The 20 MHz Xtal has been added. The code is changed automatically with the "Declare xtal = 4" or "Declare xtal = 20".

    The Timer1 measurement window is adjusted by 2 switches.

    XTAL = 4MHz:
    The Xtal frequency must to be adjusted at 4.000000MHz changing the capacitors' values.
    At 30 MHz every increment of the Timer1 correction means an adjustment of the input frequency as follows:
    30.000.000 Hz / Xtal/4 = 30 Hz

    XTAL = 20MHz:
    The Xtal frequency must to be adjusted at 20.000000Mhz changing the capacitors values.
    At 30 MHz every increment of the Timer1 correction results in an adjustment of the input frequency:
    30.000.000 Hz / Xtal/4 = 6 Hz
    The precision to adjust the Offset is better too. The value of the variable capacitor connected to the Xtal must to be smaller.

    Output frequency Xtal is available on pin PORTA.1 (Switch 1)
    For XTAL 4MHz the frequency output is 166 666.6666 Hz (FOSC/4 / 6)
    For XTAL 20MHz the frequency output is 833 333.3333 Hz (FOSC/4 / 6)
    DO NOT connect any capacitor to this pin during this test.
    To exit the test turn off the power of the PIC.

    1- Turn on the power +5V with the Switch 2 pressed to go to the Xtal adjust routine.
    2- Turn Off the power.
    3- Turn on the power +5V with the Switch 1 pressed to go to adjust the Timer1 window.
    Check the frequency and modify the Timer1 window with Switch 1 and Switch 2.
    4- After 10 seconds of inactivity, follow the prompts on the LCD.
    Press the Switch 1 to save Timer1 value or press the Switch 2 to start again the adjust.

    Every Timer1 offset is saved in the eeprom corresponding with every timing (0.1s, 0.25s, 1s)
    This makes it possible to reuse these values when recompiled without re-adjustment of the Timer1 offset.
    To perform this function change the programmer settings to not erase/program the eeprom memory when the PIC is flashed.

    With 20MHz xtal the frequency counter runs much better.
    Enjoy these new features.
    I hope this project basis helps you to start something more important.
    Alberto Freixanet