You are on page 1of 49

Agenda

State Machine Types of State Machine Rising Edge detector Sequence detector Vending machine BCD counter State Encoding Lift Controller FSM Stepper Motor Controller FSM

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

State Encoding
State assignment is process of assigning binary values to states in such a way, so as to reduce the cost of combinational circuit that drives the flip-flops It does not affect the function of state machine, hence can be selected by synthesis tool. But the way you encode the states can have a major effect on the amount of hardware you need to implement the machine.

A poor choice of codes results in a state machine that uses too much logic, or is too slow, or both.
Some of the encoding strategies are : Binary Gray One hot

40

State Encoding
If state is defined as type state_type is ( s0,s1,s2,s3);

Following state coding is obtained for different variants

STATE VECTORS STATE S0 S1 S2 S3 Binary 00 01 10 11 Gray 00 01 11 10 One-hot 0001 0010 0100 1000 Almost one-hot 0000 0001 0010 0100

41

42

3-floor lift Lift can be in any floor Si - in floor I Request can come from any floor ri - request from floor I Lift can be asked to move up or down uj,dj - up/down to jth floor

43

44

Stepper Motor Controller FSM


Ang 0 Ang 45 MoveCW Ang 335

MoveCCW

Ang 270

Ang 90

Ang 225

Ang 135

Ang 180
45

Library IEEE; use IEEE.STD_Logic_1164.all; use IEEE.Numeric_STD.all; package CONST_GRAY_STATE_ENCODE_TYPES is constant Ang_0 : unsigned(3 downto 0) := 0010; constant Ang_45 : unsigned(3 downto 0) := 0110; constant Ang_90 : unsigned(3 downto 0) := 0111; constant Ang_135 : unsigned(3 downto 0) := 0101; constant Ang_180 : unsigned(3 downto 0) := 0100; constant Ang_225 : unsigned(3 downto 0) := 1100; constant Ang_270 : unsigned(3 downto 0) := 1101; constant Ang_315 : unsigned(3 downto 0) := 1111; End package CONST_GRAY_STATE_ENCODE_TYPES;

46

Library IEEE; use IEEE.STD_Logic_1164.all; use IEEE.Numeric_STD.all; use work. CONST_GRAY_STATE_ENCODE_TYPES.all;

entity step_motor is port ( clock, reset : in std_logic; PhysicalPosition : in unsigned( 3 downto 0); MoveCW, MoveCCW : in std_logic; NewPosition : out unsigned( 3 downto 0) ); end entity step_motor;

47

architecture rtl of step_motor is signal CurrentState, NextState : unsigned(3 downto 0); begin Comb: process (PhysicalPosition , MoveCW, MoveCCW , CurrentState) begin case CurrentState is when Ang_0 => if ( MoveCW = 1 ) then NextState <= Ang_45; elsif (MoveCCW = 1 ) then NextState <= Ang_315; else NextState <= Ang_0; end if; when Ang_45 => -- complete the code as per FSM when Ang_ 90=> when Ang_135 => when Ang_180 => when Ang_225 => when Ang_270 => when Ang_315 => when others => NextState <= PhysicalPosition ; end case; end process Comb;
48

sequential : process ( clock, reset, PhysicalPosition) begin if ( reset = 0) then CurrentState <= PhysicalPosition; elsif rising_edge(Clock) then CurrentState <= NextState; end if; end process; NewPosition <= CurrentState; End architecture RTL;

49

You might also like