数字频率计
第二章 任务要求
设计一个计数式频率计,其频率测量范围为10Hz~1MHz,测量结果用6只数码管显示。有三个带锁按键开关(任何时候都只能有一个被按下)被用来选择1S、0.1S和0.01S三个闸门时间中的一个。有一个按钮开关用来使频率计复位。有两只LED,一只用来显示闸门的开与闭,另一只当计数器溢出时做溢出指示。下图显示了该频率计前面板的基本排布构想。
第三章 各模块功能及介绍
3.1 分频器
由于晶体振荡器提供的为48M的时钟,而在整个频率计里将用到周期为2s、0.2s和0.02s的闸门信号,还有译码显示的扫描信号1KHz ,所以我们在此模块先分频产生1Hz、10Hz、100Hz、1KHz四个分频信号,以留作其它模块用。
分频分别采用4个计数器来实现,当计到一定的值时输出的分频信号翻转,最后分别获得4个分频输出,分频器模块如下图所示:
图 3-1 分频器模块
此模块的复位为同步方式,当复位有效时,输出将清零。 源程序如下:
6
数字频率计
module div_clk(reset, clk, clk_1hz, clk_10hz, clk_100hz, clk_1khz); input reset,clk;
output reg clk_1hz, clk_10hz,clk_100hz,clk_1khz;
reg [29:0] counter1,counter2,counter3,counter4; //分频计数值 always @(posedge clk or negedge reset) begin if(!reset) begin counter1<=0;counter2<=0;counter3<=0;counter4<=0; clk_1hz<=0;clk_10hz<=0;clk_100hz<=0;clk_1khz<=0; end else begin if(counter1==24000000) begin counter1<=0;clk_1hz<=~clk_1hz; end else begin counter1<=counter1+1; end if(counter2==2400000) begin counter2<=0;clk_10hz<=~clk_10hz; end else begin counter2<=counter2+1; end if(counter3==240000) begin counter3<=0;clk_100hz<=~clk_100hz; end else begin counter3<=counter3+1; end if(counter4==24000) begin counter4<=0;clk_1khz<=~clk_1khz; end else begin counter4<=counter4+1; end end end
endmodule
仿真图如下所示:
图 3-2 分频器模块仿真图(一)
图 3-3 分频器模块仿真图(二)
4.2 闸门选择器
该模块主要实现对闸门的选择功能,通过输入的门选信号来确定输出的闸门,生成的模块如下图所示:
7
数字频率计
图 3-4 闸门选择器
具体实现方法如下:当三个门选信号中有且仅有门选信号gate_ch1有效时,reg变量gate为clk_1hz ,f的值为01;当三个门选信号中有且仅有门选信号gate_ch2有效时,reg变量gate为clk_10hz ,f的值为10;当三个门选信号中有且仅有门选信号gate_ch3有效时,reg变量gate为clk_100hz ,f的值为11。reg变量gate再二分频则是输出的闸门信号gate_out(f 为标记信号,标记了当前闸门的选择情况)。如果同时有两个或以上的门选信号有效,则err输出为低,否则为高。
另外输出的译码扫描信号为clk_1khz ,供后面的扫描译码模块使用。
本模块的源代码如下所示:
module gate_ch(reset,gate_ch1,gate_ch2,gate_ch3,
clk_1hz,clk_10hz,clk_100hz,clk_1khz,
gate_out,err,scan_freq,f);
input reset,gate_ch1,gate_ch2,gate_ch3,
clk_1hz,clk_10hz,clk_100hz,clk_1khz;
output reg gate_out,err; output wire scan_freq; output reg [1:0] f;
reg [2:0] counter5; // 产生扫描信号时的分频计数值 reg gate;
always @(posedge clk_1khz or negedge reset) begin
if(!reset) begin gate<=0;counter5<=0;err<=1;f=2'b01;
8
数字频率计
end else begin if(gate_ch1==0 && gate_ch2==1 && gate_ch3==1) begin gate<=clk_1hz; f=2'b01;err<=1; end else if(gate_ch1==1 && gate_ch2==0 && gate_ch3==1) begin gate<=clk_10hz;f=2'b10;err<=1; end else if(gate_ch1==1 && gate_ch2==1 && gate_ch3==0) begin gate<=clk_100hz;f=2'b11;err<=1; end else begin err<=0; end end end
assign scan_freq=clk_1khz;
always @(posedge gate or negedge reset) begin
if(!reset) begin gate_out<=0; end
else begin gate_out<=~gate_out; end end
endmodule
仿真结果如下图所示:
图 3-5 当且仅当gate_ch2有效时的仿真结果图(一)
4.3 频率计数器
频率计数器的功能为在输入的闸门信号的控制下对输入脉冲时行计数,它
是一个7拉的模十计数器。生成的模块如下图所示:
9
数字频率计
图 3-6 当且仅当gate_ch2有效时的仿真结果图(一)
当gate_out信号为高时才计数器才计数,在gate_out为低后,马上将此时计数的值cnt赋给输出cnte ,然后当输入carry_in信号再过一个脉冲后降计数的值cn清零,以备下次闸门有效时又重新开始计数。源程序如下:
module count(carry_in,gate_out,reset, cnte0,cnte1,cnte2,cnte3,cnte4,cnte5,cnte6); input carry_in,gate_out,reset;
output reg [3:0] cnte0,cnte1,cnte2,cnte3,cnte4,cnte5,cnte6;//在每一次闸门有效时读数
//器传递的计数值
reg [3:0] cnt0,cnt1,cnt2,cnt3,cnt4,cnt5,cnt6; //计数器的6个计数值
reg [2:0] counter6; // 延迟将counter-num清零的计数值 always @(posedge carry_in or negedge reset) begin if(!reset) begin cnt0<=4'b0000;cnt1<=4'b0000;cnt2<=4'b0000;cnt3<=4'b0000; cnt4<=4'b0000;cnt5<=4'b0000;cnt6<=4'b0000;//判断是否溢出 cnte0<=4'b0000;cnte1<=4'b0000;cnte2<=4'b0000;
cnte3<=4'b0000;cnte4<=4'b0000;cnte5<=4'b0000;counter6<=0; end else begin if(gate_out==1) begin counter6<=0;
if((cnt5==4'b1001)&&(cnt4==4'b1001)&&(cnt3==4'b1001)&&(cnt2==4'b1001) &&(cnt1==4'b1001)&&(cnt0==4'b1001)) begin cnt0<=4'b0000;cnt1<=4'b0000;cnt2<=4'b0000;cnt3<=4'b0000; cnt4<=4'b0000;cnt5<=4'b0000;cnt6<=cnt6+4'b0001;end else begin
if((cnt4==4'b1001)&&(cnt3==4'b1001)&&(cnt2==4'b1001)&&(cnt1==4'b1001)&&(cnt0==4'b1001)) begin cnt0<=4'b0000; cnt1<=4'b0000; cnt2<=4'b0000; cnt3<=4'b0000; cnt4<=4'b0000; cnt5<=4'b0001+cnt5; cnt6<=cnt6; end else begin
10