有限状态机教学PPT.ppt_第1页
有限状态机教学PPT.ppt_第2页
有限状态机教学PPT.ppt_第3页
有限状态机教学PPT.ppt_第4页
有限状态机教学PPT.ppt_第5页
已阅读5页,还剩46页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

有限状态机,众所周知,数字系统的基本结构由控制单元和数据处理单元两大部分组成。 控制单元在统一的同步时钟控制下,严格地按照一定的时间关系输出控制信号; 处理单元一步一 步地完成整个数字系统的操作。 其中,控制单元我们常采用有限状态机(fsm)来实现,一、问题引入:停车场计数器,有一停车场,只有一个进出口,如图所示,有两个传感器a和b,可以测出车辆的进出状况,要求设计一个停车场计数器,能够对停车场内的车辆进行计数,使用两个 led数码管显示停车场中的车辆数。,分析: 当有车出来时,首先a被挡住,接着a,b都被挡住,接着只有b被挡住,最后a、b都没被挡住。 当有车进去时,首先b被挡住,接着a,b都被挡住,接着只有a被挡住,最后a、b都没被挡住。,停车场计数器模块图,a,b,rst,clk,数码管,数码管,段码,段码,车进出 判决 模块,加减 计数器,译码器,clk_en,add/sub,车辆进入判决模块设计,a,b,1,1,step 1,车辆进入判决模块设计,a,b,1,0,step 2,车辆进入判决模块设计,a,b,0,0,step 3,车辆进入判决模块设计,a,b,0,1,step 4,车辆进入判决模块设计,a,b,1,1,step 5,车辆进出判决模块设计,设计这个模块的思想是: 引入状态,代表上述的step1step5 状态如何改变由输入决定 模块的输出由状态来决定,step1,step4,step3,step2,step5,a=1 b=0,a=0 b=0,a=0 b=1,a=1 b=1,输出clk 和a/s,1.莫尔状态机模型,状态译码,状态 寄存器,输出译码,clk,输入,现态,次态,输出,二. 状态机模型,library ieee; use ieee.std_logic_1164.all; entity system is port ( clock: in std_logic; input: in std_logic; output: out std_logic); end system; architecture moore of system is type state is(st0,st1,st2, ); signal next_state, current_state: state; begin f1: process (input, current_state) 状态译码 begin next_state = f1(input, current_state); end process;,莫尔状态机vhdl实现,f2: process (clock) 状态寄存器 begin if rising_edge(clock) then current_state = next_state; end if; end process; f3: process (current_state) 输出译码 begin output = f3(current_state); end process; end moore;,莫尔状态机vhdl实现,状态机说明部分,用户自定义数据类型定义语句,type语句用法如下: type 数据类型名 is 数据类型定义 of 基本数据类型 ; 或 type 数据类型名 is 数据类型定义 ;,以下列出了两种不同的定义方式: type st1 is array ( 0 to 15 ) of std_logic ; type week is (sun,mon,tue,wed,thu,fri,sat) ;,状态机说明部分,用户自定义数据类型定义语句,type m_state is ( st0,st1,st2,st3,st4,st5 ) ; signal present_state,next_state : m_state ;,布尔数据类型的定义语句是: type boolean is (false,true) ;,type my_logic is ( 1 ,z ,u ,0 ) ; signal s1 : my_logic ; s1 = z ;,2米利状态机,状态译码,状态 寄存器,输出译码,clk,输入,现态,次态,输出,library ieee; use ieee.std_logic_1164.all; entity system is port ( clock: in std_logic; input: in std_logic; output: out std_logic); end system; architecture mealy of system is type state is(st0,st1,st2, ); signal next_state, current_state: state; begin f1: process (input, current_state) 状态译码 begin next_state = f1(input, current_state); end process;,米利状态机vhdl实现,register: process (clock) 状态寄存器 begin if rising_edge(clock) then current_state = next_state; end if; end process; f2: process (input,current_state) 输出译码 begin output = f2(input, current_state); end process; end mealy;,米利状态机vhdl实现,3onehot状态机,状态编码为独热码:只有一位是1,其他都是0。 constant st0:std_logic_vector(2 downto 0):=”001”; constant st1:std_logic_vector(2 downto 0):=”010”; constant st2:std_logic_vector(2 downto 0):=”100”; signal current_state: std_logic_vector(2 downto 0); signal next_state: std_logic_vector(2 downto 0); 缺点:需要的硬件资源多于二进制编码状态机 优点:1.输出译码简单; 2.速度快; 3.不必考虑最优问题; 4.易于修改。,4.使用vhdl实现有限状态机的一般步骤,1)选择合适的状态机模型: 对于moore状态机来说,输出将在时钟触发信号到来后的 几个门后得到,同时在剩余的时钟周期内不变,即使输入 信号在这段时间内发生变化,输出信号也不会发生变化。 因此,我们可以说将输入和输出部分隔开了,起到了隔离 的作用。 而对于mealy状态机来说,则不然,当输入变化时,输出 在一个时钟周期内也会发生变化。因此,mealy状态机比 moore状态机的“实时性”要好,但是,也会把输入的噪声 引入输出。 有时候,moore状态机比mealy状态机需要更多的状态。 2)根据要求画出状态关系转换图 3)用vhdl语言来描述,有限状态机,假设要设计一个ad控制器,adc0809控制器功能图,有限状态机,步骤0,步骤1,步骤4,步骤3,步骤2,地址锁存 选择通道,启动转换,等待转换结束,转换结束数据输出,初态,步骤5,等待eoc 变低电平,eoc,eoc,三. 状态机的应用举例,1. adc0809控制器,选择莫尔机来实现 ,状态转移图如下,st0,st1,st2,st3,st4,st5,ale=0 start=0 oe=0,ale=1 start=0 oe=0,ale=0 start=1 oe=0,ale=0 start=0 oe=0,eoc0,eoc1,eoc1,ale=0 start=0 oe=0,ale=0 start=0 oe=1,eoc0,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是转换工作时钟 ale, start, oe : out std_logic; adda : out std_logic_vector(2 downto 0); q : out std_logic_vector(7 downto 0) ); end adcint; architecture behav of adcint is type states is (st0, st1, st2, st3,st4,st5) ; -定义各状态子类型 signal current_state, next_state: states :=st0 ; begin adda = “000”; process (clk) -状态寄存器 begin if ( clkevent and clk=1) then current_state = next_state; - 在时钟上升沿,转换至下一状态 end if; end process; - 由信号current_state将当前状态值带出此进程,pro: process(current_state,eoc) 状态译码 begin case current_state is when st0 = next_state next_state next_state if (eoc=1) then next_state if (eoc=0) then next_state next_state next_state = st0; end case ; end process pro ;,vhdl实现,pro: process(current_state) 译码输出 begin case current_state is when st0 = ale ale ale ale ale ale ale=0;start=0;oe=0; end case ; end process pro ;,vhdl实现,pro: process(current_state,eoc) 状态译码译码输出 begin case current_state is when st0 = ale ale ale ale ale ale ale=0;start=0;oe=0; next_state = st0; end case ; end process pro ;,vhdl实现:把状态译码和译码输出写在一个进程里,2. “11”序列检测器,选择莫尔机来实现 ,状态转移图如下,a/0,b/0,c/1,dout=0 ,dout=0,dout=1,din=1,din=1,din=0,din=0,din=1,din=0,library ieee; use ieee.std_logic_1164.all; entity system is port (clk: in std_logic; reset: in std_logic; din: in std_logic; dout: out std_logic); end system; architecture behave of system is type state is(a,b,c); signal next_state, current_state: state; begin process (clk,reset) - state registers begin if reset = 1 then current_state = a; elsif clkevent and clk = 1 then current_state = next_state; end if; end process;,“11”序列的vhdl描述,“11”序列的vhdl描述,f1: process (din, current_state) - next state logic begin case current_state is when a = if din = 1 then next_state if din = 1 then next_state if din = 1 then next_state null; end case; end process;,f2: process (current_state) - output logic begin if current_state = c then dout = 1; else dout = 0; end if; end process; end behave;,“11”序列的vhdl描述,2. “11”序列检测器,选择米利机来实现 ,状态转移图如下,a,b,dout=0 ,dout=1,din=1,din=0,din=0,din=1,dout=0 ,dout=0 ,library ieee; use ieee.std_logic_1164.all; entity system is port (clk: in std_logic; reset: in std_logic; din: in std_logic; dout: out std_logic); end system; architecture behave of system is type state is(a,b); signal next_state, current_state: state; begin register: process (clk,reset) - state registers begin if reset = 1 then current_state = a; elsif clkevent and clk = 1 then current_state = next_state; end if; end process;,vhdl实现,f1: process (din, current_state) - next state logic begin case current_state is when a = if din = 1 then next_state if din = 0 then next_state null; end case; end process;,f2: process (clk,din, current_state) - output logic begin if clkevent and clk = 1 then if din = 1 and current_state = b then dout = 1; else dout = 0; end if; end if; end process; end behave;,one-hot状态机实现,constant a:std_logic_vector(2 downto 0):=”001”; constant b:std_logic_vector(2 downto 0):=”010”; constant c:std_logic_vector(2 downto 0):=”100”; signal current_state: std_logic_vector(2 downto 0); signal next_state: std_logic_vector(2 downto 0);,以上三种状态机的输出都是组合逻辑电路,所以会产生毛刺 现象。 解决毛刺有三种方法: 1)改善电路结构,使得电路中信号延时一致。 2)利用时钟将信号重新读取一遍。 3)在电路中注入一信号将毛刺覆盖,但不影响正常的信号。 这里采用第二种方法,在状态机的输出端加一个寄存器,使 用系统时钟重新读一遍,就可以消除毛刺。,四.输出信号的同步,f2: process (clk,reset,current_state) - output logic begin if reset = 1 then dout = 0 else if clkevent and clk = 1 then if current_state = c then dout = 1; else dout = 0; end if; end if; end if; end process;,vhdl描述,五、状态编码,1 状态位直接输出型编码,控制信号状态编码表,每一位的编码值都赋予了实际的控制功能,即: start = current_state(4);ale = current_state(3) ; oe = current_state(2) ; lock = current_state(1) 。,【例7-7】 library ieee; use ieee.std_logic_1164.all; entity ad0809 is port ( d : in std_logic_vector(11 downto 0); clk ,eoc : in std_logic; ale,start,oe,adda : out std_logic; c_state : out std_logic_vector(4 downto 0); q : out std_logic_vector(7 downto 0) ); end ad574a; architecture behav of ad0809 is signal current_state, next_state: std_logic_vector(4 downto 0 ); constant st0 : std_logic_vector(4 downto 0) := “00000“ ; constant st1 : std_logic_vector(4 downto 0) := “11000“ ; constant st2 : std_logic_vector(4 downto 0) := “00001“ ; constant st3 : std_logic_vector(4 downto 0) := “00100“ ; constant st4 : std_logic_vector(4 downto 0) := “00110“ ; signal regl : std_logic_vector(8 downto 0); signal lock : std_logic; begin adda=1; start =current_state(4);ale=current_state(3) ; oe=current_state(2) ;lock=current_state(1); c_state=current_state;,2 顺序编码,表7-3 编码方式,【例7-8】 . signal crurrent_state,next_state: std_logic_vector(2 downto 0 ); constant st0 : std_logic_vector(2 downto 0) := “000“ ; constant st1 : std_logic_vector(2 downto 0) := “001“ ; constant st2 : std_logic_vector(2 downto 0) := “010“ ; constant st3 : std_logic_vector(2 downto 0) := “011“ ; constant st4 : std_logic_vector(2 downto 0) := “100“ ;,3 状态机剩余状态处理,剩余状态,【例7-9】 . type states is (st0, st1,s

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论