📄 countdown.vhd
字号:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity countdown is
port(clkout:in std_logic; --软件分频得到时钟信号频率为10Hz
start:in std_logic; --启动信号
sel:in std_logic; --改变位选信号
add:in std_logic; --设置时间加一信号
reset:in std_logic; --复位信号
bell:out std_logic; --计时时间到输出闹钟信号
bcd2:out std_logic_vector(11 downto 0));--计时输出信号
end entity countdown;
architecture behav of countdown is
begin
--pro1进程,实现倒计时功能,还有设定初值
pro1:process(reset,start,clkout)
variable bell_reg:std_logic; --记录倒计时时间到后响铃输出变量
variable time_up:std_logic; --标志计时时间到变量
variable sel_reg:std_logic_vector(1 downto 0); --设置时间初值的位选信号
variable cnt1:std_logic_vector(3 downto 0); --计时0.1s位
variable cnt2:std_logic_vector(3 downto 0); --计时个位
variable cnt3:std_logic_vector(3 downto 0); --计时十位
begin
if reset = '1' then --复位后初值位30S
cnt1 := "0000";
cnt2 := "0000";
cnt3 := "0011";
bell_reg:='0';
elsif rising_edge(clkout) then
if (start='1' and time_up='0')then --启动倒计时
if cnt1/="0000" then
cnt1:=cnt1-"0001";
else
cnt1:="1001";
if cnt2 /= "0000" then
cnt2:=cnt2-"0001";
else
cnt2:="1001";
if cnt3 /="0000" then
cnt3:=cnt3 - "0001";
end if;
end if;
end if;
--倒计时到停止计时并输出闹铃信号
if (cnt1="0000" and cnt2 = "0000" and cnt3 = "0000") then
bell_reg:='1';
time_up:='1';
else
bell_reg:='0';
time_up:='0';
end if;
elsif start='0' then --设置时间初值
if sel ='1' then --改变位选变量值
if sel_reg="11" then
sel_reg := "00";
else
sel_reg := sel_reg + "01";
end if;
end if;
case sel_reg is --sel_reg为“01”时,改变0.1S上的计数值
when "01" =>
if add= '1' then
if cnt1 = "1001" then
cnt1:= "0000";
else
cnt1:=cnt1+"0001";
end if;
end if;
when "10" => --sel_reg为”10“时,改变个位上的数值
if add= '1' then
if cnt2 = "1001" then
cnt2:= "0000";
else
cnt2:= cnt2+"0001";
end if;
end if;
when "11" => --sel_reg为”11“时,改变十位上的数值
if add= '1' then
if cnt3 = "1001" then
cnt3:= "0000";
else
cnt3:= cnt3+"0001";
end if;
end if;
when others => null;
end case;
end if;
end if;
bcd2(3 downto 0) <= cnt1; -- 输出显示时间
bcd2(7 downto 4) <= cnt2;
bcd2(11 downto 8) <= cnt3;
bell<=bell_reg; --闹铃输出提示时间到
end process pro1;
end behav;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -