资源描述
邵阳学院课程设计
目 录
1 概述1
1。1数字时钟的工作原理1
1。2设计任务1
2 系统总体方案设计 2
3 VHDL模块电路设计 3
3。1模块实现 3
3。1。1分频模块pinlv3
3.1.2按键去抖动模块qudou5
3.1.3按键控制模块self16
3。1.4秒、分六十进制模块cantsixty7
3.1。5时计数模块hourtwenty 9
3。1。6秒、分、时组合后的模块9
3。1。7数码管显示模块10
3.2数字时钟的顶层设计原理图 13
3。3系统仿真与调试 14
结束语 16
参考文献 17
致谢 18
附录源程序代码 19
1 概述
1。1数字时钟的工作原理
数字钟电路的基本结构由两个60进制计数器和一个24进制计数器组成,分别对秒、分、小时进行计时,当计时到23时59分59秒时,再来一个计数脉冲,则计数器清零,重新开始计时。秒计数器的计数时钟CLK为1Hz的标准信号,可以由晶振产生的50MHz信号通过分频得到.当数字钟处于计时状态时,秒计数器的进位输出信号作为分钟计数器的计数信号,分钟计数器的进位输出信号又作为小时计数器的计数信号,每一秒钟发出一个中断给CPU,CPU采用NIOS,它响应中断,并读出小时、分、秒等信息.CPU对读出的数据译码,使之动态显示在数码管上.
1。2 设计任务
设计一个基于VHDL的数字时钟,具体功能要求如下:
1.在七段数码管上具有时——分——秒的依次显示。
2.时、分、秒的个位记满十向高位进一,分、秒的十位记满五向高位进一,小
时按24进制计数,分、秒按60进制计数。
3.整点报时,当计数到整点时扬声器发出响声。
4.时间设置:可以通过按键手动调节秒和分的数值。此功能中可通过按键实现
整体清零和暂停的功能。
5.LED灯循环显示:在时钟正常计数下,LED灯被依次循环点亮。
2 系统总体方案设计
设计一个基于VHDL的数字时钟,我采用自顶向下分模块的设计。底层为实现个弄能的模块,各模块由vhdl语言编程实现:顶层采用原理图形式调用。其中底层模块包括秒、分、时三个计数器模块、按键去抖动模块、按键控制模块、时钟分频模块、数码管显示模块共7个模块。设计框图如下:
图2.1数字时钟设计框图
由图2.1可以清晰的看到数字钟系统设计中各功能模块间连接关系。系统时钟50MHZ经过分频后产生1秒的时钟信号,1秒的时钟信号作为秒计数模块的输入信号,秒计数模块产生的进位信号作为分计数模块的输入信号,分计数模块的进位信号作为时计数模块的输入信号.秒计数模块、分计数模块、时计数模块的计数输出分别送到显示模块.由于设计中要使用按键进行调节时间,而按键的动作过程中存在产生得脉冲的不稳定问题,所以就牵扯到按键去抖动的问题,对此系统中设置了按键去抖动模块,按键去抖动模块产生稳定的脉冲信号送入按键控制模块,按键控制模块根据按键的动作对秒、分、时进行调节。
3 VHDL模块电路设计
3.1 模块实现
由数字钟的顶层设计原理图可知:系统的外部输入即为系统的时钟信号CLK =50MHZ,系统的外部输出有蜂鸣器信号buzzer,LED显示信号LED[3。.1]和shan(与按键去抖动模块的o3相连),数码管显示信号xianshi[7。。0],数码管位选信号xuanze[7。。0].
下面将对内部功能模块进行详细说明,(本设计共包含5个模块):
3。1.1分频模块pinlv
对系统的时钟50MHZ进行分频,设置不同长度的计数值,当系统时钟clk有变化时计数器开始计数,当计数到某个值时输出一个信号,计数值不同输出信号的周期也就不同,从而实现了对系统时钟进行不同的分频,产生不同频率的信号。
由VHDL语言生成的模块图和程序说明如下:
图3。1分频模块
library ieee;
use ieee.std_logic_1164。all;
use ieee.std_logic_unsigned.all;
entity pinlv is
port( clk:in std_logic;——系统时钟输入端口
clk2ms:out std_logic;
clk500ms:out std_logic;
clk1s:out std_logic);--各频率信号的输出端口
end;
architecture beh of pinlv is
begin
p1:process(clk); -—进程p1
variable count1:integer range 0 to 49999999;
begin
if(clk'event and clk=’1')then count1:=count1+1;——在clk 的上升沿计数
if count1<=24999999 then clk1s<=’0';
elsif count1〈=49999999 then clk1s〈='1';
else count1:=0;——产生周期为1s的时钟信号
clk500ms〈=’0';
elsif count3〈=24999999 then clk500ms〈='1’;
else count3:=0;—-产生周期为500ms的时钟信号
end if;
end if;
end process p1;--结束进程p1
p2:process(clk);--进程p2
variable count2:integer range 0 to 99999;
begin
if(clk’event and clk=’1')then count2:=count2+1;——在clk上升沿计数
if count2<=49999 then clk2ms<=’0';
elsif count2<=99999 then clk2ms〈=’1';—-产生周期为2ms的扫描信号
end if;
end if;
end process p2;--结束进程p2
p3:process(clk); -—进程p3
variable count3:integer range 0 to 24999999;
begin
if(clk’event and clk='1’)then count3:=count3+1; ——在clk上升沿计数
if count3〈=12499999 then
end if;
end if;
end process p3;
end beh;
3.1。2按键去抖动模块qudou
本设计用到FPGA开发板上的四个按键,由于按键有反应时间、抖动的问题,可能当按键被按一次时而系统感应到几次,造成误差。所以应该进行按键消抖的处理,让每按一次键系统只感应到一次按键。可以采用软件延时,触发反相器等方式进行消除抖动,本设计中采用软件延时的方式。
由VHDL语言生成的模块图和程序说明如下:
图3。2按键去抖动模块
library ieee;
use ieee。std_logic_1164。all;
use ieee。std_logic_unsigned.all;
entity qudou is
port(clk,k1,k2,k3,k4:in std_logic;
o1,o2,o3,o4:out std_logic);——设置按键输入信号输出端口
end;
architecture beh of qudou is
begin
process(clk,k1,k2,k3,k4)
variable cant1:integer;
variable cant2:integer;
variable cant3:integer;
variable cant4:integer;
begin
if clk'event and clk=’1' then
if k1='1’ then cant1:=0;
end if;-—设置计数初值
if k2=’1' then cant2:=0;
end if; ——设置计数初值
if k3='1’ then cant3:=0;——设置计数初值
end if;
if k4=’1’ then cant4:=0;
end if; -—设置计数初值
if cant1〉2499999 then o1〈=’0’;
else o1〈=’1’;——延时0.5s
end if;
if cant2>2499999 then o2<='0';
else o2〈=’1’; --延时0.5s
end if;
if cant3〉2499999 then o3<=’0';
else o3〈=’1’; —-延时0。5s
end if;
if cant4〉2499999 then o4〈='0’;
else o4〈=’1’; ——延时0.5s
end if;
cant1:=cant1+1; —-加一计数
cant2:=cant2+1; —-加一计数
cant3:=cant3+1; ——加一计数
cant4:=cant4+1; --加一计数
end if;
end process;
end beh;
3。1。3按键控制模块self1
本设计中使用了两个按键进行对时钟的暂停和调秒操作,当ok2按下时时钟暂停,再按ok3则进行秒个位的加一计数,每按一次进行加一处理。当调节好时间后,在按ok2键重新开始计数。
由VHDL语言生成的模块图和程序说明如下:
图3.3按键控制模块
library ieee;
use ieee.std_logic_1164.all;
use ieee。std_logic_unsigned。all;
entity self1 is
port(
c:in std_logic;
ok2:in std_logic;
ok3:in std_logic;
ck:out std_logic);
end ;——设置端口
architecture bea of self1 is
signal m:std_logic;
signal t:std_logic;
begin
p1:process(ok2,ok3,c); —-ok2和ok3触发进程
begin
if ok2'event and ok2=’0’ then m〈=not m;-—由ok2 的动作产生m的电平信号
end if;
if m=’1’ then ck<=not(ok3);——把按键ok3的脉冲信号给输出
else ck<=c;-—否则把正常计数时钟给输出
end if;
end process p1;——结束进程
end bea;
3.1.4秒、分六十进制模块cantsixty
本设中秒、分的六十进制是由个位的十进制和十位的六进制进行组合实现的。当个位记到9时自动向高位进一,同时个位自动清零。当十位记到5并且个位记到9时,自动产生一个进位脉冲,同时个位和十位分别从零开始重新计数。
由VHDL语言生成的模块图和程序说明如下:
图3。4六十进制模块
library ieee;
use ieee。std_logic_1164.all;
use ieee。std_logic_unsigned。all;
entity cantsixty is
port(clk:in std_logic;
reset:in std_logic;
out1:out std_logic_vector(3 downto 0);
out2:out std_logic_vector(3 downto 0);
c:out std_logic);
end;
architecture beh of cantsixty is
signal ss1,ss2:std_logic_vector( 3 downto 0);
begin
p1:process(clk,reset)
begin
if(reset='0')then ss1〈=”0000”;ss2〈="0000”;
elsif(clk’event and clk='1’)then
if ss1=”1001” and ss2="0101" then c〈=’1’;-—当计数到59时产生进位信号
else c〈='0’;-—否则不产生
end if;
if ss1="1001” then ss1〈=”0000";
if ss2="0101” then ss2〈=”0000";
else ss2<=ss2+1;
end if;
else ss1〈=ss1+1;-—计数过程
end if;
end if;
end process p1;—-结束进程
out1〈=ss1;out2〈=ss2;—-把信号送输出
end beh;
3。1。5时计数模块hourtwenty
时计数模块是二十四进制相对复杂一点,因为当十位0或着1时个位需要记到9并产生进位信号,当十位是2时,个位记到3时,就全部从零开始重新计数。即是在十位为不同值时个位两种计数过程.
由VHDL语言生成的模块图和程序说明如下:
图3。5时计数模块
3。1。6秒、分、时组合后的模块
把设计的秒、分、时模块连接起来,再通过仿真验证,各模块间的进位是否正确
连接后的原理图如下
图3.6秒、分、时组合后原理图
3。1。7数码管显示模块
本模块中包含数码管的段选和位选设计,Led灯循环设计,以及整点报时的设计。模块的输入信号有数码管扫描频率clk2ms,秒、分、时各模块的个位和十位输入,以及由分模块向时模块产生的进位脉冲信号。
由VHDL语言生成的模块图和程序说明如下:
图3。7数码管显示原理图
library ieee;
use ieee。std_logic_1164。all;
use ieee.std_logic_unsigned。all;
entity qudong is
port(s1,s2,m1,m2,h1,h2:in std_logic_vector(3 downto 0);
clk2ms: in std_logic;
xiang:in std_logic;
signal sel:std_logic_vector( 2 downto 0);
signal A:std_logic_vector( 3 downto 0);
signal t:std_logic_vector ( 11 downto 0);
signal f:std_logic_vector(1 downto 0);
signal count1:std_logic_vector(1 downto 0);
begin
p1:process(clk2ms)
begin
if clk2ms'event and clk2ms=’1’ then sel〈=sel+1;t〈=t+1;
if t=”110010000000” then t〈=(others=〉’0');
end if;
end if;
f〈=t(11)&t(10);
if f=”01” then led(3)〈=’0’;else led(3)<=’1’;
end if;
if f="10” then led(2)〈=’0';else led(2)〈='1’;
end if;
if f=”11” then led(1)〈=’0'; else led(1)〈=’1’;
end if;-—led的循环显示设计
end process p1;
p2:process(sel,s1,s2,m1,m2,h1,h2)
begin
case sel is
when "000" =〉xuanze<=”11111110”; A<=s1;--秒个位在数码管1上显示
when "001” =〉xuanze<=”11111101"; A〈=s2;—-秒十位在数码管2上显示
when "010” =〉xuanze〈="11111011”; A<=”1010”;——数码管3上显示横杠
when ”011" =〉xuanze<="11110111"; A〈=m1;——分个位在数码管4上显示
when "100" =>xuanze〈=”11101111"; A〈=m2;—-分十位在数码管5上显示
when "101” =〉xuanze〈=”11011111”; A<="1011”;--数码管6上显示横杠
when "110” =>xuanze〈="10111111”; A〈=h1;——时个位在数码管7上显示
when ”111" =〉xuanze〈=”01111111”; A〈=h2;——时十位在数码管8上显示
when others =〉null;
end case;
end process p2;
p3:process(A)
begin
case A is
when ”0000” => xianshi <="11000000”; ——显示0
when ”0001” =〉 xianshi 〈=”11111001"; -—显示1
when ”0010” =〉 xianshi 〈="10100100”; ——显示2
when ”0011” =〉 xianshi <=”10110000"; --显示3
when "0100" =〉 xianshi <=”10011001”; ——显示4
when "0101" =〉 xianshi 〈=”10010010”; ——显示5
when "0110" =〉 xianshi <="10000010”; -—显示6
when ”0111” => xianshi 〈=”11111000”; ——显示7
when ”1000" =〉 xianshi 〈=”10000000”; ——显示8
when ”1001” =〉 xianshi <="10010000”; ——显示9
when ”1010" =〉xianshi 〈=”10111111”; --显示——
when ”1011" =>xianshi 〈="10111111"; ——显示--
when others =〉null; --数码管的段选设计
end case;
end process p3;
P4:process(xiang)
begin
if xiang=’1’ then buzzer〈=’0’; ——当进位信号xiang为1时就把低电平给buzzer让蜂鸣器响
else buzzer<=’1'; —-否则把高电平给buzzer不给蜂鸣器触发信号
end if;
end process p4; --结束进程
end behav;
3.2 数字钟的顶层设计原理图
图3。8数字钟的顶层设计原理图
3。3 系统仿真与调试
将调试好的程序下载到实验板上进行验证,达到了设计的各项功能.时钟准确计数,各模块的进位也正确,当按下实验板上的key1键时系统复位清零,实验板上的key2键可实现系统的暂停和开始,在系统暂停的状态下,按key3键可实现调节秒计数,每按一次计数加一,key4键可实现调节分模块,每按一次计数加一,长按则一直加,当达到整点时,蜂鸣器发声。其中一些模块在Quartus II下的仿真如下:
1). 按键去抖动仿真:
图3.9按键去抖动仿真效果图
由于0.5s太长,在本仿真中设置了很小的一个量10clk,从图中可以看出基本实现了按键去抖动的效果。无论按键怎么抖动,输出总是保持稳态10clk,当下一个触发来了以后,就可以触发单稳态。
2). 六十进制波形仿真:
图3。10六十进制波形仿真图
由上图可见,当1s的时钟信号加入时,个位out1从0到9不断循环,而且当个位out1记到9时产生一个进位信号 使十位out2加一,以此类推就实现了六十进制计数。基本达到了正确计数的理想效果.
3)。 二十四进制波形仿真:
图3.11二十四进制波形仿真图
由上图看出十位为0或1时,个位记到9时,十位才进行加一计数,但当十位为2时,个位记到3时,十位变成了0,个位又从0重新开始计数,这样就实现了二十四进制的计数.从图形的显示波形可知,设计基本达到了正确计数的功能。
4)。 秒、分、时组合后波形仿真:
图3。12秒、分、时组合后仿真波形图
结束语
这个实验带给我的体会很多也很深,我以前没有对数字时钟进行系统的设计,这次独立的设计,我遇到了很多问题,也走了很多弯路,还好最后终于通过自己的努力看到了理想的结果。通过实验,我对EDA技术和FPGA技术有了更进一步的理解,掌握了FPGA的层次化设计电路的方法,掌握了用VHDL语言编写各个功能模块并通过波形确定电路设计是否正确.掌握了下载验到目标器件的过程.
实验中遇到的问题很多,有的是很基础的但我却不知道,例如数码管的扫描频率,刚开始时数码管不显示,我找了很多原因都没想到是扫描频率的问题,浪费了很多时间。还有分频的时候,看过很多分频的电路程序,但那些并不是都可以实现准确的分频,需要通过波形进行验证。还有计数器的设计,我用了很长时间才编写出来,现在看看,也没有那么难了.
总之,我很感谢这次实验可以给我这样的机会,这个实验给了我很对的收获,我相信这会对我以后的学习和工作都有帮助.
参考文献
[1]周立功,SOPC嵌入式系统基础教程,北京航空航天大学出版社,2008。4
[2] 周立功,SOPC嵌入式系统实验教程,北京航空航天大学出版社,2006.7
[3] 张志刚,FPGA与SOPC设计教程—DE实践,西安电子科技大学出版社,2007
[4] 潘松 黄继业,EDA技术实用教程,科学出版社,2006。8
[5] 华清远见嵌入式培训中心,FPGA应用开发入门与典型实例,人民邮电出版社,2008。6
致 谢
非常感谢李老师对我们的细心详细的指导,要不是李老师很仔细的检查我的课程设计,并从中发现我的诸多错误,我现在也没法这么快的把课程设计完成。
李老师为人随和亲切,上课时总是不忘记鼓励我们,老师非常耐心地给我们讲了这次课程设计应该要注意的地方,我们应该用什么心态去看待这次的课程设计,他说对课程设计对于电子科学与技术专业的学生是有很大帮助的,这可以提高我们的动手能力和协同能力,所以李老师要求我们一定要认真对待!老师的鼓励使我认识到以后还要多学习各种电子方面的书籍,多进行操作,提高动手能力和理论水平!在这次课程设计中我也遇到了比较多的问题,不过李老师每次都是不厌其烦给我们批改了,经过李老师的仔细批改,大部分的错误都解决了。老师无微不至的关怀和谆谆的教诲,高深的学术造诣让我获益匪浅,也让我学到了很多的关于课程设计的宝贵的经验,这是一生受益的事情!所以,再一次由衷的的感谢李老师,谢谢!
附录 源程序代码
library ieee;
use ieee.std_logic_1164.all;
use ieee。std_logic_unsigned。all;
entity szz is
port( clk,k1,k2,k3,k4,c1,c2,ok2,ok3,reset1,reset2,clk2ms,xiang:in std_logic;
s1,s2,m1,m2,h1,h2:in std_logic_vector(3 downto 0);
clk2ms,clk500ms,clk1s,o1,o2,o3,o4,ck:out std_logic;
out1,out2,out3,out4:out std_logic_vector(3 downto 0);
xuanze,xianshi:out std_logic_vector(7 downto 0));
end szz
architecture one of szz is
signal m,t1:std_logic;
signal hh1,hh2,ss1,ss2,A:std_logic_vector( 3 downto 0);
signal sel:std_logic_vector( 2 downto 0);
signal t2:std_logic_vector ( 11 downto 0);
signal f,count1:std_logic_vector(1 downto 0);
begin
——-—-—----———--——-———————---—-—————-——————-—-—分频模块pinlv
p1:process(clk); -—进程p1
variable count1:integer range 0 to 49999999;
begin
if(clk’event and clk=’1')then count1:=count1+1;——在clk 的上升沿计数
if count1<=24999999 then clk1s〈='0’;
elsif count1〈=49999999 then clk1s〈=’1';
else count1:=0;-—产生周期为1s的时钟信号
clk500ms〈=’0’;
elsif count3<=24999999 then clk500ms〈='1’;
else count3:=0;——产生周期为500ms的时钟信号
end if;
end if;
end process p1;--结束进程p1
p2:process(clk);-—进程p2
variable count2:integer range 0 to 99999;
begin
if(clk’event and clk=’1’)then count2:=count2+1;-—在clk上升沿计数
if count2<=49999 then clk2ms〈=’0’;
elsif count2〈=99999 then clk2ms〈='1';-—产生周期为2ms的扫描信号
end if;
end if;
end process p2;——结束进程p2
p3:process(clk); ——进程p3
variable count3:integer range 0 to 24999999;
begin
if(clk'event and clk='1’)then count3:=count3+1; —-在clk上升沿计数
if count3〈=12499999 then
end if;
end if;
end process p3;
-—-———————-——-——————-—-—————————-——-———--———-—按键去抖动模块qudou
p4:process(clk,k1,k2,k3,k4)
variable cant1:integer;
variable cant2:integer;
variable cant3:integer;
variable cant4:integer;
begin
if clk’event and clk=’1' then
if k1=’1' then cant1:=0;
end if;——设置计数初值
if k2=’1’ then cant2:=0;
end if; ——设置计数初值
if k3='1’ then cant3:=0;—-设置计数初值
end if;
if k4=’1' then cant4:=0;
end if; ——设置计数初值
if cant1>2499999 then o1<='0';
else o1<=’1';——延时0.5s
end if;
if cant2〉2499999 then o2〈='0’;
else o2〈=’1'; —-延时0。5s
end if;
if cant3〉2499999 then o3〈='0’;
else o3<='1'; ——延时0。5s
end if;
if cant4>2499999 then o4<=’0’;
else o4<='1'; ——延时0.5s
end if;
cant1:=cant1+1; ——加一计数
cant2:=cant2+1; ——加一计数
cant3:=cant3+1; -—加一计数
cant4:=cant4+1; --加一计数
end if;
end process p4;
———--————---———---—-——-—————--—————-—————-—-——按键控制模块self1
p5:process(ok2,ok3,c1); -—ok2和ok3触发进程
begin
if ok2’event and ok2='0' then m<=not m;——由ok2 的动作产生m的电平信号
end if;
if m=’1' then ck〈=not(ok3);——把按键ok3的脉冲信号给输出
else ck〈=c1;—-否则把正常计数时钟给输出
end if;
end process p5;——结束进程
—-—--—------————-———-—————--------——————————-—六十进制模块cantsixty
p6:process(clk,reset1)
begin
if(reset1=’0’)then ss1〈="0000";ss2〈=”0000";
elsif(clk’event and clk=’1')then
if (ss1=”1001" and ss2="0101”) then c2〈=’1';——当计数到59时产生进位信号
else c2<=’0’;-—否则不产生
end if;
if ss1=”1001” then ss1〈="0000”;
if ss2=”0101” then ss2〈=”0000”;
else ss2<=ss2+1;
end if;
else ss1〈=ss1+1;--计数过程
end if;
end if;
end process p6;—-结束进程
out1<=ss1;out2〈=ss2;—-把信号送输出
——-———--——-——-—-———-———-——--—-——-———--—--——-—-二十四进制模块hourtwenty
p7:process(clk,reset2)
begin
if(reset2=’0')then hh1〈="0000";hh2<=”0000";
elsif(clk’event and clk=’1’)then
if (hh1=”0011” and hh2=”0010”)and(ss1=”1001” and ss2="0101”) then
hh2<="0000";
end if;
if (hh1=”1001”)and(ss2=”0101” and ss1=”1001”) then
hh2<=hh2+1;
end if;
end if;
end process p7;
p8:process(clk,reset2)
begin
if(reset2=’0’)then hh1〈="0000”;hh2〈=”0000”;
elsif(clk'event and clk=’1')then
if (hh1="0011" and hh2="0010")and(ss1=”1001" and ss2="0101”) then
hh1〈="0000";
end if;
if (hh1=”1001")and(ss1=”1001" and ss2=”0101”) then
if (hh1=”1001”) then
hh1〈=”0000”;
else hh1<=hh1+1;
end if;
end if;
end if;
end process p8;
out3<=hh1;out4〈=hh2;-—把信号送输出
———---—-————---—————-—————--——————--—-———-————数码管显示模块qudong
p9:process(clk2ms)
begin
if clk2ms’event and clk2ms='1’ then sel〈=sel+1;t2<=t2+1;
if t2="110010000000” then t2〈=(others=>’0’);
end if;
end if;
f<=t2(11)&t2(10);
if f=”01” then led(3)<='0';else led(3)〈='1’;
end if;
if f=”10" then led(2)<='0';else led(2)〈=’1’;
end if;
if f=”11” then led(1)〈='0’; else led(1)〈='1';
end if;-—led的循环显示设计
end process p9;
p10:process(sel,s1,s2,m1,m2,h1,h2)
begin
case sel is
when "000" =〉xuanze〈="11111110”; A〈=s1;-—秒个位在数码管1上显示
when ”001" =〉xuanze<=”11111101"; A〈=s2;—-秒十位在数码管2上显示
when ”010” =〉xuanze〈=”11111011”; A<=”1010";-—数码管3上显示横杠
when ”011” =〉xuanze<="11110111”; A<=m1;--分个位在数码管4上显示
when "100" =>xuanze〈="11101111”; A<=m2;-—分十位在数码管5上显示
when "101” =〉xuanze<=”11011111”; A〈=”1011”;-—数码管6上显示横杠
when "110” =>xuanze<=”10111111"; A〈=h1;—-时个位在数码管7上显示
when "111” =>xuanze〈="01111111”; A〈=h2;——时十位在数码管8上显示
when others =>null;
end case;
end process p10;
p11:process(A)
begin
case A is
when ”
展开阅读全文