VHDL多功能数字钟设计报告

VHDL多功能数字钟设计报告
VHDL多功能数字钟设计报告

基于VHDL语言

学院:信息工程学院

专业:

姓名:

学号:

2010年6月15日

一、设计要求

1、具有以二十四小时制计时、显示、整点报时、时间设置和闹钟的功能。

2、设计精度要求为1秒。

二、设计目的

1.掌握各类计数器以及计数器的级联方式;

2.掌握数码管动态显示的原理与方法;

3.掌握用FPGA技术的层次化设计方法;

4.理解数字逻辑硬件和软件的设计思想;

三、设计环境:Quartus II CPLD-5型试验箱

四、系统功能描述

1、系统输入:系统状态及较时、定时转换的控制信号为enset、k、set;

时钟信号clk采用50MHz;校时复位信号为reset,输入信号均由按键信号产生。

2、系统输出:LED显示输出;蜂鸣器声音信号输出。

3、多功能数字电子钟系统功能的具体描述如下:

(一)计时:正常工作状态下,每日按24h计时制计时并显示,蜂鸣器无声,逢整点报时。

(二)校时:在计时显示状态下,按下“enset”键,接着按下“k”键,进入“小时”待校准状态,若此时按下“set”键,小时开始校准;之后按上“k”键则进入“分”待校准状态;继续按下“k”键则进入“秒”待复零状态;再次按上“k”键数码管显示闹钟时间,并进入闹钟“小时”待校准状态;再次按下“k”键则进入闹钟“分”待校准状态;若再按上“k”键恢复到正常计时显示状态。若校时过程中按下“reset”键,则系统恢复到正常计数状态。

(1)“小时”校准状态:在“小时”校准状态下,显示“小时”的数码管以2Hz 闪烁,并以2Hz的频率递增计数。

(2)“分”校准状态:在“分”校准状态下,显示“分”的数码管以2Hz闪烁,并以2Hz的频率递增计数。

(3)“秒”校准状态:在“秒复零”状态下,显示“秒”的数码管以2Hz闪烁,并以1Hz的频率递增计数。

(4)闹钟“小时”校准状态:在闹钟“小时”校准状态下,显示“小时”的数码管以2Hz闪烁,并以2Hz的频率递增计数。

(5)闹钟“分”校准状态:在闹钟“分”校准状态下,显示“分”的数码管以2Hz闪烁,并以2Hz的频率递增计数。

(三)整点报时:蜂鸣器在“59”分钟的第“51”、“53”、“55”、“57”秒发频率为500Hz的低音,在“59”分钟的第“59”秒发频率为1000Hz的高音,结束时为整点。

(四)显示:要求采用扫描显示方式驱动6个LED数码管显示小时、分、秒。(五)闹钟:闹钟定时时间到,蜂鸣器发出频率为1000Hz的高音,持续时间为60秒。

五、各个模块分析说明

1、分频器模块

(1)模块说明:输入一个频率为50MHz的CLK,利用计数器分出

1KHz的q1KHz,500Hz的q500Hz,2Hz的q2Hz和1Hz的q1Hz。

(2)源程序:

LIBRARY ieee;

USE ieee.std_logic_1164.all;

use ieee.std_logic_unsigned.all;

ENTITY fdiv IS

PORT (CLK: IN STD_LOGIC ; --输入时钟信号

q1KHz: BUFFER STD_LOGIC;

q500Hz: BUFFER STD_LOGIC;

q2Hz: BUFFER STD_LOGIC;

q1Hz: OUT STD_LOGIC);

END fdiv ;

ARCHITECTURE bhv OF fdiv IS

BEGIN

P1KHZ:PROCESS(CLK)

VARIABLE cout:INTEGER:=0;

BEGIN

IF CLK'EVENT AND CLK='1' THEN

cout:=cout+1; --每来个时钟上升沿时cout开始计数IF cout<=25000 THEN q1KHz<='0'; --当cout<=25000时,q1KHz输出“0” ELSIF cout<50000 THEN q1KHz<='1'; --当25000

