课程设计刚做完,发个秒表的程序分享下。 实验版是Altera公司的ep2c5q208c8,晶振为50Mhz。 功能为复位,启停,用6为数码管显示。精确到1/100s。 程序为: LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; USE IEEE.STD_LOGIC_UNSIGNED.ALL; --------------实体---------------- ENTITY jishuqi IS PORT(start_stop,res,clk:IN STD_LOGIC; sel:OUT STD_LOGIC_VECTOR (2 DOWNTO 0); data:OUT STD_LOGIC_VECTOR (6 DOWNTO 0)); END jishuqi; --------------构造体------------------------------------- ARCHITECTURE rtl OF jishuqi IS SIGNAL clk_1:STD_LOGIC; SIGNAL clk_2:STD_LOGIC; SIGNAL Flag_1:STD_LOGIC:='0'; SIGNAL Flag_2:STD_LOGIC:='0'; BEGIN ---------------分频----------------------------------- PROCESS(clk) VARIABLE count:integer:=0; BEGIN IF(clk'event AND clk='1') THEN count:=count+1; IF(count <= 390) THEN clk_1<='0'; END IF; IF(count > 390 ) THEN clk_1<='1'; END IF; IF(count >= 790 ) THEN count:=0; END IF; END IF; END PROCESS; -------------------------------------------------------- PROCESS(clk) VARIABLE count_1:integer:=0; BEGIN IF(clk'event AND clk='1') THEN count_1 := count_1 + 1; IF(count_1 <= 159999) THEN clk_2 <= '0'; END IF; IF(count_1 > 159999) THEN clk_2 <= '1'; END IF; IF(count_1 >= 319999) THEN count_1 := 0; END IF; END IF; END PROCESS; --------------------计数----------------------------------- PROCESS(clk_1,clk_2,start_stop,res) VARIABLE temp:integer:=0; VARIABLE odata:integer:=0; VARIABLE second_1:integer:=0; VARIABLE second_2:integer:=0; VARIABLE second_3:integer:=0; VARIABLE second_4:integer:=0; VARIABLE second_5:integer:=0; VARIABLE second_6:integer:=0; BEGIN IF(clk_2'EVENT AND clk_2='1' AND Flag_1 = '1') THEN second_1 := second_1 + 1; IF(second_1 = 10) THEN second_1 := 0; second_2 := second_2 + 1; END IF; IF(second_2 = 10) THEN second_2 := 0; second_3 := second_3 + 1; END IF; IF(second_3 = 10) THEN second_3 := 0; second_4 := second_4 + 1; END IF; IF(second_4 = 6) THEN second_4 := 0; second_5 := second_5 + 1; END IF; IF(second_5 = 10) THEN second_5 := 0; second_6 := second_6 + 1; END IF; IF(second_6 = 6) THEN second_6 := 0; END IF; END IF;
if (res='1') then
second_1:=0;
second_2:=0;
second_3:=0;
second_4:=0;
second_5:=0;
second_6:=0;
end if;
-----------------------位选------------------------------------ IF(clk_1'EVENT AND clk_1='1') THEN temp:= temp + 1; IF(temp = 1) THEN sel <= "010"; odata := second_6; ELSIF(temp = 2) THEN sel <= "011"; odata := second_5; ELSIF(temp = 3) THEN sel <= "100"; odata := second_4; ELSIF(temp = 4) THEN sel <= "101"; odata := second_3; ELSIF(temp = 5) THEN sel <= "110"; odata := second_2; ELSIF(temp = 6) THEN sel <= "111"; odata := second_1; temp:= 0; END IF; END IF; ----------------------显示-------------------------------- IF(odata = 0) THEN data<="0111111"; ELSIF(odata = 1) THEN data<="0000110"; ELSIF(odata = 2) THEN data<="1011011"; ELSIF(odata = 3) THEN data<="1001111"; ELSIF(odata = 4) THEN data<="1100110"; ELSIF(odata = 5) THEN data<="1101101"; ELSIF(odata = 6) THEN data<="1111101"; ELSIF(odata = 7) THEN data<="0000111"; ELSIF(odata = 8) THEN data<="1111111"; ELSIF(odata = 9) THEN data<="1101111"; ELSE data<="XXXXXXX"; END IF; END PROCESS; ---------------------起停------------------------------------ PROCESS(start_stop) BEGIN IF(start_stop'EVENT AND start_stop='0') THEN Flag_1 <= NOT Flag_1; END IF; END PROCESS; END rtl;
|