VHDL 数字钟

VHDL 数字钟,第1张

library ieee

use ieee.std_logic_1164.all

use ieee.std_logic_unsigned.all

entity MINSECONDb is

port(clk,clrm,stop:in std_logic----时钟/清零信号

secm1,secm0:out std_logic_vector(3 downto 0)----秒高位/低位

co:out std_logic)-------输出/进位信号

end MINSECONDb

architecture SEC of MINSECONDb is

signal clk1,DOUT2:std_logic

begin

process(clk,clrm)

variable cnt1,cnt0:std_logic_vector(3 downto 0)---计数

VARIABLE COUNT2 :INTEGER RANGE 0 TO 10

begin

IF CLK'EVENT AND CLK='1'THEN

IF COUNT2>=0 AND COUNT2<10 THEN

COUNT2:=COUNT2+1

ELSE COUNT2:=0

DOUT2<= NOT DOUT2

END IF

END IF

if clrm='1' then----当clr为1时,高低位均为0

cnt1:="0000"

cnt0:="0000"

elsif clk'event and clk='1' then

if stop='1' then

cnt0:=cnt0

cnt1:=cnt1

end if

if cnt1="1001" and cnt0="1000" then----当记数为98(实际是经过59个记时脉冲)

co<='1'----进位

cnt0:="1001"----低位为9

elsif cnt0<"1001" then----小于9时

cnt0:=cnt0+1----计数

--elsif cnt0="1001" then

--clk1<=not clk1

else

cnt0:="0000"

if cnt1<"1001" then----高位小于9时

cnt1:=cnt1+1

else

cnt1:="0000"

co<='0'

end if

end if

end if

secm1<=cnt1

secm0<=cnt0

end process

end SEC

模块程序清单

library ieee

use ieee.std_logic_1164.all

use ieee.std_logic_unsigned.all

entity SECOND is

port(clk,clr:in std_logic----时钟/清零信号

sec1,sec0:out std_logic_vector(3 downto 0)----秒高位/低位

co:out std_logic)-------输出/进位信号

end SECOND

architecture SEC of SECOND is

begin

process(clk,clr)

variable cnt1,cnt0:std_logic_vector(3 downto 0)---计数

begin

if clr='1' then----当ckr为1时,高低位均为0

cnt1:="0000"

cnt0:="0000"

elsif clk'event and clk='1' then

if cnt1="0101" and cnt0="1000" then----当记数为58(实际是经过59个记时脉冲)

co<='1'----进位

cnt0:="1001"----低位为9

elsif cnt0<"1001" then----小于9时

cnt0:=cnt0+1----计数

else

cnt0:="0000"

if cnt1<"0101" then----高位小于5时

cnt1:=cnt1+1

else

cnt1:="0000"

co<='0'

end if

end if

end if

sec1<=cnt1

sec0<=cnt0

end process

end SEC

分模块程序清单

library ieee

use ieee.std_logic_1164.all

use ieee.std_logic_unsigned.all

entity MINUTE is

port(clk,en:in std_logic

min1,min0:out std_logic_vector(3 downto 0)

co:out std_logic)

end MINUTE

architecture MIN of MINUTE is

begin

process(clk)

variable cnt1,cnt0:std_logic_vector(3 downto 0)

begin

if clk'event and clk='1' then

if en='1' then

if cnt1="0101" and cnt0="1000" then

co<='1'

cnt0:="1001"

elsif cnt0<"1001" then

cnt0:=cnt0+1

else

cnt0:="0000"

if cnt1<"0101" then

cnt1:=cnt1+1

else

cnt1:="0000"

co<='0'

end if

end if

end if

end if

min1<=cnt1

min0<=cnt0

end process

end MIN

时模块程序清单

library ieee

use ieee.std_logic_1164.all

use ieee.std_logic_unsigned.all

entity HOUR is

port(clk,en:in std_logic----输入时钟/高电平有效的使能信号

h1,h0:out std_logic_vector(3 downto 0))----时高位/低位

end HOUR

architecture hour_arc of HOUR is

begin

process(clk)

variable cnt1,cnt0:std_logic_vector(3 downto 0)----记数

begin

if clk'event and clk='1' then---上升沿触发

if en='1' then---同时“使能”为1

if cnt1="0010" and cnt0="0011" then

cnt1:="0000"----高位/低位同时为0时

