文档详情

8VHDL语言状态机汇总课件

ni****g
实名认证
店铺
PPT
1.30MB
约72页
文档ID:590313970
8VHDL语言状态机汇总课件_第1页
1/72

第八章第八章有限状态机有限状态机FSMFSM的设计的设计 哈尔滨工业大学哈尔滨工业大学(威海威海) 信息工程学院电子工程系信息工程学院电子工程系 第八章第八章 有限状态机有限状态机FSMFSM的设计的设计 1. .状态机概念状态机概念2.2.一般状态机一般状态机3.3.摩尔状态机摩尔状态机4.4.米利状态机米利状态机5.5.状态机实例状态机实例 一一 . .有限状态机有限状态机的基本概念的基本概念有限状态机:指那些输出取决于过去输入部有限状态机:指那些输出取决于过去输入部分和当前输入部分的时序逻辑电路有限状分和当前输入部分的时序逻辑电路有限状态机一般有三部分:输入部分、输出部分和态机一般有三部分:输入部分、输出部分和状态寄存器状态寄存器Moore型有限状态机:输出信号仅与当前状型有限状态机:输出信号仅与当前状态有关态有关Mealy型有限状态机:输出信号不仅与当前型有限状态机:输出信号不仅与当前状态有关,而且还与所有的输入信号有关。

状态有关,而且还与所有的输入信号有关 状态机的特点状态机的特点克服了纯硬件数字系统顺序方式控制不灵活的缺克服了纯硬件数字系统顺序方式控制不灵活的缺点VHDL综合器易于优化综合器易于优化易构成性能良好的时序逻辑模块易构成性能良好的时序逻辑模块结构模式简单、层次分明、易读易懂、易排错结构模式简单、层次分明、易读易懂、易排错利用同步时序和全局时钟线可实现高速利用同步时序和全局时钟线可实现高速FSM运行模式类似于运行模式类似于CPU,,易于进行顺序控制易于进行顺序控制高可靠性,非法状态易控制高可靠性,非法状态易控制 时序电路的结构与特点时序电路的结构与特点1 1 状态机分类状态机分类根据输出函数的形式,可以分为根据输出函数的形式,可以分为mooremoore机和机和mealymealy机两类 一般状态机一般状态机MOORE状态机状态机MEALY状态机状态机1 1 状态机分类状态机分类 FSMFSM的表达形式的表达形式通常采用状态转换图表达电路信号的变化:通常采用状态转换图表达电路信号的变化:Moore: y = f(s) Mealy: y = f(s0,x)Moore: y = f(s) Mealy: y = f(s0,x) 1 1 状态机分类状态机分类 Moore型有限状态机的结构图 Mealy型有限状态机的结构图 FSMFSM的设计要点的设计要点定义枚举类型表达不同的状态特点;定义枚举类型表达不同的状态特点;设置信号表达现有状态和转换的状态;设置信号表达现有状态和转换的状态;对每一个现态,利用选择语句,根据控制条对每一个现态,利用选择语句,根据控制条件件x x决定转换次态决定转换次态; ; 根据现态和根据现态和x x决定输出决定输出y y。

