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

📄 bcd clock.vhd

📁 基于bcd码校时的数字钟
💻 VHD
字号:
LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.std_logic_arith.all;
USE ieee.std_logic_unsigned.all;
entity clock is
 port(             en,set,play:in std_logic;       
                   clk1,choice:in std_logic;
                        check :in std_logic_vector(3 downto 0);
                        qd:out std_logic_vector(3 downto 0);
                       q1,q2,q3: out std_logic_vector(7 downto 0);
                         en3, bell: out std_logic  );
end clock ;
ARCHITECTURE clock  OF clock  IS
 signal qa1:std_logic_vector(3 downto 0);
 signal qb1:std_logic_vector(3 downto 0);
 signal qa2:std_logic_vector(3 downto 0);
 signal qb2:std_logic_vector(3 downto 0);
 signal qa3:std_logic_vector(3 downto 0);
 signal qb3:std_logic_vector(3 downto 0);
 signal ua1:std_logic_vector(3 downto 0);
 signal ub1:std_logic_vector(3 downto 0);
 signal ua2:std_logic_vector(3 downto 0);
 signal ub2:std_logic_vector(3 downto 0);
 signal ua3:std_logic_vector(3 downto 0);
 signal ub3:std_logic_vector(3 downto 0);
 signal sa1:std_logic_vector(3 downto 0);
 signal sb1:std_logic_vector(3 downto 0);
 signal sa2:std_logic_vector(3 downto 0);
 signal sb2:std_logic_vector(3 downto 0);
 signal sa3:std_logic_vector(3 downto 0);
 signal sb3:std_logic_vector(3 downto 0);
 signal clk,clk2,s,q5,k,k1,k2,en1,en2:std_logic;
 signal bell1,bell2,bell3:std_logic;
 signal q:std_logic_vector(0 to 2);--显示选择
 signal qa,qc:std_logic_vector(0 to 3);
 signal qf:std_logic_vector(0 to 5);
 signal a1,a2,a3,a4,a5,a6:std_logic;     
begin
process(set,en,play,choice)                                                     
 begin
      if set'event and set='0' then  --校时使能脉冲转电平
           en1<=not en1;               
      else en1<=en1; end if;
      if en'event and en='0' then    --闹铃使能脉冲转电平
       en2<=not en2;               
      else en2<=en2; end if;
      if play'event and play='0'then                   --显示选择与校时模式选择
           if q<1 then s<='0'; k<='1';k2<='0';q<=q+1;
        elsif q<2 then q<=q+1;s<='1';k<='0';k2<='0';
        elsif q<3 then q<=q+1;s<='0';k<='0';k2<='1';
        elsif q<4 then q<="000";s<='0';k<='0';k2<='0';
           else q<=q+1;
         end if;
      end if;                                         --调时位选
        if choice'event and choice='0' then 
           if qa<1 then qa<=qa+1;a1<='1'; a2<='0';a3<='0';a4<='0';a5<='0';a6<='0';
        elsif qa<2 then qa<=qa+1;a1<='0'; a2<='1';a3<='0';a4<='0';a5<='0';a6<='0';
        elsif qa<3 then qa<=qa+1;a1<='0'; a2<='0';a3<='1';a4<='0';a5<='0';a6<='0';
        elsif qa<4 then qa<=qa+1;a1<='0'; a2<='0';a3<='0';a4<='1';a5<='0';a6<='0';
        elsif qa<5 then qa<=qa+1;a1<='0'; a2<='0';a3<='0';a4<='0';a5<='1';a6<='0';
        elsif qa<6 then qa<=qa+1;a1<='0'; a2<='0';a3<='0';a4<='0';a5<='0';a6<='1';
        elsif qa<7 then qa<="0000";a1<='0'; a2<='0';a3<='0';a4<='0';a5<='0';a6<='0';
        else qa<=qa+1;
        end if;end if;
      if (set='1' or en='1' or play='1' or choice='1' )then bell3<='1';--按键响
         else bell3<='0' ;end if; 
 end process;
