2018-12-04
2018-12-04 本文已影响0人
宁静致远D
数字钟代码
library IEEE;
use IEEE.STD_LOGIC_1164.all;
use IEEE.STD_LOGIC_unsigned.all;
use IEEE.STD_LOGIC_arith.all;
entity jishiqi_en is
port (clk,clr:in std_logic;
en,data:out std_logic_vector(7 downto 0);
ky1,ky2 :in STD_LOGIC
);
end jishiqi_en;
architecture jishiqi_ar of jishiqi_en is
signal count:std_logic_vector(24 downto 0);--分频计数
signal div_1,div_2,div_3:std_logic;
signal s,a1:std_logic_vector(2 downto 0);
signal dl :std_logic_vector(3 downto 0);
signal vs:std_logic_vector(7 downto 0);
signal cnt_s_g:std_logic_vector(3 downto 0);-- 秒的个位
signal cnt_s_s:std_logic_vector(3 downto 0);--秒的十位
signal cnt_m_s:std_logic_vector(3 downto 0);
signal cnt_m_g:std_logic_vector(3 downto 0);
signal cnt_h_s:std_logic_vector(3 downto 0);
signal cnt_h_g:std_logic_vector(3 downto 0);
signal c1:std_logic;
signal c2:std_logic;
signal t,cout:std_logic;
signal o1 : STD_LOGIC;
signal o2 : STD_LOGIC;
begin
p1: process(clk,clr) --分频器(1hz,512hz>50*8hz,动态扫描每个数码管大于50hz)
begin
if (clr='0') then
count<="0000000000000000000000000";
elsif(clk'event and clk='1') then
if (count="1111111111111111111111111")then
count<="0000000000000000000000000";
else
count<=count+1;
end if;
end if;
end process;
div_2<=count(14);
div_1<=count(24);
p2: process(div_2,clr)
begin
if(clr='0')then
s<="000";
elsif(div_2'event and div_2='1')then
if(s="111")then
s<="000" ;
else
s<=s+1;
end if;
end if;
end process;
a1<=s;
---------------------------------
process(clk,ky1,ky2)
variable a1:integer;
variable a2:integer;
begin
if(rising_edge(clk))then
if(ky1='1')then
a1:=0;
end if;
if(ky2='1')then
a2:=0;
end if;
if(a1>1999999)then
o1<='0';
else
o1<='1';
end if;
if(a2>1999999)then
o2<='0';
else
o2<='1';
end if;
a1:=a1 + 1;
a2:=a2 + 1;
end if;
end process;
process(o1,o2,div_1)
begin
if(falling_edge(o1))then
t<= not t;
end if;
if(t='1')then
cout<= not o2;
else
cout<=div_1;
end if;
end process;
---------------------------------------
p3:process(a1) -- 分别选中第一位数码管~~~第八位数码管
begin
case a1 is
when "000"=> vs<="00000001" ;
when "001"=> vs<="00000010" ;
when "010"=> vs<="00000100" ;
when "011"=> vs<="00001000" ;
when "100"=> vs<="00010000" ;
when "101"=> vs<="00100000" ;
when "110"=> vs<="01000000" ;
when others=> vs<="10000000";
end case;
en<=vs;
end process;
p4:process(cout,clr)--计数模块,秒,分的值满9进位并且置0,小时计到24
begin
if(clr='0')then
cnt_s_g<="0000";
cnt_s_s<="0000";
c1<='0';
else
if(rising_edge(cout))then
if(cnt_s_s="0101" and cnt_s_g="1001")then
cnt_s_s<="0000";
cnt_s_g<="0000";
c1<='1';
elsif (cnt_s_g<"1001") then
cnt_s_g<=cnt_s_g+'1';
c1<='0';
else
cnt_s_g<="0000";
if(cnt_s_s<"0101")then
cnt_s_s<=cnt_s_s+'1';
c1<='0';
else
cnt_s_s<="0000";
c1<='0';
end if;
end if;
end if;
end if;
end process;
process(clr,c1)
begin
if(clr='0')then
cnt_m_g<="0000";
cnt_m_s<="0000";
c2<='0';
else
if(rising_edge(c1))then
if(cnt_m_s="0101" and cnt_m_g="1001")then
cnt_m_s<="0000";
cnt_m_g<="0000";
c2<='1';
elsif(cnt_m_g<"1001")then
cnt_m_g<=cnt_m_g+'1';
c2<='0';
else
cnt_m_g<="0000";
if(cnt_m_s<"0101")then
cnt_m_s<=cnt_m_s+'1';
c2<='0';
else
cnt_m_s<="0000";
c2<='0';
end if;
end if;
end if;
end if;
end process;
process(clr,c2)
begin
if(clr='0')then
cnt_h_g<="0000";
cnt_h_s<="0000";
else
if(rising_edge(c2))then
if(cnt_h_s="0010" and cnt_h_g="0100")then
cnt_h_s<="0000";
cnt_h_g<="0000";
elsif(cnt_h_g<"1001")then
cnt_h_g<=cnt_h_g+'1';
else
cnt_h_g<="0000";
if(cnt_h_s<"0010")then
cnt_h_s<=cnt_h_s+'1';
else
cnt_h_s<="0000";
end if;
end if;
end if;
end if;
end process;
p5:process(vs)
begin
case vs is
when "00000001" => dl<=cnt_s_g;--把秒的个位数的计数的值赋给第一个数码管
when "00000010" => dl<=cnt_s_s;
when "00000100" => dl<="1111"; --小数点赋给第三个数码管
when "00001000" => dl<=cnt_m_g;
when "00010000" => dl<=cnt_m_s;
when "00100000" => dl<="1111";
when "01000000" => dl<=cnt_h_g;
when others => dl<=cnt_h_s;
end case;
end process;
p6:process(dl)
begin
case dl is
when"0000"=> data <="00111111"; --若dl为二进制的0,输出相应数码管显示的值0
when"0001"=> data <="00000110";
when"0010"=> data <="01011011";
when"0011"=> data <="01001111";
when"0100"=> data <="01100110";
when"0101"=> data <="01101101";
when"0110"=> data <="01111101";
when"0111"=> data <="00000111";
when"1000"=> data <="01111111";
when"1001"=> data <="01101111";
when others=> data<="10000000";
end case;
end process;
end jishiqi_ar;
”'