1、FPGA设计数字系统例1 矩形波发生器-2 例2 三角波发生器-15 例3 数字频率计-22 例4 数字钟-32 例5 交通灯控制器-43 例6 LED字符显示器-52 例7 LED跑马灯-55 例8 过河游戏-59 例9 拔河游戏-69 例10 键盘输入显示器-82 附 录 I用VHDL设计数字系统实例 为了拓宽设计思路,在光盘中再举出一些用VHDL设计数字系统的实例。以下实例只提供简单的 总体框图和简单的注释作为提示,未涉及之处留给读者思考、开拓。 例1 矩形波发生器 1. 设计任务及要求 (1) 设计一个矩形波发生器,输出频率范围为10 Hz1 kHz,分如下两挡: 1 10Hz100
2、Hz 挡,其频率可调节,频率递增步进长度为10Hz。 2 100 Hz1 kHz挡,其频率可调节,频率递增步进长度为100 Hz。 (2) 输出矩形波的占空比在1090之间可调,调节的递增步进长度为10。 (3) 输出电压峰峰值为5 V。 (4) 每个周期信号的构造数据由30个取样点的值组成。 2. 可选器件 EPM7128S、共阴极七段数码管、DAC0832、LM741、开关、电阻和电容。 3. 设计总体框图 矩形波发生器总体框图如附图1.1 所示。 附图1.1 矩形波发生器总体框图 4. 源程序及注释 (1) 上层模块 library ieee; use ieee.std_logic_11
3、64.all; use ieee.std_logic_signed.all; use ieee.std_logic_unsigned.all; entity cheif is port(clk_1m : in std_logic;-时钟信号,1MHz。 k1 : in std_logic;-调节频率的开关信号。 k2 : in std_logic;-调节占空比的开关信号。 k3 : in std_logic;-换挡开关信号。 sel : outstd_logic_vector(5 downto 0);-数码管片选信号。 d : out std_logic_vector(6 downto 0);
4、-数码管的驱动信号。 f : out std_logic_vector(7 downto 0);-输出给DAC0832的8位数字信号。 end cheif ; architecture aa of cheif is signal mode_mid: integer range 0 to 1; signal clk_125_mid : std_logic; signal s1: std_logic; signal s2 : std_logic; signal s3: std_logic; signal fs_mid: std_logic; signal statusf_mid : integer
5、 range 0 to 9; signal statush_mid : integer range 0 to 8; component keyin-调用防抖动模块。 port(k : in std_logic; kout :buffer std_logic; clk_125 : in std_logic); end component; component fp-调用分频及控制模块。 port(clk_1m: in std_logic; s1 : in std_logic; s3 : in std_logic; statusf : out integer range 0 to 9; mode
6、: out integer range 0 to 1; fs : out std_logic ); end component; component output-调用信号输出模块。 port(fs: in std_logic; s2 : in std_logic; statush: out integer range 0 to 8; f : out std_logic_vector(7downto 0) ); end component; component display-调用分频及显示模块。 port(statusf : in integer range 0 to 9; statush
7、: in integer range 0 to 8; mode : in integer range 0 to 1; clk_1m : in std_logic; clk_125 : out std_logic; sel : out std_logic_vector(5 downto 0); d : out std_logic_vector(6 downto 0) ); end component; begin u1 : keyin port map (k1,s1,clk_125_mid); u2 : keyin port map (k2,s2,clk_125_mid); u3 : keyin
8、 port map (k3,s3,clk_125_mid); u4 : fp port map (clk_1m,s1,s3,statusf_mid,mode_mid,fs_mid); u5 : output port map (fs_mid,s2,statush_mid,f); u6 : display port map (statusf_mid, statush_mid, mode_mid, clk_1m, clk_125_mid, sel,d); end aa; (2) 下层模块 防抖动模块 library ieee; use ieee.std_logic_1164.all; use ie
9、ee.std_logic_unsigned.all; entity keyin is port(k : in std_logic;-有抖动的开关信号。 kout : buffer std_logic;-消抖动后的开关信号。 clk_125 : in std_logic);-时钟信号,125 Hz。 end keyin; architecture aa of keyin is signal a,d1,d2,s,r,q1,q2,b : std_logic; begin p1 :process(clk_125) begin if(clk_125=0) then d1=k; d2=d1; end if
10、; end process; p2 :processbegin s=d1 and d2; r=(not d1) and (not d2); a=s or (not r) and a); end process; p3 :process(clk_125) begin if (clk_125event and clk_125=1) then q2=q1; q1=a; end if; b=q1 and not q2; kout= b; end process; end aa; 分频及控制模块 library ieee; use ieee.std_logic_1164.all; use ieee.st
11、d_logic_signed.all; use ieee.std_logic_unsigned.all; entity fp is port(clk_1m: in std_logic;-时钟信号,1MHz。 s1 : in std_logic;-经过防抖后输入的调节频率的开关信号。 s3 : in std_logic;-经过防抖后输入的换挡开关信号。 statusf : out integer range 0 to 9;-改变频率的控制计数器。 mode: out integer range 0 to 1;-换挡控制信号。 fs : out std_logic);-取样信号。 end fp ;
12、 architecture aa of fp is signal n : integer range 0 to 166; signal m : integerrange 0 to 1; signal status : integer range 0 to 9; signal fs2 : std_logic; signal fs2_10 : std_logic; signal fs1 : std_logic; signal count : integer range 0 to 166; signal c: integer range 0 to 10; begin process(s1,s3) b
13、egin if(s1event and s1=1)thenif(status=9)then status=0; else status=status+1; end if; end if; if(s3event and s3=1)then if(m=1)then m=0; elsif(m=0)then mnnnnnnnnnnn=null; end case; end process; process(clk_1m) begin if(clk_1mevent and clk_1m=1)then if(count=n)then-n的值不同,输出信号的频率也不同。 count=1; fs2=1; el
14、se count=count+1; fs2=0; end if; else null; end if; end process ; process(fs2) beginif(fs2event and fs2=1)then-实现换挡功能。 if(m=0)then fs2_10=fs2; else if(c=9)then c=0; fs2_10=0; else c=c+1; fs2_10=1; end if; end if; end if; end process ; process(fs2_10) begin if(fs2_10event and fs2_10=1)then fs1=not fs
15、1;-2分频后得到不同频率的取样信号。 end if; end process; fs=fs1; statusf=status; mode=m; end aa ; 信号输出模块 library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_signed.all; entity output is port(fs : in std_logic;-取样信号。 s2: in std_logic;-经过防抖后输入的调节占空比的开关信号。 statush : out integer range 0 to 8;-改变占空比的控制计数器。 f:
16、out std_logic_vector(7downto 0);-输出给DAC0832的8位数字信号。 end output; architecture aa of output is signal m: integer range 0 to 29; signal status : integer range 0 to 8; begin p1 :process(s2) begin 777776(s2event and s2=if 1)then if(status=8)then status=0; else status=status+1; end if; end if; end process
17、 p1; p3 :process(fs)-本进程描述一个用于调节脉宽的控制计数器。 begin if (fsevent and fs=1) then if(m=29)then m=0; else m case m is-当mffff case m is。 when 0=fffffff case m iswhen 0=ffffffffff case m is when 0=fffffffffffff case m is when 0=ffffffffffffffff case m is when 18=fffffffffffff case m is when 21=ffffffffff case
18、 m is when 24=fffffff case m is when 27=fffff=null; end case; end process p2; statush=status; end aa ; 分频及显示模块 library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_signed.all; use ieee.std_logic_unsigned.all; entity display is port(statusf : in integer range 0 to 9;-改变显示频率的控制计数器。 statush :
19、in integer range 0 to 8;-改变显示占空比的控制计数器。 mode : in integer range 0 to 1;-换挡控制信号。 clk_1m : in std_logic;-时钟信号,1 MHz。 clk_125 : out std_logic;-防抖动模块的时钟信号。 sel : out std_logic_vector(5 downto 0);-数码管片选信号。 d : out std_logic_vector(6 downto 0) );-数码管的驱动信号。 end display; architecture aa of display is signal n to 1999;: integer range 0 signal clk_500 : std_logic; signal clk_250 : std_logic; signal clk_125_mid
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1