ELSEcout:
=0;--输出“1”,完成1KHz频率输出
ENDIF;
ENDIF;
ENDPROCESS;
P500HZ:
PROCESS(q1KHz)--q1KHz作为输入信号,分出q500Hz
VARIABLEcout:
INTEGER:
=0;
BEGIN
IFq1KHz'EVENTANDq1KHz='1'THEN
cout:
=cout+1;
IFcout=1THENq500Hz<='0';--二分频
ELSIFcout=2THENcout:
=0;q500Hz<='1';
ENDIF;
ENDIF;
ENDPROCESS;
P2HZ:
PROCESS(q500Hz)
VARIABLEcout:
INTEGER:
=0;
BEGIN
IFq500Hz'EVENTANDq500Hz='1'THEN
cout:
=cout+1;
IFcout<=125THENq2Hz<='0';
ELSIFcout<250THENq2Hz<='1';
ELSEcout:
=0;
ENDIF;
ENDIF;
ENDPROCESS;
P1HZ:
PROCESS(q2Hz)
VARIABLEcout:
INTEGER:
=0;
BEGIN
IFq2Hz'EVENTANDq2Hz='1'THEN
cout:
=cout+1;
IFcout=1THENq1Hz<='0';
ELSIFcout=2THENcout:
=0;q1Hz<='1';
ENDIF;
ENDIF;
ENDPROCESS;
ENDbhv;
(3)模块图:
2、控制器模块
(1)模块说明:
输入端口enset,k,set键来控制6个状态,这六个状态分别是
显示计时时间状态,调计时的时、分、秒状态,调闹铃的时、分的状态,reset键是复位键,用来回到显示计时时间的状态。
(2)源程序:
libraryieee;
useieee.std_logic_1164.all;
entitycontlis
port(clk,enset,k,set,reset:
instd_logic;
cth,ctm,cts,cbh,cbm,flashh,flashm,flashs,sel_show:
outstd_logic);
endcontl;
architecturertlofcontlis
typestatsis(s0,s1,s2,s3,s4,s5);--定义6个状态
signalcurrent_state,next_state:
stats:
=s0;
begin
process(clk,reset)
begin
ifreset='1'then
current_state<=s0;
elsifclk'eventandclk='1'then
ifreset='0'then
current_state<=next_state;
endif;
endif;
endprocess;
process(current_state,enset,k,set)
begin
casecurrent_stateis
whens0=>cth<='0';ctm<='0';cts<='0';cbh<='0';cbm<='0';
flashh<='0';flashm<='0';flashs<='0';sel_show<='0';
if(enset='1'andk='1')then--若enset和k为“1”,
next_state<=s1;--由s0态转到s1态
elsenext_state<=s0;
endif;
whens1=>ctm<='0';cts<='0';cbh<='0';cbm<='0';
flashh<='1';flashm<='0';flashs<='0';sel_show<='0';
ifset='1'thencth<='1';--若set为“1”,cth输出“1”
elsecth<='0';--进入调小时状态。
endif;
if(enset='1'andk='0')then--若enest为“1”,k为“0”,
next_state<=s2;--由s1态转到s2态
elsenext_state<=s1;
endif;
whens2=>cth<='0';cts<='0';cbh<='0';cbm<='0';
flashh<='0';flashm<='1';flashs<='0';sel_show<='0';
ifset='1'thenctm<='1';
elsectm<='0';
endif;
if(enset='1'andk='1')then
next_state<=s3;
elsenext_state<=s2;
endif;
whens3=>cth<='0';ctm<='0';cbh<='0';cbm<='0';
flashh<='0';flashm<='0';flashs<='1';sel_show<='0';
ifset='1'thencts<='1';
elsects<='0';
endif;
if(enset='1'andk='0')then
next_state<=s4;
elsenext_state<=s3;
endif;
whens4=>cth<='0';ctm<='0';cts<='0';cbm<='0';
flashh<='1';flashm<='0';flashs<='0';sel_show<='1';
ifset='1'thencbh<='1';
elsecbh<='0';
endif;
if(enset='1'andk='1')then
next_state<=s5;
elsenext_state<=s4;
endif;
whens5=>cth<='0';ctm<='0';cts<='0';cbh<='0';
flashh<='0';flashm<='1';flashs<='0';sel_show<='1';
ifset='1'thencbm<='1';
elsecbm<='0';
endif;
if(enset='1'andk='0')then
next_state<=s0;
elsenext_state<=s5;
endif;
endcase;
endprocess;
endrtl;
(3)仿真波形图:
(4)模块图:
3、二选一模块
(1)源程序:
ENTITYmux21aIS
PORT(a,b,s:
INBIT;
y:
OUTBIT);
ENDENTITYmux21a;
ARCHITECTUREoneOFmux21aIS
BEGIN
PROCESS(a,b,s)
BEGIN
IFs='0'THEN
y<=a;ELSE--若s=0,y输出a,反之输出b。
y<=b;
ENDIF;
ENDPROCESS;
ENDARCHITECTUREone;
(2)仿真波形图:
(3)模块图:
4、计时模块
a、秒计时
(1)源程序:
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
useieee.std_logic_arith;
entitycnts60is
port(sld0:
bufferstd_logic_vector(3downto0);--小时个位
sld1:
bufferstd_logic_vector(7downto4);--小时十位
co:
outstd_logic;
rest:
instd_logic;
clk:
instd_logic);
endcnts60;
architecturertlofcnts60is
begin
process(clk,rest)
begin
ifrest='1'thensld1<="0000";sld0<="0000";
elsif(clk'eventandclk='1')then
if(sld1="0101"andsld0="1001")then--当sld1=5,sld0=9时
sld1<="0000";sld0<="0000";co<='1';--全清零,co输出“1”
elsifsld0="1001"then
sld0<="0000";
sld1<=sld1+1;co<='0';--sld1自加“1”
elsesld0<=sld0+1;co<='0';
endif;
endif;
endprocess;
endrtl;
(2)仿真波形图:
(3)模块图:
b、分计时
(1)源程序:
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
useieee.std_logic_arith;
entitycnt60is
port(sld0:
bufferstd_logic_vector(3downto0);
sld1:
bufferstd_logic_vector(7downto4);
co:
outstd_logic;
clk:
instd_logic);
endcnt60;
architecturertlofcnt60is
begin
process(clk)
begin
if(clk'eventandclk='1')then
if(sld1="0101"andsld0="1001")then
sld1<="0000";sld0<="0000";co<='1';
elsifsld0="1001"then
sld0<="0000";
sld1<=sld1+1;co<='0';
elsesld0<=sld0+1;co<='0';
endif;
endif;
endprocess;
endrtl;
(2)仿真波形图:
(3)模块图:
c、小时计时
(1)源程序:
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
useieee.std_logic_arith;
entitycnt24is
port(sld0:
bufferstd_logic_vector(3downto0);
sld1:
bufferstd_logic_vector(7downto4);
clk:
instd_logic);
endcnt24;
architecturertlofcnt24is
signals:
std_logic_vector(7downto0);
begin
process(clk)
begin
s<=sld1&sld0;
if(clk'eventandclk='1')then
ifs="00100011"then
sld1<="0000";
sld0<="0000";
elsifsld0="1001"then
sld0<="0000";
sld1<=sld1+1;
elsesld0<=sld0+1;
endif;
endif;
endprocess;
endrtl;
(2)仿真波形图:
(3)模块图:
d、闹钟分计时
(1)源程序:
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
useieee.std_logic_arith;
entitycntm60bis
port(sld0:
bufferstd_logic_vector(3downto0);
sld1:
bufferstd_logic_vector(7downto4);
en:
instd_logic;
clk:
instd_logic);
endcntm60b;
architecturertlofcntm60bis
begin
process(clk)
begin
if(clk'eventandclk='1')then
ifen='1'then
if(sld1="0101"andsld0="1001")then
sld1<="0000";sld0<="0000";
elsifsld0="1001"then
sld0<="0000";
sld1<=sld1+1;
elsesld0<=sld0+1;
endif;
endif;
endif;
endprocess;
endrtl;
(2)仿真波形图:
(3)模块图:
e、闹钟小时计时
(1)源程序:
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
useieee.std_logic_arith;
entitycnth24bis
port(sld0:
bufferstd_logic_vector(3downto0);
sld1:
bufferstd_logic_vector(7downto4);
en:
instd_logic;
clk:
instd_logic);
endcnth24b;
architecturertlofcnth24bis
signals:
std_logic_vector(7downto0);
begin
process(clk,en,sld1,sld0)
begin
s<=sld1&sld0;
if(clk'eventandclk='1')then
ifen='1'then
ifs="00100011"then
sld1<="0000";
sld0<="0000";
elsifsld0="1001"then
sld0<="0000";
sld1<=sld1+1;
elsesld0<=sld0+1;
endif;
endif;
endif;
endprocess;
endrtl;
(2)仿真波形图:
(3)模块图:
5、闹钟比较模块
(1)模块说明:
比较正常计数时间与闹钟定时时间是否相等,若相等,compout输出“1”,反之输出“0”。
(2)源程序:
libraryieee;
useieee.std_logic_1164.all;
entitycompis
port(th1,tm1:
instd_logic_vector(7downto4);
th0,tm0:
instd_logic_vector(3downto0);
bh1,bm1:
instd_logic_vector(7downto4);
bh0,bm0:
instd_logic_vector(3downto0);
compout:
outstd_logic);
endcomp;
architecturertlofcompis
begin
process(th1,tm1,bh1,bm1,th0,tm0,bh0,bm0)
begin
if(th1=bh1andtm1=bm1andth0=bh0andtm0=bm0)then
compout<='1';--当正常计数时间与闹钟定时时间相等时compout输出1
elsecompout<='0';
endif;
endprocess;
endrtl;
(3)仿真波形图:
(4)模块图:
6、报时模块
(1)模块说明:
该模块既实现了整点报时的功能,又实现了闹铃的功能,蜂鸣器通过所选频率的不同,而发出不同的声音。
(2)源程序:
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
entitybelis
port(tm1,ts1:
instd_logic_vector(7downto4);
tm0,ts0:
instd_logic_vector(3downto0);
compout,q1KHz,q500Hz,openbel:
instd_logic;
bell:
outstd_logic);
endbel;
architecturertlofbelis
signalt:
std_logic_vector(7downto0);
begin
process(compout,q500Hz,q1KHz,openbel,tm1,tm0,ts1,ts0)
begin
if(openbel='0')then
bell<='0';
endif;
if(openbel='1')then
if(compout='1')then--compout=1,闹铃响
bell<=q1KHz;--bell输出1KHz
elsif(tm1="0101"andtm0="1001"andts1="0101")then
casets0is
when"0001"=>bell<=q500Hz;
when"0011"=>bell<=q500Hz;
when"0101"=>bell<=q500Hz;
when"0111"=>bell<=q500Hz;--bell输出500Hz
when"1001"=>bell<=q1KHz;--bell输出1KHz
whenothers=>bell<='0';
endcase;
elsebell<='0';
endif;
endif;
endprocess;
endrtl;
(3)仿真波形图:
(4)模块图:
7、控制显示模块
(1)模块说明:
该模块实现了数码管既可以显示正常时间,又可以显示闹钟时间的功能;调时,定时闪烁功能也在此模