cnt0:="0000"

elsif cnt0<"1001" then----低位小于9时,低位记数累加

cnt0:=cnt0+1

else

cnt0:="0000"

cnt1:=cnt1+1-----高位记数累加

end if

end if

end if

h1<=cnt1

h0<=cnt0

end process

end hour_arc

动态扫描模块

library ieee

use ieee.std_logic_1164.all

use ieee.std_logic_unsigned.all

use ieee.std_logic_arith.all

entity SELTIME is

port(

clk:in std_logic------扫描时钟

secm1,secm0,sec1,sec0,min1,min0,h1,h0:in std_logic_vector(3 downto 0)-----分别为秒个位/时位;分个位/

daout:out std_logic_vector(3 downto 0)----------------输出

sel:out std_logic_vector(2 downto 0))-----位选信号

end SELTIME

architecture fun of SELTIME is

signal count:std_logic_vector(2 downto 0)----计数信号

begin

sel<=count

process(clk)

begin

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

if(count>="111") then

count<="000"

else

count<=count+1

end if

end if

case count is

when"111"=>daout<= secm0----秒个位

when"110"=>daout<= secm1----秒十位

when"101"=>daout<= sec0----分个位

when"100"=>daout<= sec1----分十位

when"011"=>daout<=min0 ----时个位

when"010"=>daout<=min1----时十位

when"001"=>daout<=h0

when others =>daout<=h1

end case

end process

end fun

报时模块

library ieee

use ieee.std_logic_1164.all

entity ALERT is

port(m1,m0,s1,s0:in std_logic_vector(3 downto 0)------输入秒、分高/低位信号

clk:in std_logic------高频声控制

q500,qlk:out std_logic)----低频声控制

end ALERT

architecture sss_arc of ALERT is

begin

process(clk)

begin

if clk'event and clk='1' then

if m1="0101" and m0="1001" and s1="0101" then----当秒高位为5,低位为9时且分高位为5

if s0="0001" or s0="0011" or s0="0101" or s0="0111" then---当分的低位为1或3或5或7时

q500<='1'----低频输出为1

else

q500<='0'----否则输出为0

end if

end if

if m1="0101" and m0="1001" and s1="0101" and s0="1001" then---当秒高位为5,低位为9时且分高位为5,----分低位为9时,也就是“59分59秒”的时候“报时”

qlk<='1'-----高频输出为1

else

qlk<='0'

end if

end if

end process

end sss_arc

显示模块

library ieee

use ieee.std_logic_1164.all

entity DISPLAY is

port(d:in std_logic_vector(3 downto 0)----连接seltime扫描部分d信号

q:out std_logic_vector(6 downto 0))----输出段选信号(电平)

end DISPLAY

architecture disp_are of DISPLAY is

begin

process(d)

begin

case d is

when"0000" =>q<="0111111"--显示0

when"0001" =>q<="0000110"--显示1

when"0010" =>q<="1011011"--显示2

when"0011" =>q<="1001111"--显示3

when"0100" =>q<="1100110"--显示4

when"0101" =>q<="1101101"--显示5

when"0110" =>q<="1111101"--显示6

when"0111" =>q<="0100111"--显示7

when"1000" =>q<="1111111"--显示8

when others =>q<="1101111"--显示9

end case

end process

end disp_are

顶层文件(原理图输入)

********************************************************************

数字钟设计模块与程序(不含秒表)

*********************************************************************

1.分频模块(原理图输入)

2. 秒模块程序

library ieee

use ieee.std_logic_1164.all

use ieee.std_logic_unsigned.all

entity SECOND is

port(clk,clr:in std_logic

sec1,sec0:out std_logic_vector(3 downto 0)

co:out std_logic)

end SECOND

architecture SEC of SECOND is

begin

process(clk,clr)

variable cnt1,cnt0:std_logic_vector(3 downto 0)

begin

if clr='1' then

cnt1:="0000"

cnt0:="0000"

elsif clk'event and clk='1' then

if cnt1="0101" and cnt0="1000" then

co<='1'

cnt0:="1001"

elsif cnt0<"1001" then

cnt0:=cnt0+1

else

cnt0:="0000"

if cnt1<"0101" then

cnt1:=cnt1+1

else

cnt1:="0000"

co<='0'

end if

end if

end if

sec1<=cnt1