END IF;

END IF;

END PROCESS;

P500HZ:PROCESS(q1KHz) --q1KHz作为输入信号,分出q500Hz VARIABLE cout:INTEGER:=0;

BEGIN

IF q1KHz'EVENT AND q1KHz='1' THEN

cout:=cout+1;

IF cout=1 THEN q500Hz<='0'; --二分频

ELSIF cout=2 THEN cout:=0;q500Hz<='1';

END IF;

END IF;

END PROCESS;

P2HZ:PROCESS(q500Hz)

VARIABLE cout:INTEGER:=0;

BEGIN

IF q500Hz'EVENT AND q500Hz='1' THEN

cout:=cout+1;

IF cout<=125 THEN q2Hz<='0';

ELSIF cout<250 THEN q2Hz<='1';

ELSE cout:=0;

END IF;

END IF;

END PROCESS;

P1HZ:PROCESS(q2Hz)

VARIABLE cout:INTEGER:=0;

BEGIN

IF q2Hz'EVENT AND q2Hz='1' THEN

cout:=cout+1;

IF cout=1 THEN q1Hz<='0';

ELSIF cout=2 THEN cout:=0;q1Hz<='1';

END IF;

END IF;

END PROCESS;

END bhv;

(3)模块图:

2、控制器模块

(1)模块说明:输入端口enset,k,set键来控制6个状态,这六个状态分别是显示计时时间状态,调计时的时、分、秒状态,调闹铃的时、分的状态,reset 键是复位键,用来回到显示计时时间的状态。

(2)源程序:

library ieee;

use ieee.std_logic_1164.all;

entity contl is

port(clk,enset,k,set,reset:in std_logic;

cth,ctm,cts,cbh,cbm,flashh,flashm,flashs,sel_show:out

std_logic);

end contl;

architecture rtl of contl is

type stats is (s0,s1,s2,s3,s4,s5); --定义6个状态

signal current_state,next_state:stats:=s0;

begin

process(clk,reset)

begin

if reset='1' then

current_state<=s0;

elsif clk'event and clk='1' then

if reset='0' then

current_state<=next_state;

end if;

end if;

end process;

process(current_state,enset,k,set)

begin

case current_state is

when s0=>cth<='0';ctm<='0';cts<='0';cbh<='0';cbm<='0';

flashh<='0';flashm<='0';flashs<='0';sel_show<='0';

if (enset='1' and k='1')then --若enset和k为“1”,

next_state<=s1; --由s0态转到s1态

else next_state<=s0;

end if;

when s1=>ctm<='0';cts<='0';cbh<='0';cbm<='0';

flashh<='1';flashm<='0';flashs<='0';sel_show<='0';

if set='1' then cth<='1'; --若set为“1”,cth输出“1” else cth<='0'; --进入调小时状态。

end if;

if (enset='1' and k='0')then --若enest为“1”,k为“0”, next_state<=s2; --由s1态转到s2态

else next_state<=s1;

end if;

when s2=>cth<='0';cts<='0';cbh<='0';cbm<='0';

flashh<='0';flashm<='1';flashs<='0';sel_show<='0';

if set='1' then ctm<='1';

else ctm<='0';

end if;

if(enset='1' and k='1')then

next_state<=s3;

else next_state<=s2;

end if;

when s3=>cth<='0';ctm<='0';cbh<='0';cbm<='0';

flashh<='0';flashm<='0';flashs<='1';sel_show<='0';

if set='1' then cts<='1';

else cts<='0';

end if;

if (enset='1' and k='0')then

next_state<=s4;

else next_state<=s3;

end if;

when s4=>cth<='0';ctm<='0';cts<='0';cbm<='0';

flashh<='1';flashm<='0';flashs<='0';sel_show<='1'; if set='1' then cbh<='1';

else cbh<='0';

end if;

if(enset='1' and k='1')then

next_state<=s5;

else next_state<=s4;

end if;

when s5=>cth<='0';ctm<='0';cts<='0';cbh<='0';

flashh<='0';flashm<='1';flashs<='0';sel_show<='1'; if set='1' then cbm<='1';

