Altera_Forum
Honored Contributor
17 years agoFrequency Counter in Verilog?
Hi all, im trying to measure a squarewave frequency which is coming from a tacho.
The first implementation i tried used a known frequency and between rising edges of this frequency the rising edge of the input was counted. Once the know frequency gave a rising edge again the count was recorded then reset. Im not sure why but it didnt work!?!? The i went on to use a much quicker reference frequency and counted how many rising edges of this occured between rising edges of the input. the reference frequency is 10KHz. My coded didnt seem to simulate correctly!?!
module f_count(resetn,ref_clock,in_clock,f_measurment);
input resetn,ref_clock,in_clock;
output f_measurment;
reg f_measurment;
integer count;
always@(posedge ref_clock) // on rising edge of the reference 10KHz
begin
if(resetn) // if reset line is high do the operation
begin
count=count+1; // increase the count
end
else // if reset line is low
count=0; // then reset the count to zero
end
end
always@(posedge in_clock) // on rising edge of the input
begin
frequency=10000/count; // calculate the frequency based on the count value
count=0; // then reset the count to zero for the next measurment
end
endmodule
The simulation in Quartus II just didnt work as I wanted, the count did seem to make sence it reduced in frequency!?!?! Am i going about doing this all wrong?