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

📄 clock.vhd

📁 用高速硬件语言VHDL设计的全功能数字钟
💻 VHD
字号:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity clock is
port(clk:in std_logic;--时钟输入12MHZ
     clr:in std_logic;--清零端
     en:in std_logic;--暂停信号
     mode:in std_logic;--控制信号,用于选择模式
     inc:in std_logic;--置数端
     seg7:out std_logic_vector(6 downto 0);--7段显示控制信号(abcdefg)
     scan:out std_logic_vector(5 downto 0)--数码管地址选择信号
    );
end clock;
architecture one of clock is
signal state:std_logic_vector(1 downto 0);--定义4种状态
signal qhh,qhl,qmh,qml,qsh,qsl:std_logic_vector(3 downto 0);-- 小时,分,秒的高位,低位
signal data:std_logic_vector(3 downto 0);
signal cnt:integer range 0 to 5;--扫描数码管的计数器
signal clk1khz,clk1hz,clk2hz:std_logic;--1khz,1hz,2hz的分频信号
signal blink:std_logic_vector(2 downto 0);--闪烁信号
signal inc_reg:std_logic;
signal sec,min:integer range 0 to 59;
signal hour:integer range 0 to 23;
begin
--------------------------1KHZ分频,用于扫描数码管地址
process(clk)
variable count:integer range 0 to 5999;
begin
if clk'event and clk='1' then
   if count=5999 then
      clk1khz<=not clk1khz;
      count:=0;
   else
      count:=count+1;
   end if;
end if;
end process;
---------------------------1HZ分频,用于计时
process(clk)
variable count:integer range 0 to 5529599;
begin
if clk'event and clk='1' then
   if count=5529599 then
      clk1hz<=not clk1hz;
      count:=0;
   else
      count:=count+1;
   end if;
end if;
end process;
--------------------------2HZ分频。用于数码管闪烁
process(clk1khz)
variable count:integer range 0 to 249;
begin
if clk1khz'event and clk1khz='1' then
   if count=249 then
      clk2hz<=not clk2hz;
      count:=0;
   else
      count:=count+1;
   end if;
end if;
end process;
-------------------------模式转换
process(mode,clr)
begin
if clr='0' then
   state<="00";
elsif mode'event and mode='0' then
   state<=state+1;
end if;
end process;
-------------------------状态控制
process(clk1hz,state,en,clr,hour,sec,min)
begin
if en='0' then
   hour<=hour;
   min<=min;
   sec<=sec;
elsif clr='0' then
   hour<=0;
   min<=0;
   sec<=0;
elsif clk1hz'event and clk1hz='1' then
   case state is
   when "00"=>if sec=59 then--------------------模式0, 正常计时
                 sec<=0;   
                 if min=59 then
                    min<=0;
                    if hour=23 then
                       hour<=0;
                    else hour<=hour+1;end if;
                  else min<=min+1;end if;
               else sec<=sec+1;end if;
  when "01"=>if inc='0' then---------------------模式1,设定小时时间
                if inc_reg='0' then
                   inc_reg<='1'; 
                   if hour=23 then
                      hour<=0;
                   else
                      hour<=hour+1;
                   end if;
                 end if;
              else
                 inc_reg<='0';
              end if;
  when "10"=>if inc='0' then---------------------模式2,设定分钟时间
                if inc_reg='0' then
                   inc_reg<='1'; 
                   if min=59 then
                      min<=0;
                   else
                     min<=min+1;
                   end if;
                 end if;
              else
                 inc_reg<='0';
              end if;
  when "11"=>if inc='0' then---------------------模式3,设定秒钟时间
                if inc_reg='0' then
                   inc_reg<='1'; 
                   if sec=59 then
                      sec<=0;
                   else
                     sec<=sec+1;
                   end if;
                 end if;
              else
                 inc_reg<='0';
              end if;
  end case;
end if;
end process;
------------------------当进行时间设定时,令数码管闪烁
process(state,clk2hz)
begin
  case state is
  when "00"=>blink<="000";
  when "01"=>blink<=(2=>clk2hz,others=>'0');
  when "10"=>blink<=(1=>clk2hz,others=>'0');
  when "11"=>blink<=(0=>clk2hz,others=>'0');              
  end case;
