⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1.txt

📁 实现时钟功能
💻 TXT
字号:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
---------------------------------------------------------------------------
entity test is
       port(clk:in std_logic;                     --工作时钟 
              p:out std_logic_vector(3 downto 0); --四个动态数码管
           -- key:in std_logic_vector(3 downto 1);  --4*4键盘s1,s2,s3三个按键
              key_count,key_stop,key_clear,key_add:in std_logic;
              y:out std_logic_vector(6 downto 0));
end test;
--------------------------------------------------------------------------
architecture bhv of test is
signal a,b,c,d:integer range 0 to 9;     --秒个位,秒十位,分个位,分十位
signal count_en,stop_en,clear_en:bit;    --计数使能对应按键s2,停止使能对应按键s1,清零使能对应按键s3
signal clk_done:std_logic_vector(2 downto 0);
signal key_add_done1,key_add_done2,key_add_done3:std_logic;
signal key_add_out:std_logic;
type state is (st0,st1);
signal s:state;
begin
---------------------------------------------------------------------------
a1:process(clk,count_en)
   variable t:integer range 0 to 4000;  --1秒计数一次
   begin    
  if clk'event and clk='1' then
       if count_en='1' then
           if t<4000 then
               t:=t+1;
             else t:=0; 
               if a<9 then a<=a+1;
                  else     a<=0;
                   if b<5 then b<=b+1;
                      else     b<=0;                     
                        if c<9 then   c<=c+1;                          
                           else       c<=0;                           
                             if d<5 then  d<=d+1;                               
                                   else   d<=0;                                 
                                  if a=9 and b=5 and c=9 and d=5 then
                                      a<=0;b<=0;c<=0;d<=0;
                                  end if;
                              end if;
                         end if;
                    end if;
                 end if;
           end if;
       elsif stop_en='1' then
            case s is
               when st0 => if key_add_out='0' then s<=st1;
                           end if;
               when st1 => if key_add_out='1' then 
            --if key_add_out='0' then 
                              s<=st0;
                              if a<9 then a<=a+1;
                              else a<=0;
                                   if b<5 then b<=b+1;
                                   else b<=0;
                                        if c<9 then c<=c+1;
                                        else c<=0;
                                             if d<5 then d<=d+1;
                                             else d<=0;
                                             end if;
                                        end if;
                                   end if; 
                              end if;
                           end if;
            end case;
       end if;
   end if;
end process;
------------------------------------------------------------------------------------
a2:process(clk,a,b,c,d,count_en,stop_en)
variable n:integer range 0 to 100;
begin
 if clk'event and clk='1' then
   if count_en='1' or stop_en='1' then
          n:=n+1;
        if n=1 then
          p<="1110";
            if      a=0 then y<="0111111"; --7段数码管显示数字
             elsif  a=1 then y<="0000110";
              elsif a=2 then y<="1011011";
              elsif a=3 then y<="1001111";
              elsif a=4 then y<="1100110";
              elsif a=5 then y<="1101101";
              elsif a=6 then y<="1111101";
              elsif a=7 then y<="0000111";
              elsif a=8 then y<="1111111";
              elsif a=9 then y<="1101111";
             end if;           
         elsif  n=2 then
               p<="1101";     
                 if     b=0 then    y<="0111111";
                  elsif b=1 then    y<="0000110";
                  elsif b=2 then    y<="1011011";
                  elsif b=3 then    y<="1001111";
                  elsif b=4 then    y<="1100110";
                  elsif b=5 then    y<="1101101";
                 end if;              
         elsif n=3 then
           p<="1011";
              if       c=0 then   y<="0111111";
                 elsif c=1 then   y<="0000110";
                 elsif c=2 then   y<="1011011";
                 elsif c=3 then   y<="1001111";
                 elsif c=4 then   y<="1100110";
                 elsif c=5 then   y<="1101101";
                 elsif c=6 then   y<="1111101";
                 elsif c=7 then   y<="0000111";
                 elsif c=8 then   y<="1111111";
                 elsif c=9 then   y<="1101111";
               end if;            
         elsif n=4 then
             p<="0111";
                 if     d=0 then    y<="0111111";
                  elsif d=1 then    y<="0000110";
                  elsif d=2 then    y<="1011011";
                  elsif d=3 then    y<="1001111";
                  elsif d=4 then    y<="1100110";
                  elsif d=5 then    y<="1101101";               
                 end if;             
         elsif n=5 then
              n:=0;
         else
             null;
       end if;
     elsif clear_en='1' then
            p<="0000";
            y<="0111111";
   end if;
end if;      
end process;
-----------------------------------------------------------------------------------
--p3:process(key,clk)
--variable c:integer range 0 to 12;                
--begin
--if key="1111" then
--   c:=12;                                      
--elsif clk'event and clk='1' and clk'last_value='0' then
--      if c/=0 then 
--         c:=c-1;
--      end if;
--end if;       
--if c=0 then
--  if clk'event and clk='1' and clk'last_value='0' then                                        
--   if    key ="011" then 
--           count_en<='0';
--           stop_en<='0';
--           clear_en<='1'; 
--   elsif key="110"  then
--           count_en<='0';
--           stop_en<='1';
--           clear_en<='0';
--   elsif key="101"  then
--           count_en<='1';
--           stop_en<='0';
--            clear_en<='0';                 
--     else  null;
--   end if;
--  end if;
--end if;
--end process; 
p3:process(clk)
   begin
     if clk'event and clk='1' then 
         clk_done<=clk_done+'1';
     end if;
   end process; 

    key_add_out<=key_add_done1 or key_add_done2 or key_add_done3;

p4:process(clk_done(2))
   begin
     if clk_done(2)'event and clk_done(2)='1' then 
        key_add_done1<=key_add;
        key_add_done2<=key_add_done1;
        key_add_done3<=key_add_done2;
     end if;
   end process;

p5:process(key_count,key_stop,key_clear)
   begin
     if key_count='0' then count_en<='1'; 
                           stop_en<='0';
                           clear_en<='0';
     elsif key_stop='0' then count_en<='0';
                             stop_en<='1';
                             clear_en<='0';
     elsif key_clear='0' then count_en<='0';
                              stop_en<='0';
                              clear_en<='1';
     end if; 
   end process;
-------------------------------------------------------------------------------------
end bhv;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -