前言
本次实验内容承接上一篇文章数码管电子时钟,在此基础上新增两个功能:
1.设置时间
2.设置闹钟,到点响铃
一丶需求分析
模块:beep counter seg_driver top
其中:
1.设置时间
2.设置闹钟,到点响铃
这两个功能都整合在counter模块,里面设置的重要信号如下所示
1.设置时间
我们需要利用开发板上的按键来设置时分秒HH:MM:SS
思路:Counter:
rst_n:复位按键 【相当于开发板上的key1】
Key[0]:空闲状态—电子时钟 【相当于开发板上的key1】
Key[1]:设置时间 【相当于开发板上的key2】
Key[2]:设置闹钟 【相当于开发板上的key3】
按键切换三个状态:
1.空闲状态—就是一个时钟
输出dout_time给seg_driver模块显示时间
2.设置时间—设置当前的时分秒,改一下几个计时器里面的初值
修改counter里面的6个计时器的值,暂停计时
Key[0]:切换修改的时间位,切换到哪一位,哪一位闪烁 【相当于开发板上的key2】
Key[1]:加1 【相当于开发板上的key3】
Key[2]:确定–退出 【相当于开发板上的key4】
3.设置闹钟—设置个条件,到几时几分几秒蜂鸣器响
修改counter里面的6个计时器的值,暂停计时
Key[0]:切换修改的时间位,切换到哪一位,哪一位闪烁 【相当于开发板上的key2】
Key[1]:加1 【相当于开发板上的key3】
Key[2]:确定–退出(确定之后输出dout_time给beep模块作为响铃时间) 【相当于开发板上的key4】
2.设置闹钟
方式与设置时间基本完全一样,区别在设置闹钟的时间不给电子时钟,电子时钟保持设置的时间计时
二丶工程源码
1.counter
module counter (
input wire clk ,
input wire rst_n ,
input wire [2:0] key ,
output reg [19:0] dout_time , //输出时间 HH:MM:SS
output wire beep_r
);
//计数器
reg [25:0] cnt ;
wire add_cnt;
wire end_cnt;
//S计时器
//个位 (0~9)
reg [3:0] cnt_s_bit;
wire add_cnt_s_bit;
wire end_cnt_s_bit;
reg [3:0] set_cnt_s_bit;
wire add_set_cnt_s_bit;
wire end_set_cnt_s_bit;
reg [3:0] clock_cnt_s_bit;
wire add_clock_cnt_s_bit;
wire end_clock_cnt_s_bit;
//十位 (0~5)
reg [2:0] cnt_s_ten;
wire add_cnt_s_ten;
wire end_cnt_s_ten;
reg [2:0] set_cnt_s_ten;
wire add_set_cnt_s_ten;
wire end_set_cnt_s_ten;
reg [2:0] clock_cnt_s_ten;
wire add_clock_cnt_s_ten;
wire end_clock_cnt_s_ten;
//M计时器
//个位 (0~9)
reg [3:0] cnt_m_bit;
wire add_cnt_m_bit;
wire end_cnt_m_bit;
reg [3:0] set_cnt_m_bit;
wire add_set_cnt_m_bit;
wire end_set_cnt_m_bit;
reg [3:0] clock_cnt_m_bit;
wire add_clock_cnt_m_bit;
wire end_clock_cnt_m_bit;
//十位 (0~5)
reg [2:0] cnt_m_ten;
wire add_cnt_m_ten;
wire end_cnt_m_ten;
reg [2:0] set_cnt_m_ten;
wire add_set_cnt_m_ten;
wire end_set_cnt_m_ten;
reg [2:0] clock_cnt_m_ten;
wire add_clock_cnt_m_ten;
wire end_clock_cnt_m_ten;
//H计时器
//个位 (0~9)
reg [3:0] cnt_h_bit;
wire add_cnt_h_bit;
wire end_cnt_h_bit;
reg [3:0] set_cnt_h_bit;
wire add_set_cnt_h_bit;
wire end_set_cnt_h_bit;
reg [3:0] clock_cnt_h_bit;
wire add_clock_cnt_h_bit;
wire end_clock_cnt_h_bit;
//十位 (0~2)
reg [1:0] cnt_h_ten;
wire add_cnt_h_ten;
wire end_cnt_h_ten;
reg [1:0] set_cnt_h_ten;
wire add_set_cnt_h_ten;
wire end_set_cnt_h_ten;
reg [1:0] clock_cnt_h_ten;
wire add_clock_cnt_h_ten;
wire end_clock_cnt_h_ten;
reg [3:0] cnt_flag;
reg [3:0] set_cnt_flag;
reg [3:0] clock_cnt_flag;
reg [2:0] state_c; //现态
reg [2:0] state_n; //次态
reg [5:0] select_seg; //在设置时间和设置闹钟的时候切换位选
wire [19:0] set_time_dout;
wire [19:0] idel_dout;
wire [19:0] clock_dout;
parameter MAX_CNT=26'd50_000_000;
//定义状态
localparam IDEL =3'b001, //空闲状态
SET_TIME =3'b010, //设置时间
SET_CLOCK=3'b100; //设置闹钟
//状态转移条件
wire idel_TO_set_time;
wire idel_TO_set_clock;
wire set_time_TO_idel;
wire set_clock_TO_idel;
//状态机第一段--状态转移
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
state_c<=IDEL;
end
else
state_c<=state_n;
end
//状态机第二段--组合逻辑
always @(*) begin
case (state_c)
IDEL: begin
if (idel_TO_set_time) begin
state_n=SET_TIME;
end
else if(idel_TO_set_clock) begin
state_n=SET_CLOCK;
end
else
state_n=state_c;
end
SET_TIME: begin
if (set_time_TO_idel) begin
state_n=IDEL;
end
else
state_n=state_c;
end
SET_CLOCK: begin
if (set_clock_TO_idel) begin
state_n=IDEL;
end
else
state_n=state_c;
end
default :state_n=IDEL;
endcase
end
assign idel_TO_set_time=state_c==IDEL&&key[0];
assign idel_TO_set_clock=state_c==IDEL&&key[1];
assign set_time_TO_idel=state_c==SET_TIME&&key[2];
assign set_clock_TO_idel=state_c==SET_CLOCK&&key[2];
//select_seg
always @(posedge clk or negedge rst_n) begin
if(!rst_n) begin
select_seg<=6'b000_000;
end
else if(idel_TO_set_time||idel_TO_set_clock) begin
select_seg<=6'b000_001;
end
else if((state_c==SET_TIME||state_c==SET_CLOCK)&&(key[0])) begin
select_seg<={select_seg[4:0],select_seg[5]};
end
end
//clock_cnt_s_bit clock_cnt_s_ten clock_cnt_m_bit clock_cnt_m_ten clock_cnt_h_bit clock_cnt_h_ten
//秒计数器---个位(0~9)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
clock_cnt_s_bit<=0;
end
else if (add_clock_cnt_s_bit) begin
if (end_clock_cnt_s_bit) begin
clock_cnt_s_bit<=0;
end
else
clock_cnt_s_bit<=clock_cnt_s_bit+1;
end
end
assign add_clock_cnt_s_bit=state_c==SET_CLOCK&&select_seg==6'b000_001&&key[1];
assign end_clock_cnt_s_bit=add_clock_cnt_s_bit&&clock_cnt_s_bit==9||idel_TO_set_clock;
//秒计数器---十位(0~5)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
clock_cnt_s_ten<=0;
end
else if (add_clock_cnt_s_ten) begin
if (end_clock_cnt_s_ten) begin
clock_cnt_s_ten<=0;
end
else
clock_cnt_s_ten<=clock_cnt_s_ten+1;
end
end
assign add_clock_cnt_s_ten=state_c==SET_CLOCK&&select_seg==6'b000_010&&key[1];
assign end_clock_cnt_s_ten=add_clock_cnt_s_ten&&clock_cnt_s_ten==5||idel_TO_set_clock;
//分计数器---个位(0~9)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
clock_cnt_m_bit<=0;
end
else if (add_clock_cnt_m_bit) begin
if (end_clock_cnt_m_bit) begin
clock_cnt_m_bit<=0;
end
else
clock_cnt_m_bit<=clock_cnt_m_bit+1;
end
end
assign add_clock_cnt_m_bit=state_c==SET_CLOCK&&select_seg==6'b000_100&&key[1];
assign end_clock_cnt_m_bit=add_clock_cnt_m_bit&&clock_cnt_m_bit==9||idel_TO_set_clock;
//分计数器---十位(0~5)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
clock_cnt_m_ten<=0;
end
else if (add_clock_cnt_m_ten) begin
if (end_clock_cnt_m_ten) begin
clock_cnt_m_ten<=0;
end
else
clock_cnt_m_ten<=clock_cnt_m_ten+1;
end
end
assign add_clock_cnt_m_ten=state_c==SET_CLOCK&&select_seg==6'b001_000&&key[1];
assign end_clock_cnt_m_ten=add_clock_cnt_m_ten&&clock_cnt_m_ten==5||idel_TO_set_clock;
//时计数器---个位(0~9)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
clock_cnt_h_bit<=0;
end
else if (add_clock_cnt_h_bit) begin
if (end_clock_cnt_h_bit) begin
clock_cnt_h_bit<=0;
end
else
clock_cnt_h_bit<=clock_cnt_h_bit+1;
end
end
assign add_clock_cnt_h_bit=state_c==SET_CLOCK&&select_seg==6'b010_000&&key[1];
assign end_clock_cnt_h_bit=add_clock_cnt_h_bit&&clock_cnt_h_bit==clock_cnt_flag||idel_TO_set_clock;
//时计数器---十位(0~2)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
clock_cnt_h_ten<=0;
end
else if (add_clock_cnt_h_ten) begin
if (end_clock_cnt_h_ten) begin
clock_cnt_h_ten<=0;
end
else
clock_cnt_h_ten<=clock_cnt_h_ten+1;
end
end
assign add_clock_cnt_h_ten=state_c==SET_CLOCK&&select_seg==6'b100_000&&key[1];
assign end_clock_cnt_h_ten=add_clock_cnt_h_ten&&clock_cnt_h_ten==2||idel_TO_set_clock;
//判断小时计时器十位是否记到 2
always @(*) begin
if (clock_cnt_h_ten==2) begin
clock_cnt_flag=4'd3;
end
else
clock_cnt_flag=4'd9;
end
assign clock_dout={clock_cnt_h_ten,clock_cnt_h_bit,clock_cnt_m_ten,clock_cnt_m_bit,clock_cnt_s_ten,clock_cnt_s_bit};
///
///
//set_cnt_s_bit set_cnt_s_ten set_cnt_m_bit set_cnt_m_ten set_cnt_h_bit set_cnt_h_ten
//秒计数器---个位(0~9)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
set_cnt_s_bit<=0;
end
else if (add_set_cnt_s_bit) begin
if (end_set_cnt_s_bit) begin
set_cnt_s_bit<=0;
end
else
set_cnt_s_bit<=set_cnt_s_bit+1;
end
end
assign add_set_cnt_s_bit=state_c==SET_TIME&&select_seg==6'b000_001&&key[1];
assign end_set_cnt_s_bit=add_set_cnt_s_bit&&set_cnt_s_bit==9||idel_TO_set_time;
//秒计数器---十位(0~5)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
set_cnt_s_ten<=0;
end
else if (add_set_cnt_s_ten) begin
if (end_set_cnt_s_ten) begin
set_cnt_s_ten<=0;
end
else
set_cnt_s_ten<=set_cnt_s_ten+1;
end
end
assign add_set_cnt_s_ten=state_c==SET_TIME&&select_seg==6'b000_010&&key[1];
assign end_set_cnt_s_ten=add_set_cnt_s_ten&&set_cnt_s_ten==5||idel_TO_set_time;
//分计数器---个位(0~9)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
set_cnt_m_bit<=0;
end
else if (add_set_cnt_m_bit) begin
if (end_set_cnt_m_bit) begin
set_cnt_m_bit<=0;
end
else
set_cnt_m_bit<=set_cnt_m_bit+1;
end
end
assign add_set_cnt_m_bit=state_c==SET_TIME&&select_seg==6'b000_100&&key[1];
assign end_set_cnt_m_bit=add_set_cnt_m_bit&&set_cnt_m_bit==9||idel_TO_set_time;
//分计数器---十位(0~5)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
set_cnt_m_ten<=0;
end
else if (add_set_cnt_m_ten) begin
if (end_set_cnt_m_ten) begin
set_cnt_m_ten<=0;
end
else
set_cnt_m_ten<=set_cnt_m_ten+1;
end
end
assign add_set_cnt_m_ten=state_c==SET_TIME&&select_seg==6'b001_000&&key[1];
assign end_set_cnt_m_ten=add_set_cnt_m_ten&&set_cnt_m_ten==5||idel_TO_set_time;
//时计数器---个位(0~9)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
set_cnt_h_bit<=0;
end
else if (add_set_cnt_h_bit) begin
if (end_set_cnt_h_bit) begin
set_cnt_h_bit<=0;
end
else
set_cnt_h_bit<=set_cnt_h_bit+1;
end
end
assign add_set_cnt_h_bit=state_c==SET_TIME&&select_seg==6'b010_000&&key[1];
assign end_set_cnt_h_bit=add_set_cnt_h_bit&&set_cnt_h_bit==set_cnt_flag||idel_TO_set_time;
//时计数器---十位(0~2)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
set_cnt_h_ten<=0;
end
else if (add_set_cnt_h_ten) begin
if (end_set_cnt_h_ten) begin
set_cnt_h_ten<=0;
end
else
set_cnt_h_ten<=set_cnt_h_ten+1;
end
end
assign add_set_cnt_h_ten=state_c==SET_TIME&&select_seg==6'b100_000&&key[1];
assign end_set_cnt_h_ten=add_set_cnt_h_ten&&set_cnt_h_ten==2||idel_TO_set_time;
//判断小时计时器十位是否记到 2
always @(*) begin
if (set_cnt_h_ten==2) begin
set_cnt_flag=4'd3;
end
else
set_cnt_flag=4'd9;
end
assign set_time_dout={set_cnt_h_ten,set_cnt_h_bit,set_cnt_m_ten,set_cnt_m_bit,set_cnt_s_ten,set_cnt_s_bit};
///
///
//计数器
always @(posedge clk or negedge rst_n) begin
if(!rst_n) begin
cnt<=0;
end
else if (add_cnt) begin
if (end_cnt) begin
cnt<=0;
end
else
cnt<=cnt+1;
end
end
assign add_cnt=state_c==IDEL||state_c==SET_CLOCK;
assign end_cnt=add_cnt&&(cnt==MAX_CNT-1||set_time_TO_idel);
//秒计时器---个位(0~9)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
cnt_s_bit<=1;
end
else if(set_time_TO_idel) begin
cnt_s_bit<=set_cnt_s_bit; //在设置时间确定之后将设置的值赋给计时器
end
else if (add_cnt_s_bit) begin
if (end_cnt_s_bit) begin
cnt_s_bit<=0;
end
else
cnt_s_bit<=cnt_s_bit+1;
end
end
assign add_cnt_s_bit=end_cnt;
assign end_cnt_s_bit=add_cnt_s_bit&&cnt_s_bit==9;
//秒计时器---十位(0~5)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
cnt_s_ten<=0;
end
else if(set_time_TO_idel) begin
cnt_s_ten<=set_cnt_s_ten;
end
else if (add_cnt_s_ten) begin
if (end_cnt_s_ten) begin
cnt_s_ten<=0;
end
else
cnt_s_ten<=cnt_s_ten+1;
end
end
assign add_cnt_s_ten=end_cnt_s_bit;
assign end_cnt_s_ten=add_cnt_s_ten&&cnt_s_ten==5;
//分计时器---个位(0~9)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
cnt_m_bit<=0;
end
else if(set_time_TO_idel) begin
cnt_m_bit<=set_cnt_m_bit;
end
else if (add_cnt_m_bit) begin
if (end_cnt_m_bit) begin
cnt_m_bit<=0;
end
else
cnt_m_bit<=cnt_m_bit+1;
end
end
assign add_cnt_m_bit=end_cnt_s_ten;
assign end_cnt_m_bit=add_cnt_m_bit&&cnt_m_bit==9;
//分计时器---十位(0~5)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
cnt_m_ten<=0;
end
else if(set_time_TO_idel) begin
cnt_m_ten<=set_cnt_m_ten;
end
else if (add_cnt_m_ten) begin
if (end_cnt_m_ten) begin
cnt_m_ten<=0;
end
else
cnt_m_ten<=cnt_m_ten+1;
end
end
assign add_cnt_m_ten=end_cnt_m_bit;
assign end_cnt_m_ten=add_cnt_m_ten&&cnt_m_ten==5;
//时计时器---个位(0~9)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
cnt_h_bit<=0;
end
else if(set_time_TO_idel) begin
cnt_h_bit<=set_cnt_h_bit;
end
else if (add_cnt_h_bit) begin
if (end_cnt_h_bit) begin
cnt_h_bit<=0;
end
else
cnt_h_bit<=cnt_h_bit+1;
end
end
assign add_cnt_h_bit=end_cnt_m_ten;
assign end_cnt_h_bit=add_cnt_h_bit&&cnt_h_bit==cnt_flag;
//时计时器---十位(0~2)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
cnt_h_ten<=0;
end
else if(set_time_TO_idel) begin
cnt_h_ten<=set_cnt_h_ten;
end
else if (add_cnt_h_ten) begin
if (end_cnt_h_ten) begin
cnt_h_ten<=0;
end
else
cnt_h_ten<=cnt_h_ten+1;
end
end
assign add_cnt_h_ten=end_cnt_h_bit;
assign end_cnt_h_ten=add_cnt_h_ten&&cnt_h_ten==2;
//判断小时计时器十位是否记到 2
always @(*) begin
if (cnt_h_ten==2) begin
cnt_flag=4'd3;
end
else
cnt_flag=4'd9;
end
assign idel_dout={cnt_h_ten,cnt_h_bit,cnt_m_ten,cnt_m_bit,cnt_s_ten,cnt_s_bit}; //拼接成 HH:MM:SS
///
///
//dout_time输出
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
dout_time<=20'b0;
end
else begin
case (state_c)
IDEL:dout_time<=idel_dout; //控制数码管显示对应状态的值
SET_TIME:dout_time<=set_time_dout;
SET_CLOCK:dout_time<=clock_dout;
default :dout_time<=IDEL;
endcase
end
end
assign beep_r=clock_dout==idel_dout; //比较设置的闹钟与现在的时间,结果输出给beep模块,到点闹铃
endmodule //counter
2.seg_driver
module seg_driver (
input wire clk,
input wire rst_n,
input wire [19:0] dout_time,
output reg [5:0] sel,
output reg [7:0] seg
);
reg [3:0] seg_flag;
reg dot; //小数点 用来显示 HH.MM.SS 这样的格式
//10ms计时器---用来切换数码管位选,以达到轮流显示时间的各位(肉眼可以看到动态的时间计数)
reg [15:0] cnt;
wire add_cnt;
wire end_cnt;
parameter MAX_CNT =50_000 ,
ZERO =7'b100_0000,
ONE =7'b111_1001,
TWO =7'b010_0100,
THREE =7'b011_0000,
FOUR =7'b001_1001,
FIVE =7'b001_0010,
SIX =7'b000_0010,
SEVEN =7'b111_1000,
EIGHT =7'b000_0000,
NINE =7'b001_0000;
//计时器
always @(posedge clk or negedge rst_n) begin
if(!rst_n) begin
cnt<=0;
end
else if(add_cnt) begin
if (end_cnt) begin
cnt<=0;
end
else
cnt<=cnt+1;
end
end
assign add_cnt=1'b1;
assign end_cnt=add_cnt&&cnt==MAX_CNT-1;
//切换数码管位选
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
sel<=6'b111_110;
end
else if(cnt==MAX_CNT-1) begin
sel<={sel[4:0],sel[5]};
end
end
//切换数码管段选
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
seg_flag<=0;
end
else begin
case (sel)
6'b111_110: begin seg_flag<=dout_time[19:18]; dot<=1'b1;end //小时 十位
6'b111_101: begin seg_flag<=dout_time[17:14]; dot<=1'b0;end //小时 个位
6'b111_011: begin seg_flag<=dout_time[13:11]; dot<=1'b1;end //分钟 十位
6'b110_111: begin seg_flag<=dout_time[10:7]; dot<=1'b0;end //分钟 个位
6'b101_111: begin seg_flag<=dout_time[6:4]; dot<=1'b1;end //秒 十位
6'b011_111: begin seg_flag<=dout_time[3:0]; dot<=1'b1;end //秒 个位
default :seg_flag<=0;
endcase
end
end
//段选译码
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
seg<=8'b1111_1111;
end
else begin
case (seg_flag)
0: seg<={dot,ZERO} ;
1: seg<={dot,ONE} ;
2: seg<={dot,TWO} ;
3: seg<={dot,THREE} ;
4: seg<={dot,FOUR} ;
5: seg<={dot,FIVE} ;
6: seg<={dot,SIX} ;
7: seg<={dot,SEVEN} ;
8: seg<={dot,EIGHT} ;
9: seg<={dot,NINE} ;
default: seg<=8'b1111_1111;
endcase
end
end
endmodule //seg_driver
3.key_debounce
module key_debounce (
input wire clk, //系统时钟 50MHz
input wire rst_n, //复位信号
input wire key, //按键输入信号
output reg key_done //消抖之后的按键信号
);
reg key_r0; //同步信号(滤波作用,滤除小于一个周期的抖动)
reg key_r1; //打拍
reg flag; //标志位
wire nedge; //下降沿检测(检测到下降沿代表开始抖动)
//计时器定义
reg [19:0] cnt;
wire add_cnt; //计时器开启
wire end_cnt; //计时记满
parameter MAX_CNT=20'd1_000_000; //20ms延时
//同步
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
key_r0<=1'b1;
end
else
key_r0<=key;
end
//打拍
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
key_r1<=1'b1;
end
else
key_r1<=key_r0;
end
assign nedge = ~key_r0 & key_r1; //检测到下降沿拉高
//标志位
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
flag<=1'b0;
end
else if (nedge) begin
flag<=1'b1;
end
else if (end_cnt) begin
flag<=1'b0;
end
end
//延时模块
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
cnt<=20'b0;
end
else if (add_cnt) begin
if (end_cnt) begin
cnt<=20'b0;
end
else
cnt<=cnt+1;
end
end
assign add_cnt=flag; //计时器开启
assign end_cnt=add_cnt&&cnt==MAX_CNT-1; //计时器关闭
//key_done输出
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
key_done<=1'b0;
end
else if (end_cnt) begin //延时满20ms采样
key_done<=~key_r0;
end
else
key_done<=1'b0;
end
endmodule //key_debounce
4.beep
module beep (
input wire clk,
input wire rst_n,
input wire beep_r,
output reg beep_out
);
reg [25:0] cnt;
wire add_cnt;
wire end_cnt;
parameter MAX_CNT=26'd50_000_000;
//计时器
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
cnt<=0;
end
else if(add_cnt) begin
if (end_cnt) begin
cnt<=0;
end
else
cnt<=cnt+1;
end
end
assign add_cnt=1;
assign end_cnt=add_cnt&&cnt==MAX_CNT-1;
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
beep_out<=1;
end
else if(beep_r) begin
beep_out<=0;
end
else if (end_cnt) begin
beep_out<=1;
end
else
beep_out<=beep_out;
end
endmodule //beep
5.顶层模块
module top (
input wire clk , //系统时钟
input wire rst_n , //复位信号
input wire [2:0] key , //三个按键
output wire [5:0] sel , //数码管位选
output wire [7:0] seg , //数码管段选
output wire beep_out
);
wire [19:0] dout_time;
wire [2:0] key_done;
wire beep_r;
//例化计时模块
counter u_counter(
.clk (clk) ,
.rst_n (rst_n) ,
.key (key_done) ,
.dout_time (dout_time) , //输出时间 HH:MM:SS
.beep_r (beep_r)
);
//例化数码管驱动
seg_driver u_seg_driver(
.clk (clk) ,
.rst_n (rst_n) ,
.sel (sel) ,
.seg (seg) ,
.dout_time (dout_time)
);
//例化按键消抖
key_debounce key_debounce2(
.clk (clk), //系统时钟 50MHz
.rst_n (rst_n), //复位信号
.key (key[0]), //按键输入信号
.key_done (key_done[0]) //消抖之后的按键信号
);
key_debounce key_debounce3(
.clk (clk), //系统时钟 50MHz
.rst_n (rst_n), //复位信号
.key (key[1]), //按键输入信号
.key_done (key_done[1]) //消抖之后的按键信号
);
key_debounce key_debounce4(
.clk (clk), //系统时钟 50MHz
.rst_n (rst_n), //复位信号
.key (key[2]), //按键输入信号
.key_done (key_done[2]) //消抖之后的按键信号
);
//例化闹钟模块
beep u_beep(
.clk (clk),
.rst_n (rst_n),
.beep_r (beep_r),
.beep_out (beep_out)
);
endmodule //top
三丶模块原理图
四丶管脚信息
五丶上板验证
数码管电子时钟(设置时间+设置闹钟)文章来源:https://www.toymoban.com/news/detail-403323.html
六丶源码
https://github.com/xuranww/update_digital_clock.git文章来源地址https://www.toymoban.com/news/detail-403323.html
到了这里,关于【FPGA】数码管电子时钟(可设置时间和闹钟)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!