1 1 状态机分类状态机分类 状态机的基本操作状态机内部状态转换状态机的下一个状态状态机内部状态转换状态机的下一个状态由译码器根据当前状态和输入条件决定由译码器根据当前状态和输入条件决定产生输出信号序列输出信号由输出译码器产生输出信号序列输出信号由输出译码器根据当前状态和输入条件决定根据当前状态和输入条件决定1 1 状态机分类状态机分类 为了能获得可综合的,高效的vhdl状态描述,建议使用枚举数据类型来定义状态机的状态,并使用多进程方式来描述状态机的内部逻辑•一般情况下,一个进程描述时序逻辑,包括状态寄存器的工作和寄存器状态的输出另一个进程描述组合逻辑,包括进程间状态值的传递逻辑以及状态以及状态转换值的输出必要时还可以引入第三个进程完成其他的逻辑功能2.2.一般状态机一般状态机 状态机的编码方式 Binary、gray-code编码使用最少的触发器,较多的组合逻辑资源,而one-hot编码反之 另一方面,对于小型设计使用Binary、gray-code更有效,而大型状态机使用one-hot编码更高效 十位数二进制码格林码独热码约翰逊0000000000000_0000_0000_00010000_00001000100010000_0000_0000_00100000_00012001000110000_0000_0000_01000000_00113001100100000_0000_0000_10000000_01114010001100000_0000_0001_00000000_11115010101110000_0000_0010_00000001_11116011001010000_0000_0100_00000011_11117011101000000_0000_1000_00000111_11118100011000000_0001_0000_00001111_11119100111010000_0010_0000_00001111_111010101011110000_0100_0000_0000111_1110011101111100000_1000_0000_0000111_1100012110010110001_0000_0000_0000111_1000013110110100010_0000_0000_0000111_0000014111010010100_0000_0000_0000110_0000015111110001000_0000_0000_0000100_00000 使用使用两段式状态机设计方法两段式状态机设计方法 设计状态机的方法多种多样,但总结起来有两大类:第一种,将状态转移和状态的操作、判断等写在一起;另一种是将状态转移单独写成一个部分,将状态的操作和判断写到另一个部分中。

第二种设计方法较好,将同步时序和组合逻辑分别放到不同的程序块中实现不仅仅便于阅读、理解、维护,更有利于综合器优化代码,利于用户添加合适的时序约束条件,利于布局布线器实现设计 一般状态机实例library IEEE;use IEEE.STD_LOGIC_1164.all;entity s_machine is port( clk : in STD_LOGIC; rst : in STD_LOGIC; state_input : in STD_LOGIC_VECTOR(1 downto 0); comb_output : out STD_LOGIC_VECTOR(1 downto 0) );end s_machine;architecture s_machine of s_machine istype states is (st0,st1,st2,st3);signal cur_state,next_state : states;2.2.一般状态机一般状态机 beginbeginprocess(clk,rst)process(clk,rst)beginbegin if rst='1' then if rst='1' then cur_state<=st0; cur_state<=st0; elsif clk'event and clk='1' then elsif clk'event and clk='1' then cur_state<=next_state; cur_state<=next_state; end if; end if; end process; end process; process(cur_state,next_state) process(cur_state,next_state)beginbegin case cur_state is case cur_state is when st0=>comb_output<="00"; when st0=>comb_output<="00"; if state_input="00" then if state_input="00" then next_state<=st0; next_state<=st0; else else next_state<=st1; next_state<=st1; end if; end if; when st1=>comb_output<="01";when st1=>comb_output<="01"; if state_input="00" then if state_input="00" then next_state<=st1; next_state<=st1; else else next_state<=st2; next_state<=st2; end if; end if; when st2=>comb_output<="10"; when st2=>comb_output<="10"; if state_input="11" then if state_input="11" then next_state<=st2; next_state<=st2; else else next_state<=st3; next_state<=st3; end if; end if; when st3=>comb_output<="11"; when st3=>comb_output<="11"; if state_input="11" then if state_input="11" then next_state<=st3; next_state<=st3; else else next_state<=st0; next_state<=st0; end if; end if; end case; end case; end process; end process;end s_machine;end s_machine; 111序列检测器例:111序列检测器的功能是连续输入三个或三个以上的1时,电路输出为1,其余情况下输出为0。

