1. Finite State Machine
FSM
Ing. Yezid Almanza Pérez
Electiva 1
Procesamiento Digital de Señales
2. • 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.
3. 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 FSM
3. 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 HDL
4. 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.
5. Bloque principal del estacionamiento
Señales adicionales
Paquete. Contador de autos Paquete. Binario a 7 segmentos CLOCK y RESET
Salida del
Salida BCD
contador
de 8 bits
SA 7 Convertidor
FSM de
de binario a
entrada-salida y
BCD
SB contador 0
Instancia 1
Sensor_A
FSM
a
DB b
4 c
d
Sensor_B Multiplexación e
FSM f
4 BCD a 7
DB segmentos
g
ánodo 0
Instancia 2 ánodo 1
4 ánodo 2
DB: Circuito antirebote ánodo 3
6. 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;
7. Arquitectura DPLDRV
Có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;
8. 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;
9. 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 is
constant 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;
10. 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;
11. 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;
12. 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;
13. Arquitectura CuentaAutos_vhd (FSM que reconoce la entrada o salida de autos)
Código desarrollado por Ing. Yezid Almanza Pérez
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;
14. Arquitectura CuentaAutos_vhd (FSM que reconoce la entrada o salida de autos)
Código desarrollado por Ing. Yezid Almanza Pérez
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;
15. 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;
16. 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;
17. 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;
18. 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;