else cbm<='0';

end if;

if(enset='1' and k='0')then

next_state<=s0;

else next_state<=s5;

end if;

end case;

end process;

end rtl;

(3)仿真波形图:

(4)模块图:

3、二选一模块

(1)源程序:

ENTITY mux21a IS

PORT(a,b,s:IN BIT;

y:OUT BIT);

END ENTITY mux21a;

ARCHITECTURE one OF mux21a IS

BEGIN

PROCESS(a,b,s)

BEGIN

IF s='0' THEN

y<=a;ELSE --若s=0,y输出a,反之输出b。

y<=b;

END IF;

END PROCESS;

END ARCHITECTURE one;

(2)仿真波形图:

(3)模块图:

4、计时模块

a、秒计时

(1)源程序:

library ieee;

use ieee.std_logic_1164.all;

use ieee.std_logic_unsigned.all;

use ieee.std_logic_arith;

entity cnts60 is

port(sld0:buffer std_logic_vector(3 downto 0); --小时个位 sld1:buffer std_logic_vector(7 downto 4); --小时十位 co: out std_logic;

rest:in std_logic;

clk :in std_logic);

end cnts60;

architecture rtl of cnts60 is

begin

process(clk,rest)

begin

if rest='1' then sld1<="0000"; sld0<="0000";