1/00/01/01/11/10/00/00/0s0s1s2s3s0:初始状态,电路还未收到一个有效1 s1:收到一个1后的状态S2:连续收到两个1后的状态 s3:连续收到三个1个后的状态 111序列检测器程序library IEEE;use IEEE.STD_LOGIC_1164.all;entity s_machine is port( clk : in STD_LOGIC; rst : in STD_LOGIC; din : in STD_LOGIC; comb_output : out STD_LOGIC );end s_machine;architecture s_machine of s_machine istype states is (st0,st1,st2,st3);signal cur_state,next_state : states; beginbeginprocess(clk,rst)process(clk,rst)beginbegin if rst='1' then if rst='1' then cur_state<=st0; cur_state<=st0; elsif clk'event and clk='1' then elsif clk'event and clk='1' then cur_state<=next_state; cur_state<=next_state; end if; end if; end process; end process; process(cur_state,next_state) process(cur_state,next_state)beginbegin case cur_state is case cur_state is when st0=>comb_output<= when st0=>comb_output<=‘‘0 0’’; ; if din= if din=‘‘1 1’’ then then next_state<=st1; next_state<=st1; else else next_state<=st0; next_state<=st0; end if; end if; when st1=>comb_output<= when st1=>comb_output<=‘‘0 0’’; ; if din= if din=‘‘1 1’’ then then next_state<=st2; next_state<=st2; else else next_state<=st0; next_state<=st0; end if; end if; when st2=> when st2=> if din= if din=‘‘1 1’’ then then next_state<=st3; next_state<=st3; comb_output<= comb_output<=‘‘1 1’’; ; else next_state<=st0; else next_state<=st0; comb_output<= comb_output<=‘‘0 0’’; ; end if; end if; when st3=> if state_input= when st3=> if state_input=‘‘1 1’’ then then next_state<=st3; next_state<=st3; comb_output<= comb_output<=‘‘1 1’’; ; else next_state<=st0; else next_state<=st0; comb_output<= comb_output<=‘‘0 0’’; ; end if; end if; end case; end case; end process; end process;end s_machine;end s_machine; 3.3.摩尔状态机摩尔状态机Moore状态机的状态机的输出出只与有限状态自动机的当只与有限状态自动机的当前状态有关,与前状态有关,与输入入信号的当前值无关。

信号的当前值无关 Moore有限状态机在时钟有限状态机在时钟CLOCK脉冲的有效边脉冲的有效边沿后的有限个门延后,输出达到稳定值即使沿后的有限个门延后,输出达到稳定值即使在一个时钟周期内输入信号发生变化在一个时钟周期内输入信号发生变化,输出也会输出也会在一个完整的时钟周期内保持稳定值而不变在一个完整的时钟周期内保持稳定值而不变输入对输出的影响要到下一个时钟周期才能反输入对输出的影响要到下一个时钟周期才能反映出来 •Moore有限状态机最重要的特点就是将有限状态机最重要的特点就是将输入与输出信号隔离开来输入与输出信号隔离开来 MooreMoore机设计:例机设计:例1 1 例例1 1 简单的简单的MooreMoore状态机设计状态机设计状态转换图如下所示,要求设置状态转换图如下所示,要求设置resetreset控制,控制,能直接使状态处于能直接使状态处于s0s03.3.摩尔状态机摩尔状态机 library ieee; use ieee.std_logic_1164.all;entity statmach is port(clk,input,reset: in std_logic; output: out std_logic);end statmach;MooreMoore机设计:例机设计:例1 1 3.3.摩尔状态机摩尔状态机 architecture beh of statmach is type state_type is (s0,s1); --采用枚举法设置状态采用枚举法设置状态 signal st: state_type; --表达当前的状态表达当前的状态begin process(clk) --设置时钟控制模块设置时钟控制模块 begin if reset='1' then st<=s0;--异步复位异步复位 elsif clk'event and clk='1' then --边沿检边沿检测测MooreMoore机设计:例机设计:例1 1 3.3.摩尔状态机摩尔状态机 case st is --根据现态决定下一状态根据现态决定下一状态 when s0 => st<=s1; when s1 => if input='1' then st<=s0; end if; end case; end if; end process; output<='1' when st=s1 else'0'; --根据状态决定输出根据状态决定输出end beh;MooreMoore机设计:例机设计:例1 1 3.3.摩尔状态机摩尔状态机 例例2 Moore2 Moore状态机的设计状态机的设计该状态机有该状态机有5 5个状态,转换图如下所示:其中个状态,转换图如下所示:其中输入控制输入控制IDID为为4 4位二进制数,在图中表达为位二进制数,在图中表达为1616进制数;进制数;MooreMoore机设计:例机设计:例2 2 3.3.摩尔状态机摩尔状态机 library ieee;use ieee.std_logic_1164.all;entity moore2 is port(clk,rst: in std_logic;id: in std_logic_vector(3 downto 0);y: out std_logic_vector(1 downto 0));end moore2;MooreMoore机设计:例机设计:例2 2 3.3.摩尔状态机摩尔状态机 architecture beh of moore2 is signal st: std_logic_vector(2 downto 0);--状态表达状态表达 --各状态命名并根据输出的特点进行赋值各状态命名并根据输出的特点进行赋值 constant s0:std_logic_vector(2 downto 0):="000"; constant s1:std_logic_vector(2 downto 0):="010"; constant s2:std_logic_vector(2 downto 0):="011"; constant s3:std_logic_vector(2 downto 0):="110"; constant s4:std_logic_vector(2 downto 0):="111";begin MooreMoore机设计:例机设计:例2 2 process(clk,rst) --状态转移关系状态转移关系begin if rst='1' then st<=s0; --异步复位异步复位 elsif clk'event and clk='1' then --时钟边沿判断时钟边沿判断 case st is when s0=> if id=x"3" then st<=s1; else st<=s0; end if; when s1=> st<=s2; when s2=> if id=x"7" then st<=s3; end if;MooreMoore机设计:例机设计:例2 2 3.3.摩尔状态机摩尔状态机 when s3=> if id=x"7" then st<=s0; elsif id=x"9" then st<=s4; end if; when s4=> if id=x"b" then st<=s0; end if; when others=>st<=s0; end case; end if; end process;y<=st(1 downto 0); --输出方程输出方程end beh;MooreMoore机设计:例机设计:例2 2 3.3.摩尔状态机摩尔状态机 例例3 3 简单状态机设计简单状态机设计初始态:初始态:z=0z=0连续连续2 2个触发沿个触发沿A=1A=1,则,则z=1;z=1;若若z=1z=1且且b=1,b=1, 则则z z保持保持1 1。

