library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;entity GJL isport(clk, reset: in std_logic;btn_green, btn_red: in std_logic; -- 新增控制按键r1, r2, y1, y2, g1, g2: out std_logic;ledag: out std_logic_vector(6 downto 0);led_sel: buffer std_logic_vector(2 downto 0));
end GJL;architecture str1 of GJL issignal sec10, sec1: integer range 0 to 9;signal ledag_temp: std_logic_vector(6 downto 0);signal dir_flag: std_logic;signal led_temp: integer range 0 to 15;signal direction: integer range 0 to 15;signal counter: integer range 0 to 511;signal clkcount, clk1Hz: std_logic;signal led_seg_s: std_logic_vector(2 downto 0);
beginp1: process(clk)beginif rising_edge(clk) thenif counter = 499 thencounter <= 0;clkcount <= not clkcount;elsecounter <= counter + 1;end if;clk1Hz <= clkcount;end if;end process;p2: process(clk1Hz, reset)beginif reset = '0' thensec10 <= 2;sec1 <= 0;dir_flag <= '0';elsif rising_edge(clk1Hz) thenif sec10 = 0 and sec1 = 0 thensec10 <= 1;sec1 <= 9;dir_flag <= not dir_flag;elsif sec10 > 0 and sec1 = 0 thensec10 <= sec10 - 1;sec1 <= 9;elsesec1 <= sec1 - 1;end if;end if;end process;p3: process(sec10, sec1, reset, dir_flag)beginif reset = '0' thenr1 <= '1'; r2 <= '1'; g1 <= '0'; g2 <= '0';elseif dir_flag = '0' thenr1 <= '0';r2 <= '1';g1 <= '1';g2 <= '0';elser1 <= '1';r2 <= '0';g1 <= '0';g2 <= '1';end if;end if;end process;p5: process(dir_flag)beginif dir_flag = '0' thendirection <= 11;elsedirection <= 10;end if;end process;p6: process(led_sel)begincase led_sel iswhen "000"=> led_temp <= direction;when "001"=> led_temp <= direction;when "010"=> led_temp <= sec10;when "011"=> led_temp <= sec1;when "100"=> led_temp <= direction;when "101"=> led_temp <= direction;when "110"=> led_temp <= sec10;when "111"=> led_temp <= sec1;when others => null;end case;end process;p7: process(clk)beginif rising_edge(clk) thenif led_seg_s = "111" thenled_seg_s <= "000";elseled_seg_s <= led_seg_s + 1;end if;ledag <= ledag_temp;led_sel <= led_seg_s;end if;end process;p8: process(led_temp)begincase led_temp iswhen 0 => ledag_temp <= "0111111";when 1 => ledag_temp <= "0000110";when 2 => ledag_temp <= "1011011";when 3 => ledag_temp <= "1001111";when 4 => ledag_temp <= "1100110";when 5 => ledag_temp <= "1101101";when 6 => ledag_temp <= "1111101";when 7 => ledag_temp <= "0000111";when 8 => ledag_temp <= "1111111";when 9 => ledag_temp <= "1101111";when 10 => ledag_temp <= "1001000";when 11 => ledag_temp <= "0010100";when others => ledag_temp <= "0000000";end case;end process;-- 新增过程,控制绿灯和红灯的开关p9: process(clk, reset, btn_green, btn_red)beginif reset = '0' thenr1 <= '1'; r2 <= '1'; g1 <= '0'; g2 <= '0';elsif rising_edge(clk) thenif btn_green = '1' theng1 <= '0'; g2 <= '0'; -- 所有绿灯灭elsif btn_red = '1' thenr1 <= '0'; r2 <= '0'; -- 所有红灯灭else-- 保持原有逻辑不变,或者根据需要调整end if;end if;end process;
end str1;