有些思路是从各种资料中搜集到的,还有一些优化之处,以后我可能会修改代码,比如用模运算进行优化。小时是 LED灯
源文件如下:
`timescale 1ns / 1ps
//
// Company:
// Engineer:
//
// Create Date: 2023/10/24 10:57:00
// Design Name:
// Module Name: digit_clock
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//
module digit_clock(clk, rst,segs, en,dot,led,add_sec,add_min,add_hour);
input clk; //clk接系统默认时钟信号,频率为100MHz
input rst; //复位键
input add_sec;
input add_min;
input add_hour;
output reg [6:0]segs;
output reg [3:0]en; //使能信号
output reg dot; //小数点
output reg [4:0] led;//小时
wire [1:0] s; //选择变量,用于每一次刷新数码管时决定使能和哪些数码管需要刷新
reg [3:0] digit; //用于传递计时生成的信号
reg [26:0] clk_cnt=0; //计数变量,记录clk的次数,达到1s就归零
reg [6:0] second; //秒,[6:4]记录秒的十位,[3:0]记录秒的个位
reg [6:0] minute; //分,同秒
reg [4:0] hour=0; //时,由于数码管数量问题,不进行显示
assign s=clk_cnt[19:18];
always@(*) //使能,根据s信号决定使能哪些端口
begin
en=4'b1111;
en[s]=0;//每当clk_cnt=10000_0000时,对应的s=0(2'b00)
end
always@(posedge clk or posedge rst or posedge add_sec or posedge add_min or posedge add_hour )
begin
if(rst)
begin//复位信号,0时可变,1时不可变
clk_cnt=0;
second=0;
minute=0;
hour=0;
end
else if(clk_cnt==10000_0000)
begin//这里是时钟信号,由于clk的一个周期为10纳秒,对应10^(-8)秒,所以要技术到10^8才能进行操作
clk_cnt=0;//归零
if(add_sec && second[3:0]<9) second[3:0]=second[3:0]+1;
if(add_min && minute[3:0]<9) minute[3:0]=minute[3:0]+1;
if(add_hour ) hour=hour+1;
if(hour>=24) hour=0;
if(second[3:0]!=9)
begin
second[3:0]=second[3:0]+1;
end//由于second[3:0]记录秒的个位,而最大值为9,因此在小于9是自增,等于9时需要进行进位和归零
else
begin
if(second[6:4]!=5) second[6:4]=second[6:4]+1;//秒的十位,同样的需要进位(逢6进1),因此和上述操作类似
else
begin
if(minute[3:0]!=9) minute[3:0]=minute[3:0]+1; //同上
else
begin
if(minute[6:4]!=5) minute[6:4]=minute[6:4]+1;
else
begin
hour=hour+1;
minute[6:4]=0;
end
minute[3:0]=0;
end
second[6:4]=0;
end
second[3:0]=0;
end
end
else clk_cnt=clk_cnt+1;//继续计数
end
always@(*) //确定位
case(s)
2'b00: begin digit=second[3:0]; dot=1; led=hour; end //秒的个位
2'b01: begin digit=second[6:4]; dot=1; led=hour; end //秒的十位
2'b10: begin digit=minute[3:0]; dot=0; led=hour; end //分的个位,这个地方注意,如果dot一直是1,会出现一些问题,即不能显示dot,猜测原因(如果分需要修改,此时en对应4'b1011,这个位置也是dot的位置,修改会导致dot失效)
2'b11: begin digit=minute[6:4]; dot=1; led=hour; end //分的十位
default: begin digit=second[3:0]; led=hour; end
endcase
always@(*)
case(digit)//根据数码管原理图进行
4'h0:segs=7'b0000001;
4'h1:segs=7'b1001111;
4'h2:segs=7'b0010010;
4'h3:segs=7'b0000110;
4'h4:segs=7'b1001100;
4'h5:segs=7'b0100100;
4'h6:segs=7'b0100000;
4'h7:segs=7'b0001111;
4'h8:segs=7'b0000000;
4'h9:segs=7'b0000100;
default:segs=7'b0000001;
endcase
endmodule
约束文件
set_property SEVERITY {Warning} [get_drc_checks UCIO-1]
set_property IOSTANDARD LVCMOS33 [get_ports clk]
set_property IOSTANDARD LVCMOS33 [get_ports dot]
set_property IOSTANDARD LVCMOS33 [get_ports rst]
#set_property IOSTANDARD LVCMOS33 [get_ports {led[]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[4]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {en[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {en[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {en[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {en[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {segs[6]}]
set_property IOSTANDARD LVCMOS33 [get_ports {segs[5]}]
set_property IOSTANDARD LVCMOS33 [get_ports {segs[4]}]
set_property IOSTANDARD LVCMOS33 [get_ports {segs[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {segs[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {segs[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {segs[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports add_sec]
set_property IOSTANDARD LVCMOS33 [get_ports add_min]
set_property IOSTANDARD LVCMOS33 [get_ports add_hour]
set_property PACKAGE_PIN W5 [get_ports clk]
set_property PACKAGE_PIN V7 [get_ports dot]
set_property PACKAGE_PIN V17 [get_ports rst]
set_property PACKAGE_PIN R2 [get_ports add_sec]
set_property PACKAGE_PIN T1 [get_ports add_min]
set_property PACKAGE_PIN U1 [get_ports add_hour]
set_property PACKAGE_PIN U3 [get_ports {led[0]}]
set_property PACKAGE_PIN P3 [get_ports {led[1]}]
set_property PACKAGE_PIN N3 [get_ports {led[2]}]
set_property PACKAGE_PIN P1 [get_ports {led[3]}]
set_property PACKAGE_PIN L1 [get_ports {led[4]}]
set_property PACKAGE_PIN U2 [get_ports {en[0]}]
set_property PACKAGE_PIN U4 [get_ports {en[1]}]
set_property PACKAGE_PIN V4 [get_ports {en[2]}]
set_property PACKAGE_PIN W4 [get_ports {en[3]}]
set_property PACKAGE_PIN W7 [get_ports {segs[6]}]
set_property PACKAGE_PIN W6 [get_ports {segs[5]}]
set_property PACKAGE_PIN U8 [get_ports {segs[4]}]
set_property PACKAGE_PIN V8 [get_ports {segs[3]}]
set_property PACKAGE_PIN U5 [get_ports {segs[2]}]
set_property PACKAGE_PIN V5 [get_ports {segs[1]}]
set_property PACKAGE_PIN U7 [get_ports {segs[0]}]
演示如下