OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 6117|回复: 0

手把手教你学FPGA设计-秒表功能

[复制链接]

31

主题

36

帖子

0

精华

初级会员

Rank: 2

积分
142
金钱
142
注册时间
2018-7-19
在线时间
8 小时
发表于 2018-9-30 11:11:06 | 显示全部楼层 |阅读模式
秒表功能
一、项目背景
同上一个项目。
二、设计目标
开发板或者模块是有 8 位数码管,本次设计需要使用1个数码管,即数码管0,实现类似于秒表的功能,具体要求如下:
复位后,数码管0显示数字0并持续1秒;然后显示数字1并持续2秒;然后显示数字2并持续3秒;以此类推,最后是显示数字9并持续10秒。然后再次循环
上板效果图如下图所示。
file:///C:/Users/pan/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpgfile:///C:/Users/pan/AppData/Local/Temp/msohtmlclip1/01/clip_image004.jpgfile:///C:/Users/pan/AppData/Local/Temp/msohtmlclip1/01/clip_image006.jpgfile:///C:/Users/pan/AppData/Local/Temp/msohtmlclip1/01/clip_image008.jpg
上板的演示效果,请登陆网址查看:www.mdy-edu.com/xxxx
三、模块设计
我们要实现的功能,概括起来就是控制8个数码管,其中数码管0亮,其他数码管不亮。并让数码管0显示不同的数字。
要控制8个数码管,就需要控制位选信号,即FPGA要输出一个8位的位选信号,设为seg_sel,其中seg_sel[0]对应数码管0seg_sel[1]对应数码管1,以此类推,seg_sel[7]对应数码管7
要显示不同的数字,就需要控制段选信号,不需要用到DP,一共有7根线,即FPGA要输出一个7位的段选信号,设为seg_mentseg_ment[6]~segm_ment[0]分别对应数码管的abcdefg(注意对应顺序)。
我们还需要时钟信号和复位信号来进行工程控制。
综上所述,我们这个工程需要4个信号,时钟clk,复位rst_n,输出的位选信号seg_sel和输出的段选信号seg_ment
  
信号线
  
信号线
FPGA管脚
内部信号
SEG_E
SEG0
Y6
seg_ment[2]
SEG_DP
SEG1
W6
未用到
SEG_G
SEG2
Y7
seg_ment[0]
SEG_F
SEG3
W7
seg_ment[1]
SEG_D
SEG4
P3
seg_ment[3]
SEG_C
SEG5
P4
seg_ment[4]
SEG_B
SEG6
R5
seg_ment[5]
SEG_A
SEG7
T3
seg_ment[6]
DIG1
DIG_EN1
T4
seg_sel[0]
DIG2
DIG_EN2
V4
seg_sel[1]
DIG3
DIG_EN3
V3
seg_sel[2]
DIG4
DIG_EN4
Y3
seg_sel[3]
DIG5
DIG_EN5
Y8
seg_sel[4]
DIG6
DIG_EN6
W8
seg_sel[5]
DIG7
DIG_EN7
W10
seg_sel[6]
DIG8
DIG_EN8
Y10
seg_sel[7]
我们先分析要实现的功能,数码管0显示数字0,翻译成信号就是seg_sel的值为8’b1111_1110seg_ment的值为7’b000_0001。然后数码管0显示数字1,也就是说seg_sel的值为8’b1111_1110seg_ment的值为7’b100_1111。以此类推,数码管0显示数字9,就是seg_sel的值为8’b1111_1110seg_ment的值为7’b000_0100
file:///C:/Users/pan/AppData/Local/Temp/msohtmlclip1/01/clip_image009.png
seg_sel一直为8’hfe,不变化。seg_ment隔一段时间后会变化,而这个时间在不同时期还不相同。把时间信息补充上,得到下面的波形示意图。
file:///C:/Users/pan/AppData/Local/Temp/msohtmlclip1/01/clip_image011.png         上图就是seg_selseg_seg信号的变化波形图。在显示第1个时,seg_sel=8’hfeseg_ment=7’h01并持续1秒;在第2个时,seg_sel=8’hfeseg_ment=7’h4f并持续2秒;以此类推,第8个时,seg_sel=8’hfeseg_ment=7’h04并持续10秒。然后又再次重复。
由波形图可知,我们需要1个计数器用来计算时间,如2秒、3秒等。另外,我们还需要一个计数器,用来计算在第几个阶段中。所以总共需要2个计数器。
本工程的工作时钟是50MHz,即周期为20ns,计数器计数到2_000_000_000/20=100_000_000个,我们就能知道2秒时间到了。以类类推,在第2次时,数到150_000_000个,就知道了3秒时间到。第9次时,数到500_000_000个,就表示10秒时间到。另外,由于该计数器是不停地计数,永远不停止的,可以认为加1条件一直有效,可写成:assignadd_cnt0==1。综上所述,结合变量法,该计数器的代码如下
file:///C:/Users/pan/AppData/Local/Temp/msohtmlclip1/01/clip_image013.jpg
1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
always @(posedge clk or negedge  rst_n)begin
  
     if(!rst_n)begin
  
         cnt0 <= 0;
  
     end
  
     else if(add_cnt0)begin
  
         if(end_cnt0)
  
            cnt0 <= 0;
  
         else
  
            cnt0 <= cnt0 + 1;
  
     end
  
