Finite state machine

18
Finite State Machine FSM Ing. Yezid Almanza Pérez Electiva 1 Procesamiento Digital de Señales

description

Desarrollo de un ejercicio para máquinas de estado finito en VHDL

Transcript of Finite state machine

Page 1: Finite state machine

Finite State MachineFSM

Ing. Yezid Almanza PérezElectiva 1

Procesamiento Digital de Señales

Page 2: Finite state machine

• El ejercicio desarrollado a continuación, esta propuesto en el libro FPGA PROTOTYPING BY EXAMPLES. Pong P. Chu. Corresponde al problema 5.5.3, pag. 125.

Page 3: Finite state machine

CONTADOR DE OCUPACIÓN DE ESTACIONAMIENTO

• Considere un estacionamiento con una sola puerta de entrada y salida. Se utilizan dos pares de fotosensores para monitorear la actividad de los carros como se muestra en la figura. Cuando un objeto esta entre el foto transmisor y el foto receptor, la luz es bloqueada y la salida correspondiente es asegurada a ‘1’. Para monitorear los eventos de 2 sensores, podemos determinar si un carro esta entrando o saliendo o un peatón esta pasando. Por ejemplo, la secuencia siguiente indica que un carro entra al estacionamiento.– Inicialmente, ambos sensores están desbloqueados (i.e. las señales a y b son “00”).– El sensor a es bloqueado (i. e., las señales a y b son “10”).– Ambos sensores están bloqueados (i. e., las señales a y b son “11”).– El sensor a es desbloqueado (i. e., las señales a y b son “01”).– Ambos sensores llegan a estar desbloqueados (i. e., las señales a y b son “00”).Diseñe un contador de ocupación de estacionamiento como sigue:1. Diseñe un FSM con dos señales de entrada, a y b, y dos señales de salida, enter y exit. Las señales

enter y exit se aseguran en un ciclo de reloj cuando un carro entra y un ciclo de reloj cuando un carro sale del estacionamiento, respenctivamente.

2. Derive el código HDL para el FSM3. Diseñe un contador con dos señales de control, inc y dec, las cuales incrementa y decrementa el

contador cuando se aseguran. Derive el código HDL4. Combine el contador y el FSM y el circuito de multiplexación de LED. Use dos pushbottons con

atirebote para la operación mímica de las dos entradas de sensores. Verifique la operación del contador de ocupación.

Page 4: Finite state machine

Diagrama de estados

A

B D

C

10

11 01

00

00

10 11

01

00

10 01

11

01 10

00

11

Page 5: Finite state machine

Sensor_A

Sensor_B

FSM deentrada-salida y

contador

SA

SB

Salida delcontador de 8 bits

7

0

Convertidor de binario a

BCD

Salida BCD

Multiplexación BCD a 7

segmentos

abcdefg

ánodo 0ánodo 1ánodo 2ánodo 3

4

4

Paquete. Binario a 7 segmentos

FSMDB

FSMDB

Instancia 1

Instancia 2

DB: Circuito antirebote

Bloque principal del estacionamientoSeñales adicionalesCLOCK y RESETPaquete. Contador de autos

4

U. Bcd

D. Bcd

C. Bcd

Page 6: Finite state machine

Entidad DPLDRV (Multiplexadora de Display)Código aportado por Adrian Costa Ospino

ENTITY DPLDRV IS PORT ( CLK50M : IN STD_LOGIC;

DIGITO0 : IN STD_LOGIC_VECTOR (3 DOWNTO 0); --Digito del display de las unidades DIGITO1 : IN STD_LOGIC_VECTOR (3 DOWNTO 0); --Digito del display de las decenas DIGITO2 : IN STD_LOGIC_VECTOR (3 DOWNTO 0); --Digito del display de las centenas

-- DIGITO3 : IN INTEGER RANGE 0 TO 8; --Digito del display de las millar DPL_ENABLE : INOUT STD_LOGIC_VECTOR (3 DOWNTO 0); DPL_SEG : INOUT STD_LOGIC_VECTOR (0 TO 7)

);END DPLDRV;

Page 7: Finite state machine

Arquitectura DPLDRVCódigo aportado por Adrian Costa Ospino

ARCHITECTURE BEHAVIORAL OF DPLDRV IS

SIGNAL DIGITO : INTEGER RANGE 0 TO 15:=0;

SIGNAL POS_DISPLAY : INTEGER RANGE 0 TO 3:=0;

SIGNAL DELAY : STD_LOGIC_VECTOR (13 DOWNTO 0);

BEGIN

DPL_ENABLE <= "1110" WHEN (POS_DISPLAY = 0) ELSE