MooreMoore机设计:例机设计:例3 3 3.3.摩尔状态机摩尔状态机 例例3 3 简单状态机设计简单状态机设计MooreMoore机设计:例机设计:例3 3 3.3.摩尔状态机摩尔状态机 architecture beh of smexamp is type sreg_type is (init,a0,a1,ok0,ok1); signal sreg:sreg_type;begin process(clk) begin if clk'event and clk='1' then case sreg is when init => if a='0' then sreg<=a0; elsif a='1' then sreg<=a1; end if;MooreMoore机设计:例机设计:例3 3 when a0 => if a='0' then sreg<=ok0; elsif a='1' then sreg<=a1; end if;when a1 => if a='0' then sreg<=a0; elsif a='1' then sreg<=ok1; end if;when ok0 => if a='0' then sreg<=ok0; elsif a='1' and b='0' then sreg<=a1; elsif a='1' and b='1' then sreg<=ok1; end if; MooreMoore机设计:例机设计:例3 3 when ok1 => if a='0' and b='0' then sreg<=a0;elsif a='0' and b='1' then sreg<=ok0; elsif a='1' then sreg<=ok1; end if;when others =>sreg<=init; end case; end if; end process; MooreMoore机设计:例机设计:例3 3 with sreg select --根据状态决定输出根据状态决定输出 z<= '0' when init|a0|a1, '1' when ok0|ok1, '0' when others;end beh;综合结果如下:综合结果如下:MooreMoore机设计:例机设计:例3 3 通过对状态图的分析,可以简化:通过对状态图的分析,可以简化:使用使用1 1个寄存器专门存放触发时的输入个寄存器专门存放触发时的输入a a;;则状态可以减少到则状态可以减少到3 3个;结构也简化了。

个;结构也简化了 MooreMoore机设计:例机设计:例4 4 architecture beh of smexamp istype sreg_type is (init,looking,ok);signal sreg:sreg_type;signal lasta:std_logic;begin process(clk) begin if clk'event and clk='1' then lasta<=a;MooreMoore机设计:例机设计:例4 4 case sreg is when init => sreg<=looking; when looking => if a=lasta then sreg<=ok; else sreg<=looking; end if; when ok => if b='1' then sreg<=ok; elsif a=lasta then sreg<=ok; else sreg<=looking; end if; when others =>sreg<=init;end case;MooreMoore机设计:例机设计:例4 4 end if; end process; with sreg select z<= '1' when ok, '0' when others;end beh;MooreMoore机设计:例机设计:例4 4 Mealy状态机状态机与Moore有限状态机不同,Mealy有限状态机的输出不单与当前状态有关,而且与输入信号的当前值有关。

