赞
踩
数码管分七段数码管和八段数码管。七段和八段的区别在于,是否包括小数点DP(Digital Point)。本实验中使用的是数码管是8段数码管,每段是由led组成。通过控制每段led的亮灭,来控制数码管显示不同的数字和字母。
如图4中(b)所示,a—dp为输入端,全部在二极管的正极,二极管的负极共同接地。只有当a—dp输入为高电平的时候,二极管才导通,然后对应的段发亮。
如图4中(c)所示,a—dp为输入端,全部在二极管的负极,二极管的正极极共同接+5v(高电平)。只有当a—dp输入为低电平的时候,二极管才导通,然后对应的段发亮。
要显示不同的数字或者字母,就要选择点亮对应的led段。图5中对应的是cyclone IV开发板上数码管的真值表,可以通过查找该表来显示我们想要的数字或者字母。
任务要求实现六个数码管依次显示0-f,间隔时间0.5s
所用芯片为:EP4CE6F17C8
建立下图所示文件夹,其中prj为项目文件夹,rtl为源码文件夹,tcl为引脚绑定代码文件夹
在rtl文件夹中新建.v文件,并进行代码编写。
状态机实现
代码分析:
module seg_dy2 ( input wire clk , input wire rst_n , output reg [7:0] seg , //段选信号 选择数码管显示的是8段中的哪一段 output reg [5:0] sel //位选信号 选择显示的数码管是哪一位 ); parameter MAX0_5 = 25'd25_000_000; //每隔0.5s数码管显示更新 parameter ZERO = 8'b1100_0000, ONE = 8'b1111_1001, TWO = 8'b1010_0100, THREE = 8'b1011_0000, FOUR = 8'b1001_1001, FIVE = 8'b1001_0010, SIX = 8'b1000_0010, SEVEN = 8'b1111_1000, EIGHT = 8'b1000_0000, NINE = 8'b1001_0000, A = 8'b1000_1000, B = 8'b1000_0011, C = 8'b1100_0110, D = 8'b1010_0001, E = 8'b1000_0110, F = 8'b1000_1110; reg [24:0] cnt_0_5s ; reg [1:0] flag ; reg [3:0] cnt_16 ; //0.5s计数器 always @(posedge clk or negedge rst_n) begin if(!rst_n)begin cnt_0_5s <= 1'b0; flag <= 1'b0; end else if(cnt_0_5s == MAX0_5 - 1'b1)begin cnt_0_5s <= 1'b0; flag <= 1'b1; end else begin cnt_0_5s <= cnt_0_5s + 1'b1; flag <= 1'b0; end end //16计数器 always @(posedge clk or negedge rst_n)begin if(!rst_n)begin cnt_16 <= 4'b0000; end else if(cnt_16 == 4'd15 && flag)begin cnt_16 <= 4'b0000; end else if(flag)begin cnt_16 <= cnt_16 + 1'b1; end else begin cnt_16 <= cnt_16; end end //状态机 reg [2:0] cstate; reg [2:0] nstate; parameter IDLE = 3'd0, P1 = 3'd1, P2 = 3'd2, P3 = 3'd3, P4 = 3'd4, P5 = 3'd5; //第一段 时序逻辑 always @(posedge clk or negedge rst_n)begin if(!rst_n)begin cstate <= 1'b0; end else begin cstate <= nstate; end end //第二段 组合逻辑 always @(*)begin case(cstate) IDLE: begin if(flag)begin nstate = P1; end else begin nstate =cstate; end end P1: begin if(flag)begin nstate = P2; end else begin nstate =cstate; end end P2: begin if(flag)begin nstate = P3; end else begin nstate =cstate; end end P3: begin if(flag)begin nstate = P4; end else begin nstate =cstate; end end P4: begin if(flag)begin nstate = P5; end else begin nstate =cstate; end end P5: begin if(flag)begin nstate = IDLE; end else begin nstate =cstate; end end default: nstate = IDLE; endcase end //第三段 时序逻辑 always @(posedge clk or negedge rst_n)begin if(!rst_n)begin sel <= 6'b111_110; end else begin case(cstate) 3'd0: begin sel = 6'b111_110; end 3'd1: begin sel = 6'b111_101; end 3'd2: begin sel = 6'b111_011; end 3'd3: begin sel = 6'b110_111; end 3'd4: begin sel = 6'b101_111; end 3'd5: begin sel = 6'b011_111; end endcase end end always @(posedge clk or negedge rst_n) begin if(!rst_n)begin seg <= 8'b1111_1111; end else begin case(cnt_16) 4'd0 : seg <= ZERO ; 4'd1 : seg <= ONE ; 4'd2 : seg <= TWO ; 4'd3 : seg <= THREE; 4'd4 : seg <= FOUR ; 4'd5 : seg <= FIVE ; 4'd6 : seg <= SIX ; 4'd7 : seg <= SEVEN; 4'd8 : seg <= EIGHT; 4'd9 : seg <= NINE ; 4'd10 : seg <= A ; 4'd11 : seg <= B ; 4'd12 : seg <= C ; 4'd13 : seg <= D ; 4'd14 : seg <= E ; 4'd15 : seg <= F ; default : seg <= ZERO ; endcase end end endmodule
非状态机实现
(增添代码时博主正在吃夜宵,代码分析后续再加,实际上和状态机没多大差别,看个人喜好选择即可)
module seg_dy22( input wire clk , input wire rst_n , output reg [7:0] seg , //段选信号 选择数码管显示的是8段中的哪一段 output reg [5:0] sel //位选信号 选择显示的数码管是哪一位 ); parameter MAX0_5 = 25'd25_000_000; //每隔0.5s数码管显示更新 parameter ZERO = 8'b1100_0000, ONE = 8'b1111_1001, TWO = 8'b1010_0100, THREE = 8'b1011_0000, FOUR = 8'b1001_1001, FIVE = 8'b1001_0010, SIX = 8'b1000_0010, SEVEN = 8'b1111_1000, EIGHT = 8'b1000_0000, NINE = 8'b1001_0000, A = 8'b1000_1000, B = 8'b1000_0011, C = 8'b1100_0110, D = 8'b1010_0001, E = 8'b1000_0110, F = 8'b1000_1110; reg [24:0] cnt_0_5s ; reg [3:0] cnt_num ; reg [2:0] cnt_seg ; always @(posedge clk or negedge rst_n) begin if(!rst_n)begin cnt_0_5s <= 1'b0; end else if(cnt_0_5s == MAX0_5 - 1'b1)begin cnt_0_5s <= 1'b0; end else begin cnt_0_5s <= cnt_0_5s + 1'b1; end end always @(posedge clk or negedge rst_n)begin if(!rst_n)begin cnt_num <= 1'b0; end else if(cnt_num == 4'd15 && cnt_0_5s == MAX0_5 - 1'b1)begin cnt_num <= 1'b0; end else if(cnt_0_5s == MAX0_5 - 1'b1)begin cnt_num <= cnt_num + 1'b1; end else begin cnt_num = cnt_num; end end always @(posedge clk or negedge rst_n)begin if(!rst_n)begin cnt_seg <= 3'd0; end else if(cnt_seg == 3'd5 && cnt_0_5s == MAX0_5 - 1'b1)begin cnt_seg <= 3'd0; end else if(cnt_0_5s == MAX0_5 - 1'b1)begin cnt_seg <= cnt_seg + 1'b1; end else begin cnt_seg <= cnt_seg; end end always @(posedge clk or negedge rst_n)begin if(!rst_n)begin sel <= 6'b111_110; end else if(cnt_seg == 3'd0 )begin sel <= 6'b111_101; end else if(cnt_seg == 3'd1 )begin sel <= 6'b111_011; end else if(cnt_seg == 3'd2 )begin sel <= 6'b110_111; end else if(cnt_seg == 3'd3 )begin sel <= 6'b101_111; end else if(cnt_seg == 3'd4 )begin sel <= 6'b011_111; end else if(cnt_seg == 3'd5 )begin sel <= 6'b111_110; end else begin sel <= sel; end end always @(posedge clk or negedge rst_n)begin if(!rst_n)begin seg <= 8'b1111_1111; end else begin case (cnt_num) 4'd0 : seg <= ZERO ; 4'd1 : seg <= ONE ; 4'd2 : seg <= TWO ; 4'd3 : seg <= THREE; 4'd4 : seg <= FOUR ; 4'd5 : seg <= FIVE ; 4'd6 : seg <= SIX ; 4'd7 : seg <= SEVEN; 4'd8 : seg <= EIGHT; 4'd9 : seg <= NINE ; 4'd10 : seg <= A ; 4'd11 : seg <= B ; 4'd12 : seg <= C ; 4'd13 : seg <= D ; 4'd14 : seg <= E ; 4'd15 : seg <= F ; default : seg <= ZERO ; endcase end end endmodule
打开Quartus Ⅱ,跟随以下图片示例进行操作:
项目目录定位到rtl文件夹(编译产生的文件都将保存在该文件夹中),对项目进行命名(一般建议项目名与文件名保持一致)
添加编写的.v文件
定位到rtl文件夹
点击打开并确定
选择芯片
一路next
进入Quartus界面后按照下图操作:
点击下图所示按钮,进行代码分析,若无错即可开始进行引脚绑定
引脚TCL文件如下:
# Copyright (C) 2018 Intel Corporation. All rights reserved. # Your use of Intel Corporation's design tools, logic functions # and other software and tools, and its AMPP partner logic # functions, and any output files from any of the foregoing # (including device programming or simulation files), and any # associated documentation or information are expressly subject # to the terms and conditions of the Intel Program License # Subscription Agreement, the Intel Quartus Prime License Agreement, # the Intel FPGA IP License Agreement, or other applicable license # agreement, including, without limitation, that your use is for # the sole purpose of programming logic devices manufactured by # Intel and sold by Intel or its authorized distributors. Please # refer to the applicable agreement for further details. # Quartus Prime Version 18.1.0 Build 625 09/12/2018 SJ Standard Edition # File: D:\intelFPGA\core\seg_led_static\tcl\seg_led_static_top.tcl # Generated on: Wed Apr 26 09:36:22 2023 package require ::quartus::project set_location_assignment PIN_A4 -to sel[0] set_location_assignment PIN_B4 -to sel[1] set_location_assignment PIN_A3 -to sel[2] set_location_assignment PIN_B3 -to sel[3] set_location_assignment PIN_A2 -to sel[4] set_location_assignment PIN_B1 -to sel[5] set_location_assignment PIN_B7 -to seg[0] set_location_assignment PIN_A8 -to seg[1] set_location_assignment PIN_E1 -to clk set_location_assignment PIN_E15 -to rst_n set_location_assignment PIN_A5 -to seg[7] set_location_assignment PIN_B8 -to seg[6] set_location_assignment PIN_A7 -to seg[5] set_location_assignment PIN_B6 -to seg[4] set_location_assignment PIN_B5 -to seg[3] set_location_assignment PIN_A6 -to seg[2]
关于如何运行TCL文件绑定引脚请自行搜索,不再赘述。
引脚绑定结果如下:
点击全编译按钮运行无错后即可上板烧录
如出现编程引脚复用报错,按下图操作:
右键Device
设为常用引脚即可
若还有其他报错,自行搜索
连接板子后按照下图所示操作:
点击start:
第一次连接开发板后会提醒下载相关驱动,如果没有提醒,参考此博文
右上角显示successful证明烧录成功
运行效果:
通过编写此次项目,进一步熟悉了Verilog语法,同时逐渐掌握了计时器和状态机这两个Verilog语言的核心,并且了解了数码管的工作原理。
https://blog.csdn.net/weixin_43828944/article/details/122296149
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。