
实例教学六 流水灯 cpld fpga可编程逻辑器件.ppt
28页流水灯 的VHDL设计,桂林师范高等专科学校 羊日飞,流水灯实例运行环境,红色飓风二代XILINX FPGA开发板 RC2-3S400,流水灯板级硬件电路,CPLD的引脚作输出:,,“0” LED 亮,“1” LED 灭,流水灯的点亮方案,周期是15s,这个过程不断循环重复,,流水灯芯片级设计框图,时钟源 50MHz,分频器,计数器,LED驱动,8位LED,,,,,CPLD,1/50000000分频 输出时钟信号的频率是1Hz,周期为1s16进制计数器 即有16种状态,每经过1s计数值加“1”,也即跳到下一个状态译码器 将16种状态码转换为相应的8位二进制LED驱动码1/8分频器(Divider),architecture clkdiv_stru of clkdiv is signal count: std_logic_vector(3 downto 0); begin process( reset , clock) begin if reset=‘0’ then count=“0000”; q=‘0’; elsif clock’event and clock=‘1’ then if count=4 then count=“0000”; q= not q; else count=count+ “0001” ; end if; end if; end process; end clkdiv_stru;,VHDL语法 ——信号的数据类型,已学:std_logic(标准逻辑位)、std_logic_vector (标准逻辑矢量) 整型 integer VHDL提供了一个预定义整型数据类型,即integer。
integer类型数据的取值范围: -2,147,483,647~+2,147,483,647 信号定义为整型: 例如:signal A: integer; 定义时,可以指定该信号的取值范围,使用关键字range…to… 例如:signal A: integer range 0 to 100; 整型信号的赋值: 例如: signal A: integer := 4; B=6;,1/50000000分频器(Divider),,entity clkdiv is port( clk: in std_logic; q: buffer std_logic; ); end clkdiv;,entity clkdiv is port( clk: in std_logic; q: buffer std_logic; ); end clkdiv;,1/8分频器,1/50000000分频器,实体部分 不变,1/50000000分频器(Divider),architecture clkdiv_stru of clkdiv is signal count: std_logic_vector(3 downto 0); begin process( reset , clock) begin if reset=‘0’ then count=“0000”; q=‘0’; elsif clock’event and clock=‘1’ then if count=4 then count=“0000”; q= not q; else count=count+ “0001” ; end if; end if; end process; end clkdiv_stru;,architecture clkdiv_stru of clkdiv is signal count: integer range 0 to 25000000:=0; begin process( reset , clock) begin if reset=‘0’ then count=0; q=‘0’; elsif clock’event and clock=‘1’ then if count=25000000 then count=0; q= not q; else count=count+1; end if; end if; end process; end clkdiv_stru;,,16进制计数器,,entity counter is port( reset: in std_logic; clock: in std_logic; Q: out std_logic_vector(3 downto 0) ); end counter;,architecture counter_stru of counter is signal count: std_logic_vector(3 downto 0); begin process( reset , clock) begin if reset=‘0’ then count=“0000”; elsif clock’event and clock=‘1’ then if count=“1111” then count=“0000”; else count=count+ “0001” ; end if; end if; end process; Q=count; end counter_stru;,LED驱动译码器,实体,LED驱动,,,8位LED控制码输出,每位控制一个LED,4位状态码输入 “0000”~ “1111”,entity decoder is port( state: in std_logic_vector(3 downto 0); Y: out std_logic_vector(7 downto 0) ); end decoder ;,译码器的VHDL代码,architecture decoder38_stru of decoder38 is signal comb : std_logic_vector(2 downto 0); begin comb Y Y Y Y Y Y Y Y Y = “ZZZZZZZZ”; end case; end process; end decoder38_stru;,译码器的VHDL代码,architecture decoder38_stru of decoder38 is signal comb : std_logic_vector(2 downto 0); begin comb Y Y Y Y Y Y Y Y Y = “ZZZZZZZZ”; end case; end process; end decoder38_stru;,architecture decoder_stru of decoder is begin process(state) begin case state is when “0000” = Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y = “ZZZZZZZZ”; end case; end process; end decoder_stru;,,模块化的系统设计,三个模块如何组合起来? 它们之间又是通过什么方式连接?,分频器,计数器,LED驱动,clkdiv.vhd,counter.vhd,decoder.vhd,VHDL语法 ——层次化建模和元件例化,层次化建模是一种模块化的设计方法。
顶层模块:代表一个完整的设计,它的输入输出端口直接连接到CPLD/FPGA芯片的引脚,顶层模块内部包含若干个子模块 子模块:实现某个独立功能的实体模块,它的输入输出端口出现在顶层模块内部子模块 1,,子模块 2,,子模块 3,,,顶层模块,流水灯的层次化建模,顶层模块:flowlight,,时钟源 50MHz,分频器,计数器,LED驱动,8位LED,,,,,CPLD,流水灯的层次化建模,顶层模块:flowlight,--实体 entity flowlight is port( reset: in std_logic; mclk: in std_logic; ledout: out std_logic_vector(7 downto 0) ); end flowlight; --结构体 architecture flowlight_stru of flowlight is Begin …… end flowlight_stru;,流水灯的层次化建模,子模块:clkdiv(分频器)、counter(计数器)、decoder(译码器),entity clkdiv is port( clk: in std_logic; q: buffer std_logic; ); end clkdiv;,clkdiv.vhd,entity counter is port( reset: in std_logic; clock: in std_logic; Q: out std_logic_vector(3 downto 0) ); end counter;,counter.vhd,entity decoder is port( state: in std_logic_vector(3 downto 0); Y: out std_logic_vector(7 downto 0) ); end decoder ;,decoder.vhd,VHDL语法 ——层次化建模和元件例化,顶层模块与子模块的关系: 顶层模块包含若干个子模块; 顶层模块中不描述子模块的具体内容,子模块的具体内容在单独的VHDL源文件中,每个子模块也是一个独立的实体。
顶层模块中包含子模块的元件声明和元件例化子模块 1,,子模块 2,,子模块 3,,,顶层模块,VHDL语法 ——元件声明,语法格式,元件声明的语法格式: architecture 顶层模块结构体名 of 顶层模块实体名 is …… component 元件名/子模块实体名 port( 端口名:信号模式 信号类型; …. ); end component; …… begin …… end 顶层模块结构体名;,流水灯的顶层实体 ——结构体部分,architecture flowlight_stru of flowlight is component clkdiv port( clk: in std_logic; q: buffer std_logic); end component; component counter port( reset: in std_logic; clock: in std_logic; Q: out std_logic_vector(3 downto 0)); end component; component decoder port( state: in std_logic_vector(3 downto 0); Y: out std_logic_vector(7 downto 0)); end component; Begin …… end flowlight_stru;,特别注意: 元件声明出现在结构体Begin之前,VHDL语法 ——元件例化,语法格式,元件例化的语法格式: architecture 顶层模块结构体名 of 顶层模块实体名 is 元件声明 begin 实例名:元件名/子模块实体名 port map( …… 子模块端口信号=顶层实体信号, …… ); …… end 顶层模块结构体名;,流水灯的顶层实体——结构体部分,architecture flowlight_stru of flowlight is component clkdiv port( clk: in std_logic; q: buffer std_logic); end component; …… Begin clkdiv_m:clkdiv port map( clk=?, q=? ); …… end flowlight_stru;,应该连接到顶层实体的输入时钟信号 res。