sec0<=cnt0

end process

end SEC

3.分模块程序

library ieee

use ieee.std_logic_1164.all

use ieee.std_logic_unsigned.all

entity MINUTE is

port(clk,en:in std_logic

min1,min0:out std_logic_vector(3 downto 0)

co:out std_logic)

end MINUTE

architecture MIN of MINUTE is

begin

process(clk)

variable cnt1,cnt0:std_logic_vector(3 downto 0)

begin

if clk'event and clk='1' then

if en='1' then

if cnt1="0101" and cnt0="1000" then

co<='1'

cnt0:="1001"

elsif cnt0<"1001" then

cnt0:=cnt0+1

else

cnt0:="0000"

if cnt1<"0101" then

cnt1:=cnt1+1

else

cnt1:="0000"

co<='0'

end if

end if

end if

end if

min1<=cnt1

min0<=cnt0

end process

end MIN

4.时模块程序

library ieee

use ieee.std_logic_1164.all

use ieee.std_logic_unsigned.all

entity HOUR is

port(clk,en:in std_logic

h1,h0:out std_logic_vector(3 downto 0))

end HOUR

architecture hour_arc of HOUR is

begin

process(clk)

variable cnt1,cnt0:std_logic_vector(3 downto 0)

begin

if clk'event and clk='1' then

if en='1' then

if cnt1="0010" and cnt0="0011" then

cnt1:="0000"

cnt0:="0000"

elsif cnt0<"1001" then

cnt0:=cnt0+1

end if

end if

end if

h1<=cnt1

h0<=cnt0

end process

end hour_arc

5.扫描模块程序

library ieee

use ieee.std_logic_1164.all

use ieee.std_logic_unsigned.all

use ieee.std_logic_arith.all

entity SELTIME is

port(

clk:in std_logic

sec1,sec0,min1,min0,h1,h0:in std_logic_vector(3 downto 0)

daout:out std_logic_vector(3 downto 0)

sel:out std_logic_vector(2 downto 0))

end SELTIME

architecture fun of SELTIME is

signal count:std_logic_vector(2 downto 0)

begin

sel<=count

process(clk)

begin

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

if(count>="101") then

count<="000"

else

count<=count+1

end if

end if

case count is

when"000"=>daout<= sec0

when"001"=>daout<= sec1

when"010"=>daout<= min0

when"011"=>daout<= min1

when"100"=>daout<=h0

when others =>daout<=h1

end case

end process

end fun

6.显示模块程序

library ieee

use ieee.std_logic_1164.all

entity DISPLAY is

port(d:in std_logic_vector(3 downto 0)

q:out std_logic_vector(6 downto 0))

end DISPLAY

architecture disp_are of DISPLAY is

begin

process(d)

begin

case d is

when"0000" =>q<="0111111"

when"0001" =>q<="0000110"

when"0010" =>q<="1011011"

when"0011" =>q<="1001111"

when"0100" =>q<="1100110"

when"0101" =>q<="1101101"

when"0110" =>q<="1111101"

when"0111" =>q<="0100111"

when"1000" =>q<="1111111"

when others =>q<="1101111"

end case

end process

end disp_are

7.定时闹钟模块程序

library ieee

use ieee.std_logic_1164.all

entity ALERT is

port(m1,m0,s1,s0:in std_logic_vector(3 downto 0)

clk:in std_logic

q500,qlk:out std_logic)

end ALERT

architecture sss_arc of ALERT is

begin

process(clk)

begin

if clk'event and clk='1' then

if m1="0101" and m0="1001" and s1="0101" then

if s0="0001" or s0="0011" or s0="0101" or s0="0111" then

q500<='1'

else

q500<='0'

end if

end if

if m1="0101" and m0="1001" and s1="0101" and s0="1001" then

qlk<='1'

else

qlk<='0'

end if

end if

end process

end sss_arc

图11

程序如下:

library IEEE

use IEEE.STD_LOGIC_1164.ALL

use IEEE.STD_LOGIC_ARITH.ALL

use IEEE.STD_LOGIC_UNSIGNED.ALL

entity xuan21 is

Port ( alarm,a,b: in std_logic

y:out std_logic)

end xuan21

architecture one of xuan21 is

begin

process(alarm,a,b)

begin

if alarm='0' then y<=aelse y<=b

end if

end process

end one