process(k,clk)
 begin
      if clk'event and clk='1' then
            if (k2='1' and en1='1')then            --调时
                if (a1='1') then
                    qa1<=check;
              elsif (a2='1') then
                   if check>"0101" then
                    qb1<=qb1;
                   else
                    qb1<=check;end if;
              elsif (a3='1') then
                    qa2<=check;
              elsif (a4='1') then
                   if check>"0101" then
                    qb2<=qb2;
                   else
                    qb2<=check;end if;
              elsif (a5='1') then
                    if (qb3="0010" and check>3) then
                     qa3<=qa3;
                    else
                    qa3<=check;end if;
              elsif (a6='1') then
                    if check>"0010" then
                      qb3<=qb3;
                     else 
                    qb3<=check;end if;end if;     
            else 
                 qa1<=qa1+1;                                          --计时
                 if (qa1>=6) and(qa1<=9) then qa1<=qa1+1;k1<='0';
                 if(qa1>=9) then qa1<="0000";QB1<=QB1+1;
                 if (qa1>=9 AND qb1>=5) then  qb1<="0000";qa2<=qa2+1;
                 if(qa2>=9) then qa2<="0000";QB2<=QB2+1;
                 if (qa2>=9 AND qb2>=5) then  qb2<="0000";qa3<="0000";k1<='1';--整点报时
                 if qb3<2  then 
                   if(qa3<9) then qa3<=qa3+1;QB3<=QB3;
                    else qb3<=qb3+1;qa3<="0000";
                    end if;
                   elsif qa3<3 then  qa3<=qa3+1;qb3<=qb3;
                else qA3<="0000";QB3<="0000";sa1<=sa1+1;sa2<=sa2+1;      --星期,日期进位
                  if sa1>=6 then sa1<="0000";
                  if  sb2<3  then 
                  if(sa2<9) then sa2<=sa2+1;sB2<=sB2;
                   else sb2<=sb2+1;sa2<="0000";
                   end if;
                  elsif sa2<1 then  sa2<=sa2+1;sb2<=sb2;
                else sA2<="0000";sB2<="0000";sa3<=sa3+1;
                  if  sb3<1 then 
                   if sa3<9 then sa3<=sa3+1;sb3<=sb3;
                    else
                    sa3<="0000";sb3<=sb3+1; end if;
                   elsif (sa3<2) then 
                    sa3<=sa3+1;sb3<=sb3;
                     else sa3<="0000";sb3<="0000";end if;end if;
                       end if;
                end if ;end if;end if;
              end if;end if;end if;
              if (en1='1' and k='1') then                         --星期或日期调整
                if (a1='1') then
                   if check>6 then
                    sa1<=sa1;
                   else
                    sa1<=check;end if;
              elsif (a2='1') then
                    sb1<=sb1;
              elsif (a3='1') then
                    if (sb2="0011" and check>1) then
                    sa2<=sa2;
                    else
                    sa2<=check;end if;
              elsif (a4='1') then
                    if sb2>3 then
                     sb2<=sb2;else
                    sb2<=check;end if;
              elsif (a5='1') then
                    if (sb3="0001" and check>2) then
                     sa3<=sa3;else
                    sa3<=check;end if;
              elsif (a6='1') then
                     if check>1 then sb3<=sb3;else
                    sb3<=check;end if;end if;
              else  sb3<=sb3; 
             end if;end if;end if;
  end process;
  process (s,en,en1,clk)                                               --定时调整
          begin  
           if clk'event  and clk='1' then
              if (en1='1' and s='1') then 
                if (a1='1') then
                    ua1<=check;
              elsif (a2='1') then
                    if check>5 then ub1<=ub1;else
                    ub1<=check;end if;
              elsif (a3='1') then
                    ua2<=check;
              elsif (a4='1') then
                    if check>5 then ub2<=ub2;else
                    ub2<=check;end if;
              elsif (a5='1') then
                    if (ub3="0010" and check>3) then
                    ua3<=ua3;else
                    ua3<=check;end if;
              elsif (a6='1') then
                    if ub3>2 then ub3<=ub3;else
                    ub3<=check;end if;end if;
              else ub3<=ub3 ;end if;  
               if en2='0' then q5<='0';                          --闹铃开关
                elsif (ub1=qb1 and ua1=qa1 and ub2=qb2 and ua2=qa2 and ub3=qb3 and ua3=qa3 ) then 
                     q5<='1';
                end if;
            end if;
        end process;