"1101" WHEN (POS_DISPLAY = 1) ELSE

"1011" WHEN (POS_DISPLAY = 2) ELSE

"0111";

DIGITO <= DIGITO0 WHEN (POS_DISPLAY = 0) ELSE

DIGITO1 WHEN (POS_DISPLAY = 1) ELSE

DIGITO2 WHEN (POS_DISPLAY = 2) ELSE

DIGITO3;

DPL_SEG <= "0000001" WHEN (DIGITO=0) ELSE

"1001111" WHEN (DIGITO=1) ELSE

"0010010" WHEN (DIGITO=2) ELSE

"0000110" WHEN (DIGITO=3) ELSE

"1001100" WHEN (DIGITO=4) ELSE

"0100100" WHEN (DIGITO=5) ELSE

"0100000" WHEN (DIGITO=6) ELSE

"0001111" WHEN (DIGITO=7) ELSE

"0000000" WHEN (DIGITO=8) ELSE

"0000100" WHEN (DIGITO=9) ELSE

"1111111";

PROCESS (CLK50M)

BEGIN

IF (CLK50M'EVENT AND CLK50M='1') THEN

DELAY <= DELAY+1;

IF ( DELAY="11111111111111" ) THEN

POS_DISPLAY <= POS_DISPLAY+1;

END IF;

END IF;

END PROCESS;

END BEHAVIORAL;

Page 8: Finite state machine

Entidad DB_FSM (Máquina de estados finitos para el antirebote)

Tomado del libro FPGA PROTOTYPING BY EXAMPLES. Pong P. Chu. Pag. 120, 121

• entity DB_FSM is Port ( clk : in STD_LOGIC;sw : in STD_LOGIC;db : inout STD_LOGIC);

end DB_FSM;

Page 9: Finite state machine

Arquitectura DB_FSM (Máquina de estados finitos para el antirebote)

Tomado del libro FPGA PROTOTYPING BY EXAMPLES. Pong P. Chu. Pag. 120, 121 architecture Behavioral of DB_FSM isconstant N: integer:= 19; -- 2^N * 20 ns = 10.24 ms;signal q_reg, q_next: unsigned(N-1 downto 0);signal m_db: std_logic;type eg_state_type is (zero, wait1_1, wait1_2, wait1_3, one, wait0_1, wait0_2, wait0_3);signal state_reg, state_next: eg_state_type := zero;Begin process (clk) begin if (clk'event and clk = '1') then q_reg <= q_next; end if; end process; -- next state logic q_next <= q_reg + 1; -- output db m_db <= '1' when q_reg = 0 else '0'; -- state register process (clk) begin if (clk'event and clk = '1') then state_reg <= state_next; end if; end process;

Page 10: Finite state machine

process (state_reg, sw, m_db) begin state_next <= state_reg; db <= '0'; case state_reg is

when zero => if sw = '1' then state_next <= wait1_1; end if;

when wait1_1 => if sw = '0' then state_next <= zero; else if m_db = '1' then state_next <= wait1_2; end if; end if; when wait1_2 => if sw = '0' then state_next <= zero; else if m_db = '1' then state_next <= wait1_3; end if; end if; when wait1_3 => if sw = '0' then state_next <= zero; else if m_db = '1' then state_next <= one; end if; end if; when one => db <= '1'; if sw = '0' then state_next <= wait1_1; end if; when wait0_1 => db <= '1'; if sw = '1' then state_next <= one; else if m_db = '1' then state_next <= wait0_2; end if; end if; when wait0_2 => db <= '1'; if sw = '1' then state_next <= one; else if m_db = '1' then state_next <= wait0_3; end if; end if; when wait0_3 => db <= '1'; if sw = '1' then state_next <= one; else if m_db = '1' then state_next <= zero; end if; end if;

end case; end process;end Behavioral;

Page 11: Finite state machine

Entidad CuentaAutos_vhd (FSM que reconoce la entrada o salida de autos)

Código desarrollado por Ing. Yezid Almanza Pérez

entity CuentaAutos_vhd is Port ( rst : in STD_LOGIC; clk : in STD_LOGIC; sens_a : in STD_LOGIC; sens_b : in STD_LOGIC; cuenta : inout STD_LOGIC_VECTOR (7 DOWNTO 0) );

end CuentaAutos_vhd;

Page 12: Finite state machine

Arquitectura CuentaAutos_vhd (FSM que reconoce la entrada o salida de autos)Código desarrollado por Ing. Yezid Almanza Pérez

architecture Behavioral of CuentaAutos_vhd is

 

type estados is (edo_a, edo_b, edo_c, edo_d);

signal state_act, state_sig, state_prev, state_aux: estados := edo_a;

signal cont, contact: STD_LOGIC_VECTOR(7 downto 0):=(others=> '0');

signal sensor_A, sensor_B: STD_LOGIC;

 

begin

 

Antirebote_1: entity work.DB_FSM(behavioral) port map (sw => Sens_A, clk => clk, db=>sensor_A );

 

Antirebote_2: entity work.DB_FSM(behavioral) port map (sw => Sens_B, clk => clk, db=>sensor_B );

 

-- state register

process (clk, RST)

begin

if(RST='1') then

state_act <= edo_a;

state_aux <= edo_a;

contact <= (others => '0');

elsif (clk'event and clk='1') then

state_act <= state_sig;

state_aux <=state_prev;

contact<=cont ;

end if;

end process;

 

Page 13: Finite state machine

process (state_act, sensor_a, sensor_b, state_aux, contact)

begin

 case state_act is

when edo_a => if (state_aux = edo_d) then cont <= contact + 1;

elsif (state_aux = edo_b) then cont <= contact - 1;

else cont <= contact;

end if;

if (sensor_a = 'H' and sensor_b = 'L') then state_sig <= edo_b;

elsif (sensor_a = 'L' and sensor_b = 'H') then state_sig <= edo_d;

else state_sig <= edo_a;

end if;

state_prev <= edo_a;

when edo_b => if (state_aux = edo_a and sensor_a = 'H' and sensor_b = 'L') then

state_sig <= edo_b;

state_prev <= edo_a;

elsif (state_aux = edo_c and sensor_a = 'H' and sensor_b = 'L') then

state_sig <= edo_b;

state_prev <= edo_c;

elsif (state_aux = edo_a and sensor_a = 'H' and sensor_b = 'H') then

state_sig <= edo_c;

state_prev <= edo_b;

elsif (state_aux = edo_c and sensor_a = 'L' and sensor_b = 'L') then

state_sig <= edo_a;

state_prev <= edo_b;

else

state_sig <= edo_a;

state_prev <= edo_a;

end if;

cont <= contact;

Arquitectura CuentaAutos_vhd (FSM que reconoce la entrada o salida de autos)Código desarrollado por Ing. Yezid Almanza Pérez

Page 14: Finite state machine

when edo_c => if (state_aux = edo_b and sensor_a = 'H' and sensor_b = 'H') then

state_sig <= edo_c;

state_prev <= edo_b;

elsif (state_aux = edo_d and sensor_a = 'H' and sensor_b = 'H') then

state_sig <= edo_c;

state_prev <= edo_d;

elsif (state_aux = edo_b and sensor_a = 'L' and sensor_b = 'H') then

state_sig <= edo_d;

state_prev <= edo_c;

elsif (state_aux = edo_d and sensor_a = 'H' and sensor_b = 'L') then

state_sig <= edo_b;

state_prev <= edo_c;

else

state_sig <= edo_a;

state_prev <= edo_a;

end if;

cont <= contact;

when edo_d => if (state_aux = edo_c and sensor_a = 'L' and sensor_b = 'H') then

state_sig <= edo_d;

state_prev <= edo_c;

elsif (state_aux = edo_a and sensor_a = 'L' and sensor_b = 'H') then

state_sig <= edo_d;

state_prev <= edo_a;

elsif (state_aux = edo_c and sensor_a = 'L' and sensor_b = 'L') then

state_sig <= edo_a;

state_prev <= edo_d;

elsif (state_aux = edo_a and sensor_a = 'H' and sensor_b = 'H') then

state_sig <= edo_c;

state_prev <= edo_d;

else

state_sig <= edo_a;

state_prev <= edo_a;

end if;

cont <= contact;

end case;

end process;

cuenta <= cont;

end Behavioral;

Arquitectura CuentaAutos_vhd (FSM que reconoce la entrada o salida de autos)Código desarrollado por Ing. Yezid Almanza Pérez

Page 15: Finite state machine

Entidad BYTE2BCD (Convierte un byte a código BCD) Código aportado por Adrian Costa Ospino

ENTITY BYTE2BCD IS PORT ( BYTE : INOUT STD_LOGIC_VECTOR (7 DOWNTO 0); DIGITO0 : INOUT STD_LOGIC_VECTOR (3 DOWNTO 0); DIGITO1 : INOUT STD_LOGIC_VECTOR (3 DOWNTO 0); DIGITO2 : INOUT STD_LOGIC_VECTOR (3 DOWNTO 0));END BYTE2BCD;

Page 16: Finite state machine

Arquitectura BYTE2BCD Código aportado por Adrian Costa Ospino

ARCHITECTURE BEHAVIORAL OF BYTE2BCD IS SIGNAL DU0 : STD_LOGIC_VECTOR( 3 DOWNTO 0 ); SIGNAL DU1 : STD_LOGIC_VECTOR( 3 DOWNTO 0 ); SIGNAL DU2 : STD_LOGIC_VECTOR( 3 DOWNTO 0 ); SIGNAL DU3 : STD_LOGIC_VECTOR( 3 DOWNTO 0 ); SIGNAL DU4 : STD_LOGIC_VECTOR( 3 DOWNTO 0 ); SIGNAL DU5 : STD_LOGIC_VECTOR( 3 DOWNTO 0 ); SIGNAL DD0 : STD_LOGIC_VECTOR( 3 DOWNTO 0 ); SIGNAL DD1 : STD_LOGIC_VECTOR( 3 DOWNTO 0 ); SIGNAL DD2 : STD_LOGIC_VECTOR( 3 DOWNTO 0 ); SIGNAL DC0 : STD_LOGIC_VECTOR( 3 DOWNTO 0 ); BEGIN --UNIDADES. DU0 <= '0'&BYTE(7 DOWNTO 5) WHEN (BYTE(7 DOWNTO 5)<5) ELSE '0'&BYTE(7 DOWNTO 5)+3; DU1 <= DU0(2 DOWNTO

0)&BYTE(4) WHEN ( (DU0(2 DOWNTO 0)&BYTE(4))<5 ) ELSE DU0(2 DOWNTO 0)&BYTE(4)+3; DU2 <= DU1(2 DOWNTO 0)&BYTE(3) WHEN ( (DU1(2 DOWNTO 0)&BYTE(3))<5 ) ELSE DU1(2 DOWNTO 0)&BYTE(3)+3; DU3 <= DU2(2 DOWNTO 0)&BYTE(2) WHEN ( (DU2(2 DOWNTO 0)&BYTE(2))<5 ) ELSE DU2(2 DOWNTO 0)&BYTE(2)+3; DU4 <= DU3(2 DOWNTO 0)&BYTE(1) WHEN ( (DU3(2 DOWNTO 0)&BYTE(1))<5 ) ELSE DU3(2 DOWNTO 0)&BYTE(1)+3; DU5 <= DU4(2 DOWNTO 0)&BYTE(0);

--DECENAS. DD0 <= '0'&DU0(3)&DU1(3)&DU2(3) WHEN ( (DU0(3)&DU1(3)&DU2(3))<5 ) ELSE '0'&DU0(3)&DU1(3)&DU2(3)+3; DD1 <=

DD0(2)&DD0(1)&DD0(0)&DU3(3) WHEN ( (DD0(2)&DD0(1)&DD0(0)&DU3(3))<5 ) ELSE DD0(2)&DD0(1)&DD0(0)&DU3(3)+3; DD2 <= DD1(2)&DD1(1)&DD1(0)&DU4(3);

--CENTENAS. DC0 <= '0'&'0'&DD0(3)&DD1(3); DIGITO0 <= DU5; DIGITO1 <= DD2; DIGITO2 <= DC0; -- DIGITO0 <= CONV_INTEGER(DU5); -- DIGITO1 <= CONV_INTEGER(DD2); -- DIGITO2 <= CONV_INTEGER(DC0); -- DIGITO3

<= CONV_INTEGER(PRBDATA); END BEHAVIORAL;

Page 17: Finite state machine

Entidad Estacionamiento (Integración completa)Código desarrollado por Ing. Yezid Almanza Pérez

• entity Estacionamiento is Port ( rst: in STD_LOGIC;

SA : in STD_LOGIC; SB : in STD_LOGIC;

clk: in STD_LOGIC; Led: out STD_LOGIC_VECTOR (7 downto 0);

anodos: out STD_LOGIC_VECTOR (3 downto 0); segmentos: out STD_LOGIC_VECTOR (0 to 7) );

end Estacionamiento;

Page 18: Finite state machine

Arquitectura Estacionamiento (Integracion completa)por Ing. Yezid Almanza Pérez

architecture Behavioral of Estacionamiento is

Signal C : STD_LOGIC_VECTOR (7 downto 0);Signal Seg : STD_LOGIC_VECTOR (7 downto 0); Signal AComun : STD_LOGIC_VECTOR (3 downto 0);

Begin

FSM: CuentaAutos_vhd port map (rst => rst, sens_a => SA, sens_b => SB, clk=> clk, cuenta => C );

Deco7seg: Binario2Display port map (Byte => C, CLK => clk, Enable =>AComun, Segmentos => Seg);

segmentos <= Seg;anodos <= AComun;Led <= C;

end Behavioral;