end process;
------------------------秒计数的十进制转BCD码
process(sec)
begin
  case sec is
  when 0|10|20|30|40|50=>qsl<="0000";
  when 1|11|21|31|41|51=>qsl<="0001";
  when 2|12|22|32|42|52=>qsl<="0010";
  when 3|13|23|33|43|53=>qsl<="0011";
  when 4|14|24|34|44|54=>qsl<="0100";          
  when 5|15|25|35|45|55=>qsl<="0101";               
  when 6|16|26|36|46|56=>qsl<="0110";
  when 7|17|27|37|47|57=>qsl<="0111";
  when 8|18|28|38|48|58=>qsl<="1000";
  when 9|19|29|39|49|59=>qsl<="1001";
  when others=>null;
  end case;
  case sec is
  when 0|1|2|3|4|5|6|7|8|9=>qsh<="0000";
  when 10|11|12|13|14|15|16|17|18|19=>qsh<="0001";
  when 20|21|22|23|24|25|26|27|28|29=>qsh<="0010";
  when 30|31|32|33|34|35|36|37|38|39=>qsh<="0011";
  when 40|41|42|43|44|45|46|47|48|49=>qsh<="0100";
  when 50|51|52|53|54|55|56|57|58|59=>qsh<="0101";
  when others=>null;
  end case;
end process;
-------------------------分计数的十进制转BCD码
process(min)
begin
  case min is
  when 0|10|20|30|40|50=>qml<="0000";
  when 1|11|21|31|41|51=>qml<="0001";
  when 2|12|22|32|42|52=>qml<="0010";
  when 3|13|23|33|43|53=>qml<="0011";
  when 4|14|24|34|44|54=>qml<="0100";          
  when 5|15|25|35|45|55=>qml<="0101";               
  when 6|16|26|36|46|56=>qml<="0110";
  when 7|17|27|37|47|57=>qml<="0111";
  when 8|18|28|38|48|58=>qml<="1000";
  when 9|19|29|39|49|59=>qml<="1001";
  when others=>null;
  end case;
  case min is
  when 0|1|2|3|4|5|6|7|8|9=>qmh<="0000";
  when 10|11|12|13|14|15|16|17|18|19=>qmh<="0001";
  when 20|21|22|23|24|25|26|27|28|29=>qmh<="0010";
  when 30|31|32|33|34|35|36|37|38|39=>qmh<="0011";
  when 40|41|42|43|44|45|46|47|48|49=>qmh<="0100";
  when 50|51|52|53|54|55|56|57|58|59=>qmh<="0101";
  when others=>null;
  end case;
end process;
------------------------------小时计数的十进制转BCD码
process(hour)
begin
  case hour is
  when 0|10|20=>qhl<="0000";
  when 1|11|21=>qhl<="0001";
  when 2|12|22=>qhl<="0010";
  when 3|13|23=>qhl<="0011";
  when 4|14=>qhl<="0100";
  when 5|15=>qhl<="0101";
  when 6|16=>qhl<="0110";
  when 7|17=>qhl<="0111";
  when 8|18=>qhl<="1000";
  when 9|19=>qhl<="1001";
  when others=>null;
  end case;
  case hour is
  when 0|1|2|3|4|5|6|7|8|9=>qhh<="0000";
  when 10|11|12|13|14|15|16|17|18|19=>qhh<="0001";
  when 20|21|22|23=>qhh<="0010";
  when others=>null;
  end case;
end process;
--------------------------数码管动态扫描计数
process(clk1khz)
begin
if clk1khz'event and clk1khz='1' then
   if cnt=5 then
      cnt<=0;
   else
      cnt<=cnt+1;
   end if;
end if;
end process;
--------------------------数码管动态扫描
process(cnt,qhh,qhl,qmh,qml,qsh,qsl,blink)
begin
  case cnt is
  when 0=>data<=qsl or(blink(0)&blink(0)&blink(0)&blink(0));scan<="000001";
  when 1=>data<=qsh or(blink(0)&blink(0)&blink(0)&blink(0));scan<="000010";
  when 2=>data<=qml or(blink(1)&blink(1)&blink(1)&blink(1));scan<="000100";
  when 3=>data<=qmh or(blink(1)&blink(1)&blink(1)&blink(1));scan<="001000";
  when 4=>data<=qhl or(blink(2)&blink(2)&blink(2)&blink(2));scan<="010000";
  when 5=>data<=qhh or(blink(2)&blink(2)&blink(2)&blink(2));scan<="100000";
  when others=>null;
  end case;
end process;
--------------------------7段译码
process(data)
begin
  case data is
  when "0000"=>seg7<="0111111";
  when "0001"=>seg7<="0000110";
  when "0010"=>seg7<="1011011";
  when "0011"=>seg7<="1001111";
  when "0100"=>seg7<="1100110";
  when "0101"=>seg7<="1101101";
  when "0110"=>seg7<="1111101";
  when "0111"=>seg7<="0000111";
  when "1000"=>seg7<="1111111";
  when "1001"=>seg7<="1101111";
  when others=>seg7<="0000000";
  end case;
end process;
end one;

⌨️ 快捷键说明

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