elsif (clk'event and clk='1') then

if (sld1="0101"and sld0="1001")then --当sld1=5,sld0=9时 sld1<="0000"; sld0<="0000";co<='1';--全清零,co输出“1” elsif sld0="1001" then

sld0<="0000";

sld1<=sld1+1;co<='0'; --sld1自加“1”

else sld0<=sld0+1;co<='0';

end if;

end if;

end process;

end rtl;

(2)仿真波形图:

(3)模块图:

b、分计时

(1)源程序:

library ieee;

use ieee.std_logic_1164.all;

use ieee.std_logic_unsigned.all;

use ieee.std_logic_arith;

entity cnt60 is

port(sld0:buffer std_logic_vector(3 downto 0);

sld1:buffer std_logic_vector(7 downto 4);

co: out std_logic;

clk :in std_logic);

end cnt60;

architecture rtl of cnt60 is

begin

process(clk)

begin

if (clk'event and clk='1') then

if (sld1="0101"and sld0="1001")then

sld1<="0000"; sld0<="0000";co<='1';

elsif sld0="1001" then

sld0<="0000";

sld1<=sld1+1;co<='0';

else sld0<=sld0+1;co<='0';

end if;

end if;

end process;

end rtl;

(2)仿真波形图:

(3)模块图:

c、小时计时

(1)源程序:

library ieee;

use ieee.std_logic_1164.all;

use ieee.std_logic_unsigned.all;

use ieee.std_logic_arith;

entity cnt24 is

port(sld0:buffer std_logic_vector(3 downto 0); sld1:buffer std_logic_vector(7 downto 4); clk :in std_logic);

end cnt24;

architecture rtl of cnt24 is

signal s:std_logic_vector(7 downto 0);

begin

process(clk)

begin

s<=sld1&sld0;

if (clk'event and clk='1') then

if s="00100011"then

sld1<="0000";

sld0<="0000";

elsif sld0="1001" then

sld0<="0000";

sld1<=sld1+1;

else sld0<=sld0+1;

end if;

end if;

end process;

end rtl;

(2)仿真波形图:

(3)模块图:

d、闹钟分计时

(1)源程序:

library ieee;

use ieee.std_logic_1164.all;

use ieee.std_logic_unsigned.all;

use ieee.std_logic_arith;

entity cntm60b is

port(sld0:buffer std_logic_vector(3 downto 0); sld1:buffer std_logic_vector(7 downto 4); en:in std_logic;

clk :in std_logic);

end cntm60b;

architecture rtl of cntm60b is

begin

process(clk)

begin

if (clk'event and clk='1') then

if en='1' then

if (sld1="0101"and sld0="1001")then

sld1<="0000"; sld0<="0000";

elsif sld0="1001" then

sld0<="0000";

sld1<=sld1+1;

else sld0<=sld0+1;

end if;

end if;

end if;

end process;

end rtl;

(2)仿真波形图:

(3)模块图:

e、闹钟小时计时

(1)源程序:

library ieee;

use ieee.std_logic_1164.all;

use ieee.std_logic_unsigned.all;

use ieee.std_logic_arith;

entity cnth24b is

port(sld0:buffer std_logic_vector(3 downto 0); sld1:buffer std_logic_vector(7 downto 4); en:in std_logic;

clk :in std_logic);

end cnth24b;

architecture rtl of cnth24b is

signal s:std_logic_vector(7 downto 0);

begin

process(clk,en,sld1,sld0)

begin

s<=sld1&sld0;

if (clk'event and clk='1') then

if en='1' then

if s="00100011"then

sld1<="0000";

sld0<="0000";

elsif sld0="1001" then

sld0<="0000";

sld1<=sld1+1;

else sld0<=sld0+1;

end if;

end if;

end if;

end process;

end rtl;

(2)仿真波形图:

(3)模块图:

5、闹钟比较模块

(1)模块说明:比较正常计数时间与闹钟定时时间是否相等,若相等,compout 输出“1”,反之输出“0”。

(2)源程序:

library ieee;

use ieee.std_logic_1164.all;

entity comp is

port(th1,tm1:in std_logic_vector(7 downto 4);

th0,tm0:in std_logic_vector(3 downto 0);

bh1,bm1:in std_logic_vector(7 downto 4);

bh0,bm0:in std_logic_vector(3 downto 0);

compout:out std_logic);

end comp;

architecture rtl of comp is

begin

process(th1,tm1,bh1,bm1,th0,tm0,bh0,bm0)

begin

if( th1=bh1 and tm1=bm1 and th0=bh0 and tm0=bm0 ) then

compout<='1';--当正常计数时间与闹钟定时时间相等时compout输出1

else compout<='0';

end if;

end process;

end rtl;

(3)仿真波形图:

(4)模块图:

6、报时模块

(1)模块说明:该模块既实现了整点报时的功能,又实现了闹铃的功能,蜂鸣器通过所选频率的不同,而发出不同的声音。

(2)源程序:

library ieee;

use ieee.std_logic_1164.all;

use ieee.std_logic_unsigned.all;

entity bel is

port(tm1,ts1:in std_logic_vector(7 downto 4);

tm0,ts0:in std_logic_vector(3 downto 0);

compout,q1KHz,q500Hz,openbel:in std_logic;

bell:out std_logic);

end bel;

architecture rtl of bel is

signal t:std_logic_vector(7 downto 0);

begin

process(compout,q500Hz,q1KHz,openbel,tm1,tm0,ts1,ts0)

begin

if (openbel ='0' ) then

bell<='0';

end if;

if (openbel ='1' ) then

if (compout='1') then --compout=1,闹铃响

bell<=q1KHz; --bell输出1KHz

elsif (tm1="0101" and tm0="1001"and ts1="0101")then case ts0 is

when"0001"=>bell<=q500Hz;

when"0011"=>bell<=q500Hz;

when"0101"=>bell<=q500Hz;

when"0111"=>bell<=q500Hz; --bell输出500Hz

when"1001"=>bell<=q1KHz; --bell输出1KHz

when others=>bell<='0';

end case;

else bell<='0';

end if;

end if;

end process;

end rtl;

(3)仿真波形图:

(4)模块图:

7、控制显示模块

(1)模块说明:该模块实现了数码管既可以显示正常时间,又可以显示闹钟时间的功能;调时,定时闪烁功能也在此模块中真正实现。

(2)源程序:

library ieee;

use ieee.std_logic_1164.all;

entity show_con is

port(th1,tm1,ts1:in std_logic_vector(7 downto 4);

th0,tm0,ts0:in std_logic_vector(3 downto 0);

bh1,bm1:in std_logic_vector(7 downto 4);

bh0,bm0:in std_logic_vector(3 downto 0);

sec1,min1,h1: out std_logic_vector(7 downto 4);

sec0,min0,h0: out std_logic_vector(3 downto 0);

q2Hz,flashs,flashh,flashm,sel_show:in std_logic);

end show_con;

architecture rtl of show_con is

begin

process(th1,tm1,ts1,th0,tm0,ts0,bh1,bm1,bh0,bm0,q2Hz,flashs,flashh,f lashm,sel_show)

begin

if sel_show='0'then

if ( flashh='1'and q2Hz='1')then

h1<="1111";h0<="1111"; --显示小时数码管以2Hz闪烁

min1<=tm1;min0<=tm0;

sec1<=ts1;sec0<=ts0;

elsif (flashm='1'and q2Hz='1')then

h1<=th1;h0<=th0;

min1<="1111";min0<="1111";

sec1<=ts1;sec0<=ts0;

elsif (flashs='1'and q2Hz='1')then

h1<=th1;h0<=th0;

min1<=tm1;min0<=tm0;

sec1<="1111";sec0<="1111";

else

h1<=th1;h0<=th0;

min1<=tm1;min0<=tm0;

sec1<=ts1;sec0<=ts0;

end if;

elsif sel_show='1'then--若sel_show为“1”,数码管显示闹钟时间 if(flashh='1' and q2Hz='1')then

h1<="1111";h0<="1111";

min1<=bm1;min0<=bm0;

sec1<="0000";sec0<="0000";

elsif ( flashm='1' and q2Hz='1')then

h1<=bh1;h0<=bh0;

min1<="1111";min0<="1111";

sec1<="0000";sec0<="0000";

else

h1<=bh1;h0<=bh0;

min1<=bm1;min0<=bm0;

sec1<="0000";sec0<="0000";

end if ;

end if;

end process;

end rtl;

(3)模块图:

8、动态扫描显示模块

(1)模块说明:由6组输入信号和7个数据输出信号和8个位选通信号来实现数码管的驱动和信号扫描,进而实现了时钟时,分,秒的动态显示。

(2)源程序:

library ieee;

use ieee.std_logic_1164.all;

use ieee.std_logic_unsigned.all;

entity SCAN_LED is

port(clk1:in std_logic;

h0:in std_logic_vector(3 downto 0);

h1:in std_logic_vector(7 downto 4);

min0:in std_logic_vector(3 downto 0);

min1:in std_logic_vector(7 downto 4);

sec0:in std_logic_vector(3 downto 0);

sec1:in std_logic_vector(7 downto 4);

daout:out std_logic_vector(6 downto 0);

se:out std_logic_vector(2 downto 0));

end;

architecture one of SCAN_LED is

signal cnt6:std_logic_vector(2 downto 0); signal a: std_logic_vector(3 downto 0) ;

begin

p1:process(clk1)

begin

if clk1'event and clk1 ='1' then

cnt6<=cnt6+1;

if cnt6=5 then

cnt6<="000";

end if;

end if;

se<=cnt6;

end process p1;

p2:process(cnt6,h1,h0,min1,min0,sec1,sec0) begin

case cnt6 is --控制数码管位选when "000"=>a<=sec0;

when "001"=>a<=sec1;

when "010"=>a<=min0;

when "011"=>a<=min1;

when "100"=>a<=h0;

when "101"=>a<=h1;

when others =>null;

end case;

end process p2;

p3:process(a)

begin

case a is --控制数码管段选when"0000"=>daout<="0111111"; when"0001"=>daout<="0000110"; when"0010"=>daout<="1011011"; when"0011"=>daout<="1001111"; when"0100"=>daout<="1100110"; when"0101"=>daout<="1101101"; when"0110"=>daout<="1111101"; when"0111"=>daout<="0000111"; when"1000"=>daout<="1111111"; when"1001"=>daout<="1101111";

when others=>NULL;

end case;

end process p3;

end;

(3)仿真波形图:

(4)模块图:

六、引脚锁定

七、顶层电路图

相关主题
相关文档
最新文档