•Mealy有限状态机的输出直接受输入信号的当前值影响,而输入信号可能在一个时钟周期内任意时刻变化,这使得Mealy有限状态机对输入的响应发生在当前时钟周期,比Moore有限状态机对输入信号的响应要早一个周期因此,输入信号的噪声可能影响在输出的信号4.4.米利状态机米利状态机 FSMFSM的设计:的设计:MealyMealy机机 设计要点:设计要点:用输入和状态控制进程;用输入和状态控制进程;用用casecase语句分别选择每一个状态;语句分别选择每一个状态;用用ifif语句确定输入条件,指定相应的下一语句确定输入条件,指定相应的下一状态和输出值;状态和输出值;输出立即赋值(使用一个进程);输出立即赋值(使用一个进程);状态等待时钟条件满足再进行赋值(使用状态等待时钟条件满足再进行赋值(使用另一个进程);另一个进程);4.4.米利状态机米利状态机 例例 MealyMealy状态机设计状态机设计该状态机具有该状态机具有4 4个状态,输入个状态,输入x x,输出,输出z z;状;状态转换图如下所示;态转换图如下所示;MealyMealy机设计:例机设计:例1 1 4.4.米利状态机米利状态机 library ieee;use ieee.std_logic_1164.all;entity mealy is port(x,clk: in std_logic; z: out std_logic);end mealy;architecture beh of mealy is type state is (s0,s1,s2,s3);--设置状态类设置状态类型型 signal current_state,next_state: state; --现态与次态现态与次态begin MealyMealy机设计:例机设计:例1 1 4.4.米利状态机米利状态机 aaa:process(current_state,x) --决定次态和决定次态和输出输出 begin case current_state is when s0=> if x='0' then z<='0';next_state<=s0; else z<='1';next_state<=s2; end if; when s1=> if x='0' then z<='0';next_state<=s0; else z<='0';next_state<=s2; end if;MealyMealy机设计:例机设计:例1 1 when s2=> if x='0' then z<='1';next_state<=s2; else z<='0';next_state<=s3; end if; when s3=> if x='0' then z<='0';next_state<=s3; else z<='1';next_state<=s1; end if; end case; end process aaa; --这个进程与时钟无关这个进程与时钟无关MealyMealy机设计:例机设计:例1 1 4.4.米利状态机米利状态机 sync:process --将现态转移为次态将现态转移为次态 begin wait until clk'event and clk='1'; current_state<=next_state; end process sync; --这个进程受时钟控制这个进程受时钟控制end beh;MealyMealy机设计:例机设计:例1 1 4.4.米利状态机米利状态机 4 状态机实例ADC0809EOCDIN[7..0]ALEOESTARTADDA控制器控制器RDWRRAM_DIN[7..0]ADDRESS[12..0]CS6264OESTARTALECSRDWRRAM_DIN[7..0]ADDRESS[12..0]ADDACLKRSTDIN[7..0]EOC4.4.米利状态机米利状态机 控制器程序library IEEE;library IEEE;use IEEE.STD_LOGIC_1164.all;use IEEE.STD_LOGIC_1164.all;use ieee.std_logic_unsigned.all;use ieee.std_logic_unsigned.all;entity adtosram isentity adtosram is port( port( clk : in STD_LOGIC; clk : in STD_LOGIC; ----工作时钟工作时钟 rst : in STD_LOGIC; rst : in STD_LOGIC; ----复位复位 eoc : in STD_LOGIC; eoc : in STD_LOGIC; ----转换结束标志转换结束标志 din : in STD_LOGIC_VECTOR(7 downto 0); din : in STD_LOGIC_VECTOR(7 downto 0); oe : out STD_LOGIC; oe : out STD_LOGIC; start : out STD_LOGIC; start : out STD_LOGIC; ale : out STD_LOGIC; ale : out STD_LOGIC;----地址锁存允许地址锁存允许 cs : out STD_LOGIC; cs : out STD_LOGIC; rd : out STD_LOGIC; rd : out STD_LOGIC; wr : out STD_LOGIC; wr : out STD_LOGIC; adda : out STD_LOGIC; adda : out STD_LOGIC; --AD--AD地址线最低位地址线最低位 ram_din : out STD_LOGIC_VECTOR(7 downto 0); ram_din : out STD_LOGIC_VECTOR(7 downto 0); address : out STD_LOGIC_VECTOR(12 downto 0) address : out STD_LOGIC_VECTOR(12 downto 0) ); );end adtosram;end adtosram; architecture adtosram of adtosram istype ad_states is (st0,st1,st2,st3,st4,st5,st6,st7);type writ_states is (start_write,write1,write2,write3,write_end);signal ram_current_state,ram_next_state:writ_states;signal adc_current_state,adc_next_state:ad_states;signal adc_end:std_logic; signal lock: std_logic; --转换后数据输出锁存信号转换后数据输出锁存信号signal enable:std_logic; --ad转换允许信号,高电平有效转换允许信号,高电平有效signal addres_plus:std_logic;--sram地址加地址加1时钟信号时钟信号signal adc_data:std_logic_vector(7 downto 0);signal addres_cnt:std_logic_vector(12 downto 0);beginadda<=‘1’; --选择通道选择通道1rd<=‘1’; -- 禁止写禁止写sramadc:process(adc_current_state,eoc,enable) –ad转换组合电路进程转换组合电路进程beginif rst='1' then adc_next_state<=st0;elsecase adc_current_state is when st0=>ale<='0';start<='0'; oe<='0';lock<=‘0’; adc_end<=‘0’; --ad转换初始化转换初始化if enable=‘1’ then adc_next_state<=st1;--允许转换,转下一状态允许转换,转下一状态else adc_next_state<=st0; --禁止转换,仍停留在本状态禁止转换,仍停留在本状态end if;when st1=>ale<='1';start<='0'; oe<='0';lock<='0'; adc_end<='0';adc_next_state<=st2; --通道选择地址锁存,并转下一状态通道选择地址锁存,并转下一状态when st2=>ale<='1';start<='1'; oe<='0';lock<='0'; adc_end<='0';adc_next_state<=st3; --启动启动ad转换信号转换信号startwhen st3=>ale<='1';start<='1'; oe<='0';lock<=‘0’; adc_end<=‘0’; --延迟一个脉冲周期延迟一个脉冲周期if eoc='1' then adc_next_state<=st4; else adc_next_state<=st3; end if;when st4=>ale<='0';start<='0'; oe<='0';lock<='0'; adc_end<='0';if eoc=‘1’ then adc_next_state<=st5;--转化结束,转下一状态转化结束,转下一状态 else adc_next_state<=st4; end if; when st5=>ale<='0';start<='0'; oe<='1';lock<=‘1’; adc_end<=‘1’; --开启数据输出使能信号开启数据输出使能信号oeadc_next_state<=st6;--开启数据锁存信号开启数据锁存信号 when st6=>ale<='0';start<='0'; oe<='1';lock<='1'; adc_end<='1';adc_next_state<=st7;when st7=>ale<='0';start<='0'; oe<='1';lock<='1'; adc_end<='1'; adc_next_state<=st0;when others=>adc_next_state<=st0;end case; end if;end process adc; ad_state:process(clk) ad_state:process(clk) --ad--ad转换时序进程转换时序进程beginbeginif clk'event and clk='1' thenif clk'event and clk='1' thenadc_current_state<=adc_next_state;adc_current_state<=adc_next_state;end if;end if;end process ad_state;end process ad_state;data_lock:process(lock)data_lock:process(lock)beginbeginif lock='1' and lock'event thenif lock='1' and lock'event thenadc_data<=din;adc_data<=din;end if;end if;end process data_lock;end process data_lock;writ_state:process(clk,rst) writ_state:process(clk,rst) --sram--sram数据写入控制数据写入控制 begin begin if rst='1' then ram_current_state<=start_write; if rst='1' then ram_current_state<=start_write; elsif clk'event and clk='1' then elsif clk'event and clk='1' then ram_current_state<=ram_next_state;ram_current_state<=ram_next_state;end if;end if;end process writ_state;end process writ_state; ram_write:process(ram_current_state,adc_end)begincase ram_current_state is when start_write=>cs<='1'; wr<='1';if addres_cnt="1111111111111"then enable<=‘0’; --sram地址计数器满,禁止地址计数器满,禁止ad转换转换ram_next_state<=start_write;else enable<='1';ram_next_state<=write1; end if; when write1=>cs<='1'; wr<='1';enable<='1'; addres_plus<='0';if adc_end=‘1‘ then –判断判断ad转换是否结束转换是否结束ram_next_state<=write2;else enable<='1';ram_next_state<=write1; --ad转换周期未结束,等待转换周期未结束,等待end if;when write2=>cs<=‘1’; wr<=‘1’; --打开打开sram片选信号片选信号enable<=‘0’; addres_plus<=‘0’; --禁止禁止ad转换转换address<=addres_cnt; --输出输出13位地址位地址 ram_din<=adc_data; ram_din<=adc_data; ----转换好数据输向转换好数据输向sramsram数据口数据口ram_next_state<=write3; ram_next_state<=write3; when write3=>cs<=when write3=>cs<=‘‘0 0’’; wr<=; wr<=‘‘0 0’’; ; ----打开写允许信号打开写允许信号enable<=enable<=‘‘0 0’’; addres_plus<=; addres_plus<=‘‘1 1’’; ; ----产生地址加产生地址加1 1时钟上升沿,使地址计时钟上升沿,使地址计数器加数器加1 1ram_next_state<=write_end;ram_next_state<=write_end;----进入结束状态进入结束状态 when write_end=>cs<='1'; wr<='1'; when write_end=>cs<='1'; wr<='1';enable<=enable<=‘‘1 1’’; ; ----打开打开adad转换允许开关转换允许开关addres_plus<=addres_plus<=‘‘0 0’’; ; ----地址加地址加1 1时钟脉冲结束时钟脉冲结束ram_next_state<=start_write;ram_next_state<=start_write;end case;end case;end process ram_write;end process ram_write;counter:process(addres_plus) counter:process(addres_plus) ----地址计数器加地址计数器加1 1进程进程beginbeginif rst='1' then addres_cnt<="0000000000000";if rst='1' then addres_cnt<="0000000000000";elsif addres_plus'event and addres_plus='1' thenelsif addres_plus'event and addres_plus='1' thenaddres_cnt<=addres_cnt+1;addres_cnt<=addres_cnt+1;end if;end if;end process counter;end process counter; end adtosram;end adtosram; ADC0809采集控制模块的设计5.5.状态机实例状态机实例 ADC0809功能简介功能简介ADC0809是是CMOS的的8位位A/D转换器,片内有转换器,片内有8路路模拟开关,可控制模拟开关,可控制8个模拟量中的一个进个模拟量中的一个进入转换器中。

