实验考试FPGA实验代码.doc

上传人:s****u 文档编号:12753344 上传时间:2020-05-22 格式:DOC 页数:20 大小:884.52KB
返回 下载 相关 举报
实验考试FPGA实验代码.doc_第1页
第1页 / 共20页
实验考试FPGA实验代码.doc_第2页
第2页 / 共20页
实验考试FPGA实验代码.doc_第3页
第3页 / 共20页
点击查看更多>>
资源描述
备注:FPGA实验的源程序要与相应的电路结合看并有一定的理解,务必要熟练知道引脚的分配,软件的基本操作详见实验指导书的实验一。实验二:12选1的数据选择器 具体步骤: 第1步:新建一个Quartus项目。 第2步:在Quartus项目中新建一个VHDL文件,并命名为mux_2to1.vhd,实现2选1的电路功能,其真值表和电路符号如下图所示。即当s1时,输出my;当s0时,输出mx。 代码一:VHDL程序代码如下。 library ieee; use ieee.std_logic_1164.all; entity mux_2to1 is port( s,x,y:in std_logic; m:out std_logic); end mux_2to1; architecture behave_mux_2to1 of mux_2to1 is begin m=x when s=0 else y ; end behave_mux_2to1; 2、28位宽2选1的数据选择器 在完成2选1数据选择器之后,将信号x和y的位宽由1位扩展为8位。更改后的电路图如下: 实验代码如下: library ieee; use ieee.std_logic_1164.all; entity mux_2to1_8bit is port( s:in std_logic;x,y:in std_logic_vector(7 downto 0); m:out std_logic); end mux_2to1_8bit; architecture behave_mux_2to1_8bit of mux_2to1_8bit is begin m=x when s=0 else y; end behave_mux_2to1_8bit;第2步:接着把mux_2to1_8bit.vhd设定为项目的顶层设计文件。实现方法如下图所示,在项目浏览器(Project Navigator)中选择文件(Files)页,选中mux_2to1_8bit.vhd,单击右键,选择“Set as Top-Level Entity”命令即可。34选1的数据选择器 在完成2选1电路之后,将电路扩展为4选1数据选择器,电路及其真值表如下图所示。 代码修改如下: library ieee; use ieee.std_logic_1164.all; entity mux_4to1 is port( s:in std_logic_vector(1 downto 0); u,v,w,x:in std_logic; m:out std_logic ); end mux_4to1; architecture behave_mux_4to1 of mux_4to1 is begin m=u when s=00 else v when s=01 else w when s=10 else x; end behave_mux_4to1; 文件另存为mux_4to1.vhd。 接着将mux_4to1.vhd设定为项目的顶层设计文件,再进行语法检查和引脚分配。 4实现3位宽的4选1数据选择器。 电路如下图所示。代码完成后,另存为mux_4to1_3bit.vhd。 library ieee; use ieee.std_logic_1164.all; entity mux_4to1_3bit is port( s:in std_logic_vector(1 downto 0); u,v,w,x:in std_logic_vector(2 downto 0); m:out std_logic_vector(2 downto 0) ); end mux_4to1_3bit; architecture behave_mux_4to1_3bit of mux_4to1_3bit is begin m=u when s=00 else v when s=01 else w when s=10 else x; end behave_mux_4to1_3bit;实验三:七段数码管显示 1显示简单字符 七段数码管显示电路如下图所示: 图中包含一个七段解码器模块,c2c0是解码器的3个输入,当输入值不同时,输出不同的字符。如表中所示,当输入值为100111时,输出空格,即数码管全暗。七段数码管的不同段位用数字06表示,注意七段数码管是共阳极的,即各管段输入低电平时,数码管亮;否则数码管暗。 具体实验步骤如下: 第1步:新建一个Quartus项目。 第2步:新建一个VHDL文件,实现上述七段解码器。具体代码如下: library ieee; use ieee.std_logic_1164.all; entity char_7seg is port( c:in std_logic_vector(2 downto 0); hex:out std_logic_vector(6 downto 0); end char_7seg; architecture behave_char_7seg of char_7seg is begin with c(2 downto 0) select hex= 0001001 when 000 , -H 0000110 when 001 , -E 1000111 when 010 , -L 1000000 when 011 , -O 1111111 when others; - end behave_char_7seg; 保存VHDL文件,并命名为char_7seg.vhd。 第3步:语法检查,通过后,进行引脚分配,分配表如下: 信号 DE2上的器件 C2.0 SW2SW0 hex6.0 HEX06.0 2显示09数字 在完成简单字符显示电路之后,设计一个用于显示09数字的七段数码管电路。电路图如下图所示,c3c0是七段数码器的输入,当输入00001001时,则输出09,如表中所示;当输入10101111时,输出空格。 对上述代码进行相应的修改,并将文件另存为num_7seg.vhd。 接着将num_7seg.vhd设定为项目的顶层设计文件,再进行语法检查和引脚分配。 引脚分配表如下表所示,具体的FPGA引脚可通过查找附录表获取。分配完后重新编译项目文件,并下载验证。 信号 DE2上的器件 C3.0 SW3SW0 hex6.0 HEX06.0 library ieee; use ieee.std_logic_1164.all; entity num_7seg is port( c:in std_logic_vector(3 downto 0); hex:out std_logic_vector(6 downto 0); end num_7seg; architecture behave_num_7seg of num_7seg is begin with c(3 downto 0) select hex= 1000000 when 0000 , -0 1111001 when 0001 , -1 0100100 when 0010 , -2 0110000 when 0011 , -3 0011001 when 0100 , -4 0010010 when 0101 , -5 0000010 when 0110 , -6 1111000 when 0111 , -7 0000000 when 1000 , -8 0010000 when 1001 , -9 1111111 when others; - end behave_num_7seg;3循环显示4个字符 循环显示4个字符的电路图如下: 电路的工作原理是,输入端U、V、W和X的输入值分别是000、001、010和011,通过s1和s0选择四个输入端其中一个作为七段解码器的输入值,从而显示H、L、E和O任一字符。方法二: 通过VHDL代码实现,这里需要用到元件的概念。整个文件的代码如下: library ieee; use ieee.std_logic_1164.all; entity char_4to1_7segv is port( s:in std_logic_vector(1 downto 0); U,V,W,X:in std_logic_vector(2 downto 0); hex0:out std_logic_vector(6 downto 0); end char_4to1_7segv; architecture behave_char_4to1_7segv of char_4to1_7segv is component char_7seg is port( c:in std_logic_vector(2 downto 0); hex:out std_logic_vector(6 downto 0); end component; component mux_4to1_3bit is port( s:in std_logic_vector(1 downto 0); u,v,w,x:in std_logic_vector(2 downto 0); m:out std_logic_vector(2 downto 0); end component; signal mout:std_logic_vector(2 downto 0); begin u1:mux_4to1_3bit port map(s,U,V,W,X,mout); u2:char_7seg port map(mout,hex0) ; end behave_char_4to1_7segv; 代码中用红色标识出来的就是元件的定义和调用部分。将文件另存为char_4to1_7segv.vhd,并将其设定为项目的顶层设计文件。再进行语法检查、引脚分配和编译下载。 实验四:BCD码显示及运算 1二进制码到BCD码的转换 二进制码与BCD码之间的转换关系见下表: 表中将4位二进制输入V=v3v2v1v0转换成2位十进制Dd1d0,实现办法是用SW3.0作为二进制输入,而用HEX1和HEX0作为十进制输出的显示。从上述表中可以看出,当V=9时,d10、d0V;反之,d11、d0V10。 1、BCD二进制转换为十进制library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all;entity bin_bcd is port(v:in std_logic_vector( 3 downto 0); hex0,hex1:out std_logic_vector( 6 downto 0);end entity bin_bcd;architecture behave_bin_bcd of bin_bcd issignal d0,d1:std_logic_vector(3 downto 0);component num_7seg is port( c:in std_logic_vector(3 downto 0); hex:out std_logic_vector(6 downto 0); end component;begin process(v)beginif(v=9) then d1=0000;d0=v;else d1=0001;d0=v-10;end if;end process;dd0:num_7seg port map(d0,hex0);dd1:num_7seg port map(d1,hex1);end behave_bin_bcd;21位BCD加法器 电路原理是输入两个BCD码A和B以及1位进位输入cin,输出是BCD码的和sum以及1位进位输出cout。例如当A1001(9)、B1001(9)、cin1时,cout1,sum1001(9)。电路的输出最大值也就是19。 1位BCD加法器可以利用两个二进制加法器实现,其原理如下图所示。在VHDL中,二进制加法可以直接用A+B实现。不过前提是需要使用std_logic_unsigned.all这个程序包。 2、1位BCD加法library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all;entity bcd_add_1bit is port(A,B:in std_logic_vector( 3 downto 0); cin:in std_logic; hex0:out std_logic_vector( 6 downto 0); ledg0:out std_logic);end entity bcd_add_1bit;architecture behave_bcd_add_1bit of bcd_add_1bit issignal sum:std_logic_vector(3 downto 0);signal cout:std_logic;signal m:std_logic_vector(4 downto 0); signal c:std_logic; component num_7seg is port( c:in std_logic_vector(3 downto 0); hex:out std_logic_vector(6 downto 0); end component;begin process(A,B,cin)begin m=(0&A)+(0&B)+cin; c=m(4) or (m(3) and m(2) or (m(3) and m(1); cout=c; sum=m(3 downto 0)+(0&c&c&0); end process;dd2:num_7seg port map(sum,hex0);ledg0=cout;end behave_bcd_add_1bit;实验五:分频器 CPLD/FPGA与单片机相比,一个非常明显的优势就在于它的高速性。但是因为很多外围器件的驱动需要低频的时钟(若时钟频率太高,则键盘扫描容易出错,七段数码管会闪烁和不稳定等),所以常常需要用到分频电路,其目的是用一个时钟频率生成另一个时钟频率。常用的分频电路又分为偶数倍分频和奇数倍分频两种。1.偶数倍分频 偶数倍分频的原理十分简单,例如8分频率电路设计第1步:新建一个VHDL文件,并另存为clk_div10.vhd。第2步:VHDL代码如下:library ieee; use ieee.std_logic_1164.all; entity clk_div8 is port( clkin :in std_logic; clkout:out std_logic); end clk_div8; architecture behave_clk_div8 of clk_div8 is constant N: Integer:=3;signal Counter:Integer RANGE 0 TO N; signal Clk: Std_Logic;begin process(clkin) begin if rising_edge(clkin) then 每计到4个(03)上升沿,输出信号翻转一次 if Counter=N then Counter=0; Clk=NOT Clk; else Counter= Counter+1; end if; end if; end process; clkout= Clk; end behave_clk_div8; 第3步:语法检查通过后,再进行功能仿真。仿真结果应该如下图所示。 上面给出的是一个8分频率电路,其他倍频数的分频电路可以通过修改Counter的上限值N得到。一般的计算规则是:对一个2x 分频的电路来说,counter上限值是N=x-1(从0计到x-1恰好为x次,每x个上升沿翻转一次就实现了2x分频)。如果希望产生一个下降沿翻转的分频电路,只需将rising_edge( Clkin)改成falling_edge(Clkin)即可。第4步:将DE2平台上的50MHz时钟分频为1Hz频率的时钟,将文件另存为clk_1_gen.vhd。将输入时钟引脚接在CLK_50,输出时钟引脚接在LEDR0,可以看到LEDR0每过1秒亮一下。library ieee; use ieee.std_logic_1164.all; entity clk_1_gen is port( clkin :in std_logic; ledr0:out std_logic); end clk_1_gen; architecture behave_clk_1_gen of clk_1_gen is constant N: Integer:=(25000000-1);signal Counter:Integer RANGE 0 TO N;signal Clk: Std_Logic;begin process(clkin) begin if rising_edge(clkin) then -每计到4个(03)上升沿,输出信号翻转一次 if Counter=N then Counter=0; Clk=NOT Clk; else Counter= Counter+1; end if; end if; end process; ledr0=Clk;end behave_clk_1_gen;三、实验内容1.计数器 在VHDL中,可以用Q=Q+1简单地实现一个计数器,也可以用LPM来实现。下面分别对这两种方法进行介绍。 方法一: 第1步:新建一个Quartus项目。 第2步:建立一个VHDL文件,实现一个8位计数器。计数器从“00000000”开始计到“11111111”,计数器的模是256。计数器模块还需要包含一个时钟clock、一个使能信号en、一个异步清0信号aclr和一个同步数据加载信号sload。模块符号如下图所示: 第3步:VHDL代码如下: library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity counter_8bit is port( clock,en,sload,aclr:in std_logic; data:in std_logic_vector(7 downto 0); q:buffer std_logic_vector(7 downto 0); end counter_8bit; architecture behave_counter_8bit of counter_8bit is begin process(clock,aclr) begin if (aclr=1) then q=00000000; else if rising_edge(clock) then if sload=1 then q=data; elsif en=1 then q=q+1; end if; end if; end if; end process; end behave_counter_8bit; 第4步:将VHDL文件另存为counter_8bit.vhd,并将其设定为项目的最顶层文件,再进行语法检查。 第5步:语法检查通过以后,用KEY0表示clock,SW7.0表示data,SW810分别表示en、sload和aclr;LEDR7.0表示q。 第6步:引脚分配完成后,编译并下载。 第7步:修改上述代码,把计数器的模更改为100,应如何操作。 计数器的模更改为100,代码如下:library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity counter_7bit is port( clock,en,sload,aclr:in std_logic; data:in std_logic_vector(6 downto 0); q:buffer std_logic_vector(6 downto 0); end counter_7bit; architecture behave_counter_7bit of counter_7bit is begin process(clock,aclr) begin if (aclr=1) then q=0000000; else if rising_edge(clock) then if sload=1 then q=data; elsif en=1 then q1100011) then q=0000000; end if; end if; end if; end if; end process; end behave_counter_7bit;2.时钟电路 利用上面设计好的计数器和分频器设计一个实时的时钟。一共需要1个模24计数器、2个模6计数器、2个模10计数器、一个生成1Hz的分频器和6个数码管解码器。最终用HEX5HEX4显示小时(023),用HEX3HEX2显示分钟(059),用HEX1HEX0显示秒钟(059)。 (1)下面是模24的计数器的设计: 第1步:新建一个VHDL文件,并另存为counter_24.vhd。 第2步:输入以下代码: library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity counter_24 is port( clk,en,clr:in std_logic; q1,q0:buffer std_logic_vector(3 downto 0); end counter_24; architecture behave_counter_24 of counter_24 is signal c:std_logic; signal q:integer range 0 to 23; begin process(clk,clr) begin if (clr=0) then q0=0000;q12) or (q09) or (q1=2) and (q0=3) then q0=0000;q1=0000; elsif q0=9 then q0=0000;q1=q1+1; else q0=q0+1; end if; end if; end if; end if; end process; end behave_counter_24; (2)一个生成1Hz的分频器counter_10,代码如下:LIBRARY ieee;USE ieee.std_logic_1164.all;Use ieee.std_logic_unsigned.all;ENTITY counter_10 ISGeneric(N:integer:=10);port(clk,clr,en: in std_logic;q: buffer integer range 0 to 9;cout: out std_logic);END counter_10;ARCHITECTURE behave OF counter_10 ISbegincout=1 when (q=9) else 0;process(clk)beginif clr=1 then q=0;elsif en=1 then if falling_edge(clk) THENif qN-1 then q=q+1;else q=0;end if;end if;end if;end process;end behave;(3)模6计数器:counter_6:LIBRARY ieee;USE ieee.std_logic_1164.all;Use ieee.std_logic_unsigned.all;ENTITY counter_6 ISGeneric(N:integer:=6);port(clk,clr,en: in std_logic;q: buffer integer range 0 to 9;cout: out std_logic);END counter_6;ARCHITECTURE behave OF counter_6 ISbegincout=1 when (q=5) else 0;process(clk)beginif clr=1 then q=0;elsif en=1 then if falling_edge(clk) THENif qN-1 then q=q+1;else q=0;end if;end if;end if;end process;end behave;(4)七段数码管代码char_7seg:library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all;entity char_7seg is port( c:in std_logic_vector(3 downto 0); hex:out std_logic_vector(6 downto 0); end char_7seg; architecture behave of char_7seg is begin with c(3 downto 0) select hex=1000000 when 0000 ,-0 1111001 when 0001 ,-1 0100100 when 0010 ,-2 0110000 when 0011 ,-3 0011001 when 0100 ,-4 0010010 when 0101 ,-5 0000010 when 0110 ,-6 1111000 when 0111 ,-7 0000000 when 1000 ,-8 0010000 when 1001 ,-9 1111111 when others; - end behave;第3步:语法检查,通过后直接生成符号。 第4步:采用图形编辑器,将几个模块连接起来构成一个时钟。最终效果如下图所示: 第5步:在加上相应的输入和输出端口,保存为clock.bdf。 第6步:语法分析、编译和下载,观察实验现象。 引脚分配程序导入开发板测试成功!
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!