8.2LED控制VHDL程序与仿真本节分别介绍采用FPGA对LED进行静态和动态显示的数字时钟控制程序。1.例1:FPGA驱动LED静态显示--文件名:decoder.vhd。--功能:译码输出模块,LED为共阳接法。--最后修改日期:2004.3.24。libraryIEEE;useIEEE.STD_LOGIC_1164.ALL;useIEEE.STD_LOGIC_ARITH.ALL;useIEEE.STD_LOGIC_UNSIGNED.ALL;entitydecoderisPort(seg:instd_logic_vector(3downto0);--四位二进制码输入q3:outstd_logic_vector(6downto0));--输出LED七段码enddecoder;architectureBehavioralofdecoderisbeginprocess(seg)begincasesegiswhen"0000"=>q3<="0000001";--0when"0001"=>q3<="1001111";--1when"0010"=>q3<="0010010";--2when"0011"=>q3<="0000110";--3when"0100"=>q3<="1001100"--4when"0101"=>q3<="0100100";--5when"0110"=>q3<="0100000";--6when"0111"=>q3<="0001111";--7when"1000"=>q3<="0000000";--8when"1001"=>q3<="0000100";--9whenothers=>q3<="1111111";endcase;endprocess;endBehavioral;2.例2:FPGA驱动LED动态显示(4位)--文件名:dynamic.vhd。--功能:动态扫描模块,位选信号高电平有效。--最后修改日期:2004.3.24。libraryIEEE;useIEEE.STD_LOGIC_1164.ALL;useIEEE.STD_LOGIC_ARITH.ALL;useIEEE.STD_LOGIC_UNSIGNED.ALL;entitydynamicisPort(clk:instd_logic;reset:instd_logic;din1:instd_logic_vector(6downto0);--译码后的数据信号1(4位2进制数据通过例1中的decoder模块译码得到din1,din2,din3,din4)din2:instd_logic_vector(6downto0);--译码后的数据信号2din3:instd_logic_vector(6downto0);--译码后的数据信号3din4:instd_logic_vector(6downto0);--译码后的数据信号4shift:outstd_logic_vector(3downto0);--位选信号bus4:outstd_logic_vector(6downto0));--数据信号enddynamic;architectureBehavioralofdynamicissignalscan_clk:std_logic_vector(1downto0);beginprocess(clk,scan_clk,reset)--分频进程variablescan:std_logic_vector(17downto0);beginifreset='1'thenscan:="000000000000000000";scan_clk<="00";elsifclk'eventandclk='1'thenscan:=scan+1;endif;scan_clk<=scan(17downto16);endprocess;process(scan_clk,din1,din2,din3,din4)--扫描进程begincasescan_clkiswhen"00"=>bus4<=din1;shift<="0001";when"01"=>bus4<=din2;shift<="0010";when"10"=>bus4<=din3;shift<="0100";when"11"=>bus4<=din4;shift<="1000";whenothers=>bus4<="0000000";shift<="0000";endcase;endprocess;endBehavioral;