Skip to main content

Frequency counter in VHDL (Synthesizeble)

Its Done...!! The Frequency Counter or Measurer is ready. You can measure any I/P frequency from 3Hz to 95KHz i.e. (95,000 Hz for DE1 board Cyclone II FPGA).(This can be diffrent for difrent FPGAs according to their propogation delays and other time factors). Above this value the FPGA gets unstable and show at-random values.

There are 3 levels of Accuracy
  • Hz.
  • (displayed value) x 10 Hz.
  • (displayed value) x 100 Hz.
 I have put a reset button, which is Pushbutton when clicked initiate the frequency measurement process. It then holds the value for user to read.



Right now the free-reading i.e. without any initialization, feature is not added. I will add that soon.

Now, working with code :-

To make the code easy I have used my 0 to 9999 counter code and just made some small changes in it. The Clk freq used is 24MHz.

I created 4 files likewise,
  1. freqcount.vhd (Top-level Entity)
  2. sec_clk.vhd (To generate the time window, which is used for accuracy)
  3. sec_clktry.vhd (Here I added this file for demo so as to generate frequency in circuit itself)
  4. seg7.vhd (Integer to 7-seg converter)
  5. freqcount.sof (Quartus II file only for DE1 board)
  • Now, if you are measuring freq. which is below 9999 then follow the steps. 
  1. In sec_clk.vhd file, keep the max_count=48000000. 
  2. This will generate a 2 sec window (As i/p clock freq is 24MHz) in which the First second will be High i.e. '1' and Another second will be '0'.                                                  
  3. So the code I designed it as, when there is high i.e. '1' then for the i/p of freq to be measured for every high of that freq there is a count. 
  4. When the time window goes to Low level i.e. '0' the counting stops. The counted value is then displayed on Seven seg and is in Hz.
  5. If you have querries about how I generated the time window check my Clock Divider project here.
  6. To generate any freq to be measured follow the formula given below:
      The time window is of 1 sec i.e. as the i/p clock is of 24MHz i.e.(24000000Hz) then for generation of 1 sec it should count till 24000000. Now say you want to generate freq of 90 Hz then,

  replace the max_count value in sec_clktry by

max_count = 24000000/(freq you want at o/p)

max_count = 24000000/90

Then, max_count = 266666.66

Put max_count = 266666 in sec_clktry  for 90Hz freq.

Now, when you run the code then the sec_clktry freq will be given to freq counter and it then gives you at o/p.

If you want to measure any external freq. Then comment out the clki signal and sec_clktry component and portmap in architecture. And re instantiate the clki i/p in entity ports.

  • Now for measuring freq more than 9999 just change the value of max_count in sec_clk as follows,
  1. For freq x 10Hz accuracy, change max_count = 4800000 in sec_clk.vhd (This will generate 0.2 sec window).
  2. For freq x 100 Hz accuracy, change max_count = 480000 in sec_clk.vhd (This will generate 0.02 sec window).
I soon will upload video with full description. If you have any querries mail me at
  • prasadp4009@gmail.com
  • prasadp4009@vhdlcodes.com
Download links:
Enjoy Programming...!! Keep looking, more projects on way.

Video:






Comments

  1. Great design my family's looking for a new gadgets this one will be our topic.
    I’m from: Denon avr 1611 .

    ReplyDelete
  2. can you give me your pinout (.ufc) of the frequency counter?

    ReplyDelete
  3. will this design work for a variable input signal of 4- 20 hz with a variable duty cycle ?

    ReplyDelete

Post a Comment

Popular posts from this blog

Website is being updated with new UI!

Hi E veryone , Pardon me. It took me very long to get back on managing this website. You all during some part of a time in your life, you get so busy that you forget what you actually need to do to keep up. I am moving whole code database on Github so you all will never face any problem with finding codes. Feel free to follow me on Github for updates. My Github: https://github.com/prasadp4009   Stay tuned for updates. Thank you all.

Blu ShopMart

A Bluetooth and RFID based Shopping trolly concept designed for today's world. If any company wants to have this product, contact me on: prasadp4009@gmail.com

VHDL code for Clock Divider

This is a clock divider code, just set the max-count value as per your requirenment. For ex. If I want 1Hz freq. set the max count to i/p freq value viz. 1sec = 1Hz Then, to get time period of 1sec i.e. 1 Hz frequency set max-count to 240000 as shown below: 1sec  =  24000000  -- for i/p frequency of 24 MHz. To get your desired frequency just calculate the maxcount with the formula given below: max_count = 24000000 * (1/your required frequency) CODE: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 library IEEE ; use IEEE.STD_LOGIC_1164.ALL ; use IEEE.numeric_std.all ; entity clk_div is Port ( Clk : in std_logic ; rst : in std_logic ; op : out std_logic ); end clk_div ; architecture behavioral of clk_div is constant max_count : natural := 24000000 ; -- I used 24MHz clock ...