end
  
  
assign add_cnt0 = 1 ;
  
assign end_cnt0 = add_cnt0 &&  cnt0== x-1 ;
第二个计数器用于表示第几个,很自然可以看到,每个阶段完成后,该计数器加1,因此加1条件可为end_cnt0。该计数器一共要数10次。所以代码为:
file:///C:/Users/pan/AppData/Local/Temp/msohtmlclip1/01/clip_image015.jpg
1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
always @(posedge clk or negedge  rst_n)begin
  
     if(!rst_n)begin
  
         cnt1 <= 0;
  
     end
  
     else if(add_cnt1)begin
  
         if(end_cnt1)
  
             cnt1 <= 0;
  
         else
  
            cnt1 <= cnt1 + 1;
  
     end
  
end
  
  
assign add_cnt1 = end_cnt0;
  
assign end_cnt1 = add_cnt1 &&  cnt1== 10-1 ;
接下来设计seg_sel。该信号一直为8’hfe,所以代码直接写成如下:
file:///C:/Users/pan/AppData/Local/Temp/msohtmlclip1/01/clip_image016.png
1
assign seg_sel = 8'hfe ;
我们来思考输出信号seg_ment的变化。概括起来,在第1次的时候输出值为7’h01;在第2次的时候输出值为7’h4f;以此类推,在第8次的时候输出值为7’h0f。我们用信号cnt1来代替第几次,也就是:当cnt1==0的时候,输出值为7’h01;在cnt1==1的时候输出值为7’h4f;以此类推,在cnt1==9的时候输出值为7’h04。再进一步翻译成代码,就变成如下:
file:///C:/Users/pan/AppData/Local/Temp/msohtmlclip1/01/clip_image017.png
1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
always   @(posedge clk or negedge rst_n)begin
  
     if(rst_n==1'b0)begin
  
         seg_ment <= 7'h01;
  
     end
  
     else if(cnt1==0)begin
  
         seg_ment <= 7'h01;
  
     end
  
     else if(cnt1==1)begin
  
         seg_ment <= 7'h4f;
  
     end
  
     else if(cnt1==2)begin
  
         seg_ment <= 7'h12;
  
     end
  
     else if(cnt1==3)begin
  
         seg_ment <= 7'h06;
  
     end
  
     else if(cnt1==4)begin
  
         seg_ment <= 7'h4c;
  
     end
  
     else if(cnt1==5)begin
  
         seg_ment <= 7'h24;
  
     end
  
     else if(cnt1==6)begin
  
         seg_ment <= 7'h20;
  
    end
  
     else if(cnt1==7)begin
  
         seg_ment <= 7'h0f;
  
     end
  
     else if(cnt1==8)begin
  
         seg_ment <= 7'h00;
  
     end
  
     else if(cnt1==9)begin
  
         seg_ment <= 7'h04;
  
     end
  
end
然后,用组合逻辑把x的值确定下来。
file:///C:/Users/pan/AppData/Local/Temp/msohtmlclip1/01/clip_image018.png
file:///C:/Users/pan/AppData/Local/Temp/msohtmlclip1/01/clip_image019.png
1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
always   @(*)begin
  
     if(cnt1==0)begin
  
         x = 50_000_000;
  
     end
  
     else if(cnt1==1)begin
  
         x = 100_000_000;
  
     end
  
     else if(cnt1==2)begin
  
         x = 150_000_000;
  
     end
  
     else if(cnt1==3)begin
  
         x = 200_000_000;
  
     end
  
     else if(cnt1==4)begin
  
         x = 250_000_000;
  
     end
  
     else if(cnt1==5)begin
  
         x = 300_000_000;
  
     end
  
     else if(cnt1==6)begin
  
         x = 350_000_000;
  
     end
  
     else if(cnt1==7)begin
  
         x = 400_000_000;
  
     end
  
     else if(cnt1==8)begin
  
         x = 450_000_000;
  
     end
  
     else if(cnt1==9)begin
  
         x = 500_000_000;
  
     end
  
end


手把手教你学FPGA设计-秒表功能.pdf

1.44 MB, 下载次数: 309

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-7-5 06:22

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表