入转换器中ADC0809的分辨率为的分辨率为8位,转换位,转换时间约时间约100 μs,含锁存控制的,含锁存控制的8路多路开关,路多路开关,输出由三态缓冲器控制,单输出由三态缓冲器控制,单5 V电源供电电源供电 ADC0809数据采集电路图 ADC0809的管脚及主要控制信号时序图 ADC0809采集控制状态图 源码实现源码实现LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;ENTITY ADCINT IS PORT ( D : IN STD_LOGIC_VECTOR(7 DOWNTO 0); --0809的的8位转换数据输出位转换数据输出 CLK ,EOC : IN STD_LOGIC; --CLK是转是转换工作时钟换工作时钟 LOCK1, ALE, START, OE, ADDA : OUT STD_LOGIC; Q : OUT STD_LOGIC_VECTOR(7 DOWNTO 0) );END ADCINT;ADC0809采集控制状态图采集控制状态图 源码实现源码实现ARCHITECTURE behav OF ADCINT ISTYPE states IS (st0, st1, st2, st3,st4,st5,st6) ; --定义各定义各状态子类型状态子类型 SIGNAL current_state, next_state: states :=st0 ; SIGNAL REGL : STD_LOGIC_VECTOR(7 DOWNTO 0); SIGNAL LOCK : STD_LOGIC; -- 转换后数据转换后数据输出锁存时钟信号输出锁存时钟信号 BEGIN ADDA <= '1'; LOCK1 <=LOCK; PRO: PROCESS(current_state,EOC) BEGIN --规规定各状态转换方式定各状态转换方式ADC0809采集控制状态图 源码实现源码实现CASE current_state IS  WHEN st0 => ALE<='0';START<='0';OE<='0';LOCK<='0' ;next_state <= st1; WHEN st1 => ALE<='1';START<='0';OE<='0';LOCK<='0' ;next_state <= st2;  WHEN st2 => ALE<='0';START<='1';OE<='0';LOCK<='0' ;next_state <= st3;  WHEN st3 => ALE<='0';START<='0';OE<='0';LOCK<='0';  IF (EOC='1') THEN next_state <= st3; --测试测试EOC的下的下降沿降沿 ELSE next_state <= st4;  END IF ;ADC0809采集控制状态图 源码实现源码实现WHEN st4=> ALE<='0';START<='0';OE<='0';LOCK<='0';  IF (EOC='0') THEN next_state <= st4; --测试测试EOC的上升沿,的上升沿,=1表明转表明转换结束换结束 ELSE next_state <= st5; --继续等待继续等待 END IF ; WHEN st5=> ALE<='0';START<='0';OE<='1';LOCK<='0';next_state <= st6; WHEN st6=> ALE<='0';START<='0';OE<='1';LOCK<='1';next_state <= st0;  WHEN OTHERS => ALE<='0';START<='0';OE<='0';LOCK<='0';next_state <= st0;  END CASE ; END PROCESS PRO ;ADC0809采集控制状态图 源码实现源码实现 PROCESS (CLK)  BEGIN IF ( CLK'EVENT AND CLK='1') THEN  current_state <= next_state; -- 在时钟上升沿,转换至下一在时钟上升沿,转换至下一状态状态 END IF; END PROCESS; -- 由信号由信号current_state将当前状态值带出此进将当前状态值带出此进程,进入进程程,进入进程PRO PROCESS (LOCK) -- 此进程中,在此进程中,在LOCK的上升沿,将转的上升沿,将转换好的数据锁入换好的数据锁入 BEGIN IF LOCK='1' AND LOCK'EVENT THEN REGL <= D ; END IF; END PROCESS ;  Q <= REGL;END behav;ADC0809采集控制状态图 交通灯交通灯控制器控制器St0St1St2St3st0延时 library ieee;use ieee.std_logic_1164.all;entity traffic1 is port(clk,reset:in std_logic; red1,yellow1,green1,red2,yellow2,green2:out std_logic);end traffic1;architecture rtl of traffic1 is type state_t is (st0,st1,st2,st3,st4,st5,st6,st7); signal state,nextstate:state_t;begin process(reset,clk) begin if reset='1' then state<=st0; elsif clk'event and clk='1' then state<=nextstate; end if; end process; process(state)begin case state is when st0=>red1<='0';yellow1<='0';green1<='1'; red2<='1'; yellow2<='0';green2<='1'; nextstate<=st1; when st1=>red1<='0';yellow1<='0';green1<='1'; red2<='1';yellow2<='0';green2<='1'; nextstate<=st2; when st2=>red1<='0';yellow1<='0';green1<='1'; red2<='1';yellow2<='0';green2<='1'; nextstate<=st3; when st3=>red1<='0';yellow1<='1';green1<='0'; red2<='1';yellow2<='0';green2<='0'; nextstate<=st4; when st4=>red1<='1';yellow1<='0';green1<='0'; red2<='0';yellow2<='0';green2<='1'; nextstate<=st5; when st5=>red1<='1';yellow1<='0';green1<='0'; red2<='0';yellow2<='0';green2<='1'; nextstate<=st6;when st6=>red1<='1';yellow1<='0';green1<='0'; red2<='0';yellow2<='0';green2<='1'; nextstate<=st7;when st7=>red1<='1';yellow1<='0';green1<='0'; red2<='0';yellow2<='1';green2<='0'; nextstate<=st0;end case; end process;end rtl; 。

下载提示
相似文档
正为您匹配相似的精品文档