1、3数字频率计的设计设计要求1设计一个能测量方波信号频率的频率计,测量结果用十进制数显示。2. 测量的频率范围是110KHz,分成两个频段,即1999Hz,1KHz10KHz,用三位数码管显示测量频率,用LED显示表示单位,如亮绿灯表示Hz,亮红灯表示KHz。3. 具有自动校验和测量两种功能,即能用标准时钟校验测量精度。4. 具有超量程报警功能,在超出目前量程档的测量范围时,发出灯光和音响信号。设计提示l 脉冲信号的频率就是在单位时间内所产生的脉冲个数,其表达式为f=N/T,f为被测信号的频率,N为计数器所累计的脉冲个数,T为产生N个脉冲所需的时间。所以,在1秒时间内计数器所记录的结果,就是被测
2、信号的频率。l 测量/校验选择模块*l 计数器模块l 送存选择报警模块l 锁存模块l 扫描显示模块 l 测量/校验选择模块*输入信号:选择信号selet被测信号meas测试信号test输出信号:CP1当selet=0时,为测量状态,CP1=meas;当selet=1时,为校验状态,CP1=test。校验与测量共用一个电路,只是被测信号CP1不同而已。l 设置1秒定时信号(周期为2秒),在1秒定时时间内的所有被测信号送计数器输入端。l 计数器对CP1信号进行计数,在1秒定时结束后,将计数器结果送锁存器锁存,同时将计数器清零,为下一次采样测量做好准备。l 设置量程档控制开关K,单位显示信号Y,当K
3、0时,为1999Hz量程档,数码管显示的数值为被测信号频率值,Y显示绿色,即单位为Hz;当K=1时,为1KHz10KHz量程档,被测信号频率值为数码管显示的数值乘1000,Y显示红色,即单位为KHz。l 设置超出量程档测量范围示警信号alert。计数器由四级十进制计数构成(带进位C)。若被测信号频率小于1KHz(K=0),则计数器只进行三级十进制计数,最大显示值为999.Hz,如果被测信号频率超过此范围,示警信号驱动灯光、扬声器报警;若被测信号为1KHz10KHz(K=1),计数器进行四位十进制计数,取高三位显示,最大显示值为9.99KHz,如果被测信号频率超过此范围,报警。设计文档一、原理
4、测频的原理归结成一句话,即在单位时间内对被测信号进行计数。下图说明了测频的原理及误差产生的原因。图1 测频的原理在上图中,假设时基信号为1khz,则用此法测得待测信号为1khz*7=70khz。但从图中可以看出,待测信号应该在77khz,误差约为7/779.1%。这个误差是比较大的,实际上,测量的脉冲个数的误差会在间。假设所测得脉冲个数为n,则所测频率的最大误差为1/(n-1)*100%。显然,减小误差的方法就是增大n。本频率计的要求测量结果以3位数表示,则测频误差应为1%0.1%,则n的取值范围为: 100nen,num=numina,display=lsb); u2:display por
5、t map(en1=en,num=numinb,display=middle); u3:display port map(en1=en,num=numinc,display=msb); end;这段程序是用类属映射的方法将一位七段数码管扩展成一个显示器的电路,其实显示器可以用图形的形式生成,但这里用这个方法主要是为了学习VHDL的不同设计方法。图形方法的优点是比较直观,各个管脚的连接不容易出错,特别是当一个模块的包含许多子模块的时候,更能体现出图形方法的优越性。又例如:library ieee;use ieee.std_logic_1164.all;entity fredevider10 is
6、generic (n:integer:=9);port(clkin:in std_logic; clkout:out std_logic);end;architecture behavior of fredevider10 issignal counter:integer range 0 to n;signal clk:std_logic;begin process(clkin) begin if rising_edge(clkin) then if counter=n then clk=not clk; counter=0; else counter=counter+1; end if; e
7、nd if; end process; clkout=clk; end;上述程序是一个十分频电路,如果想得到其它倍数的分频电路可以通过修改counter的上限值N得到。一般的计算规则是:对一个2x的分频电路来说,counter上限值N=x-1(从0计到x-1恰好为x次,每个上升沿翻转一次就实现了2x分频)。附录附录1:状态机程序library ieee;use ieee.std_logic_1164.all;entity statemachine isport(clock,clock10hz,clock100hz,clock100khz:in std_logic;low,over:in std
8、logic; -欠量程信号和超量程信号reset:in std_logic; en:out std_logic; -控制七段数码管的显示dp1,dp2:out std_logic; -dp1为百位的小数点 -dp2为十位的小数点overlight,lowlight:out std_logic; -待测信号频率超过总量-的指示灯 outclock:out std_logic -输出时基信号);end;architecture mooremachine of statemachine istype state_type is(f10k,f100k,overerror,lowerror);sign
9、al state:state_type;begin process(clock100khz,reset) begin if (reset=1)then state if over=1then state=overerror; elsif low=1 then state=f10k; else state if over=1 then state=f100k; elsif low=1 then state=lowerror; else state if low=1 then state=f10k; elsif over=1 then state=overerror; else state if
10、low=1 then state=lowerror; elsif over=1 then state=f100k; else state en=1; -允许七段数码管显示数字 outclock=clock100hz; -时基信号为100hz overlight=0; -待测信号频率未超过总量程 lowlight=0; dp1=0; -百位上小数点不亮 dp2 outclock=clock10hz; overlight=0; lowlight=0; en=1; dp1=1; dp2 en=0; outclock=clock100hz; overlight=1; lowlight en=0; ou
11、tclock=clock10hz; lowlight=1; overlight=0; end case; end process; end; 附录2:计数器程序:library ieee;use ieee.std_logic_1164.all; entity counter is port(clock1,clock2:in std_logic;result1,result2,result3:out integer range 0 to 9;over,low:out std_logic);end;architecture behavior of counter issignal en,entra
12、nsfer:std_logic;signal num1,num2,num3:integer range 0 to 9;signal overmode:std_logic; begin process(clock1) begin if rising_edge(clock1) then en=not en; end if; end process; process(clock2) begin if rising_edge(clock2) then if en=1 then -en=1时传输数据 if entransfer=1 then -entransfer以保证数值-与信号只传输一次 entra
13、nsfer=0; if overmode=1 then -超量程的情况 low=0; over=1; overmode=0; elsif num3=0 then -欠量程的情况 low=1; over=0; else low=0; over=0; result1=num1; result2=num2; result3=num3; end if; num1=0; num2=0; num3=0; end if; else -en=0时计数 entransfer=1; low=0; over=0; if num1=9 then if num2=9 then if num3=9 then overmo
14、de=1; else overmode=0; num3=num3+1; num2=0; num1=0; end if; else overmode=0; num2=num2+1; num1=0; end if; else overmode=0; num1=num1+1; end if; end if; end if; result1=num1; result2=num2; result3=num3; end process;end;附录3:同步整形电路程序:library ieee;use ieee.std_logic_1164.all; entity signallatch isport(c
15、lock:in std_logic;signalin:in std_logic;signalout:out std_logic);end;architecture pataflow of signallatch issignal clear,s:std_logic;begin process(signalin) begin if clear=1 then s=0; elsif rising_edge(signalin) then s=1; end if; end process; process(clock) begin if falling_edge(clock) then if s=1 t
16、hen signalout=1; clear=1; else signalout=0; clear=0; end if; end if; end process;end; 附录4:分频器程序library ieee;use ieee.std_logic_1164.all;entity fredevider10 isgeneric (n:integer:=9);port(clkin:in std_logic; clkout:out std_logic);end;architecture behavior of fredevider10 issignal counter:integer range
17、 0 to n;signal clk:std_logic;begin process(clkin) begin if rising_edge(clkin) then if counter=n then clk=not clk; counter=0; else counter=counter+1; end if; end if; end process; clkout=clk; end;library ieee;use ieee.std_logic_1164.all;entity fredevider200 isgeneric (n:integer:=99);port(clkin:in std_
18、logic; clkout:out std_logic);end;architecture behavior of fredevider200 issignal counter:integer range 0 to n;signal clk:std_logic;begin process(clkin) begin if rising_edge(clkin) then if counter=n then clk=not clk; counter=0; else counter=counter+1; end if; end if; end process; clkout=clk; end;附录5:
19、锁存器程序library ieee;use ieee.std_logic_1164.all;entity shuocun isport(clk,d:in std_logic; q:out std_logic);end;architecture behavior of shuocun isbegin process(clk) begin if(clkevent and clk=1) then qdisplaydisplaydisplaydisplaydisplaydisplaydisplaydisplaydisplaydisplaydisplay=0000000; end case; else displayen,num=numina,display=lsb); u2:display port map(en1=en,num=numinb,display=middle); u3:display port map(en1=en,num=numinc,display=msb); end;