仿真波形如下图12:

图12

(2)三位二选一:

模块图如图13。用以进行正常计时时间与闹铃时间显示的选择,alarm输入为按键。当alarm按键未曾按下时二选一选择器会选择输出显示正常的计时结果,否则当alarm按键按下时选择器将选择输出显示闹铃时间显示。

图13

程序如下:

library IEEE

use IEEE.STD_LOGIC_1164.ALL

use IEEE.STD_LOGIC_ARITH.ALL

use IEEE.STD_LOGIC_UNSIGNED.ALL

entity x213 is

Port ( alarm : in std_logic

y:out std_logic_vector(3 downto 0)

a,b: in std_logic_vector(3 downto 0))

end x213

architecture one of x213 is

begin

process(alarm,a,b)

begin

if alarm='0' then y<=aelse y<=b

end if

end process

end one

仿真结果如下图14:

图14

8、整点报时及闹时:

模块图如图15。在59分51秒、53秒、55秒、57秒给扬声器赋以低音512Hz信号,在59分59秒给扬声器赋以高音1024Hz信号,音响持续1秒钟,在1024Hz音响结束时刻为整点。当系统时间与闹铃时间相同时给扬声器赋以高音1024Hz信号。闹时时间为一分钟。

图15

程序如下:

library IEEE

use IEEE.STD_LOGIC_1164.ALL

use IEEE.STD_LOGIC_ARITH.ALL

use IEEE.STD_LOGIC_UNSIGNED.ALL

entity voice is

Port ( hou1,huo0,min1,min0,sec1,sec0,hh,hl,mh,ml: std_logic_vector(3 downto 0)

in_1000,in_500:in std_logic

q : out std_logic)

end voice

architecture one of voice is

begin

process(min1,min0,sec1,sec0)

begin

if min1="0101" and min0="1001" and sec1="0101" then

if sec0="0001" or sec0="0011" or sec0="0101" or sec0="0111"

then q<=in_500

elsif sec1="0101" and sec0="1001" then q<=in_1000

else q<='0'

end if

else q<='0'

end if

if min1=mh and min0=ml and hou1=hh and huo0=hl then

q<=in_1000

end if

end process

end one

仿真波形如下图16

图16

9、顶层原理图:

三、感想

通过这次设计,既复习了以前所学的知识,也进一步加深了对EDA的了解,让我对它有了更加浓厚的兴趣。特别是当每一个子模块编写调试成功时,心里特别的开心。但是在画顶层原理图时,遇到了不少问题,最大的问题就是根本没有把各个模块的VHD文件以及生成的器件都全部放在顶层文件的文件夹内,还有就是程序设计的时候考虑的不够全面,没有联系着各个模式以及实验板的情况来编写程序,以至于多考虑编写了译码电路而浪费了很多时间。在波形仿真时,也遇到了一点困难,想要的结果不能在波形上得到正确的显示

:在分频模块中,设定输入的时钟信号后,却只有二分频的结果,其余三个分频始终没反应。后来,在数十次的调试之后,才发现是因为规定的信号量范围太大且信号的初始值随机,从而不能得到所要的结果。还有的仿真图根本就不出波形,怎么调节都不管用,后来才知道原来是路径不正确,路径中不可以有汉字。真是细节决定成败啊!总的来说,这次设计的数字钟还是比较成功的,有点小小的成就感,终于觉得平时所学的知识有了实用的价值,达到了理论与实际相结合的目的,不仅学到了不少知识,而且锻炼了自己的能力,使自己对以后的路有了更加清楚的认识,同时,对未来有了更多的信心。

四、参考资料:

1、潘松,王国栋,VHDL实用教程〔M〕.成都:电子科技大学出版社,2000.(1)

2、崔建明主编,电工电子EDA仿真技术北京:高等教育出版社,2004

3、李衍编著,EDA技术入门与提高王行西安:西安电子科技大学出版社,2005

4、侯继红,李向东主编,EDA实用技术教程北京:中国电力出版社,2004

5、沈明山编著,EDA技术及可编程器件应用实训北京:科学出版社,2004

6、侯伯亨等,VHDL硬件描述语言与数字逻辑电路设计西安: 西安电子科技大学出版社,1997

7、辛春艳编著,VHDL硬件描述语言北京:国防工业出版社,2002 就这些


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7783772.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存