process(clk2,q5)                                                 --闹铃铃声
variable q4:integer range 0 to 10600;
variable f:std_logic;
 begin
 if clk2'event and clk2='1' then
    if q5='1' then
         if q4<1200 then f:='1';q4:=q4+1;
      elsif q4<1600 then f:='0';q4:=q4+1;
      elsif q4<2800 then f:='1';q4:=q4+1;
      elsif q4<3200 then f:='0';q4:=q4+1;
      elsif q4<4400 then f:='1';q4:=q4+1;
      elsif q4<5200 then f:='0';q4:=q4+1;
      elsif q4<8400 then f:='1';q4:=q4+1;
      elsif q4<8840 then f:='0';q4:=q4+1;
      elsif q4<9800 then f:='1';q4:=q4+1;
      elsif q4<10600 then f:='0';q4:=q4+1;
       else f:='0';q4:=0;q4:=q4+1; 
        end if;
    else f:='0';
     end if;
   end if;
 bell2<=f ;
 end process;
process(qf)                                  --当前校时位数显示
begin
case qf is
   when "100000"=>qc<="0001";
   when "010000"=>qc<="0010";
   when "001000"=>qc<="0011";
   when "000100"=>qc<="0100";
   when "000010"=>qc<="0101";
   when "000001"=>qc<="0110";
   when others =>qc<="0000";
end case;
 qf<=a1&a2&a3&a4&a5&a6;
end process;
process(clk2,k1)
variable f:std_logic;
begin
if clk2'event and clk2='1' then
if k1='1' then f:=qa1(0);
    else f:='0';end if;end if;
bell1<=f;
end process;
 process(clk1)                                                      --分频
  variable cnt:integer range 0 to 5999999;
  variable ff:std_logic;
  begin
  if clk1'event and clk1='1' then
    if cnt<5999999 then
     cnt:=cnt+1;
    else
     cnt:=0;ff:=not ff;
    end if;
  end if;
  clk<=ff;
 end process;
process(clk1)                                                     
  variable cnt:integer range 0 to 5999999;
  variable ff:std_logic;
  begin
  if clk1'event and clk1='1' then
    if cnt<1459 then
     cnt:=cnt+1;
    else
     cnt:=0;ff:=not ff;
    end if;
  end if;
  clk2<=ff;
 end process;process(s,k)
begin 
    if s='1' then                                                   --显示选择
   q1(7 downto 4)<=ub1;q1(3 downto 0)<=ua1 ;q2(7 downto 4)<=ub2 ;q2(3 downto 0)<=ua2;q3(7 downto 4)<=ub3;q3(3 downto 0)<=ua3; 
   elsif  k='1' then 
   q1(7 downto 4)<=sb1;q1(3 downto 0)<=sa1 ;q2(7 downto 4)<=sb2 ;q2(3 downto 0)<=sa2;q3(7 downto 4)<=sb3;q3(3 downto 0)<=sa3;
    else q1(7 downto 4)<=qb1;q1(3 downto 0)<=qa1 ;q2(7 downto 4)<=qb2 ;q2(3 downto 0)<=qa2;q3(7 downto 4)<=qb3;q3(3 downto 0)<=qa3; 
   end if;
 end process;
    sb1<="0000";
    bell<=(bell2 or bell1 or bell3) and clk2;en3<=en2;qd<=qc;              --闹铃状态显示
  end ;



⌨️ 快捷键说明

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