在Basys3板子上实现数字时钟,要求可以进行时、分、秒计时的二十四小时制并具有闹钟和暂停功能的数字电子钟,代码怎么实现
时间: 2024-03-20 21:44:22 浏览: 149
以下是基于Verilog语言的代码实现,可以在Vivado中进行编译和综合:
```
module digital_clock(
input clk, //时钟信号
input rst, //复位信号
input [3:0]btn, //按键信号
output reg [6:0]seg, //数码管输出信号
output reg [3:0]an //数码管选择信号
);
reg [3:0]hour = 0; //小时数,4位二进制数
reg [3:0]min = 0; //分钟数,4位二进制数
reg [3:0]sec = 0; //秒钟数,4位二进制数
reg [3:0]alarm_hour = 0; //闹钟小时数,4位二进制数
reg [3:0]alarm_min = 0; //闹钟分钟数,4位二进制数
reg [3:0]alarm_sec = 0; //闹钟秒钟数,4位二进制数
reg [1:0]pause = 0; //暂停状态,2位二进制数
reg [1:0]alarm_set = 0; //闹钟设置状态,2位二进制数
reg [1:0]alarm_on = 0; //闹钟开关状态,2位二进制数
reg [1:0]alarm_ring = 0; //闹钟响铃状态,2位二进制数
//时钟模块
always @(posedge clk or negedge rst) begin
if(!rst) begin
hour <= 0;
min <= 0;
sec <= 0;
end else begin
if(!pause[0]) begin //秒钟计时
sec <= sec + 1;
if(sec >= 60) begin
sec <= 0;
min <= min + 1;
if(min >= 60) begin
min <= 0;
hour <= hour + 1;
if(hour >= 24) begin
hour <= 0;
end
end
end
end
if(btn[0] && !pause[0]) begin //暂停计时
pause <= 1;
end
if(btn[0] && pause[0]) begin //继续计时
pause <= 0;
end
end
end
//闹钟模块
always @(posedge clk or negedge rst) begin
if(!rst) begin
alarm_hour <= 0;
alarm_min <= 0;
alarm_sec <= 0;
end else begin
if(btn[1] && !alarm_set[0]) begin //设置闹钟
alarm_set <= 1;
end
if(alarm_set[0] && !btn[1]) begin //保存闹钟时间
alarm_hour <= hour;
alarm_min <= min;
alarm_sec <= sec;
alarm_set <= 0;
end
if(btn[2] && !alarm_on[0]) begin //开启闹钟
alarm_on <= 1;
end
if(btn[2] && alarm_on[0]) begin //关闭闹钟
alarm_on <= 0;
alarm_ring <= 0;
end
if(alarm_on[0] && !alarm_ring[0] && hour == alarm_hour && min == alarm_min && sec == alarm_sec) begin //闹钟响铃
alarm_ring <= 1;
end
end
end
//数码管驱动模块
always @(hour, min, sec, alarm_hour, alarm_min, alarm_sec, pause, alarm_ring) begin
if(pause[1]) begin //显示暂停状态
seg <= 7'b1111110;
an <= 4'b1110;
end else if(alarm_ring[1]) begin //闹钟响铃状态
seg <= 7'b0001111;
an <= 4'b1110;
end else begin //正常显示时、分、秒
case(an)
4'b1110: begin //选择第一个数码管
case({hour[2], hour[1], hour[0]})
3'b000: seg <= 7'b1000000; //0
3'b001: seg <= 7'b1111001; //1
3'b010: seg <= 7'b0100100; //2
3'b011: seg <= 7'b0110000; //3
3'b100: seg <= 7'b0011001; //4
3'b101: seg <= 7'b0010010; //5
3'b110: seg <= 7'b0000010; //6
3'b111: seg <= 7'b1111000; //7
endcase
end
4'b1101: begin //选择第二个数码管
case({hour[3], min[2], min[1], min[0]})
4'b0000: seg <= 7'b1000000; //0
4'b0001: seg <= 7'b1111001; //1
4'b0010: seg <= 7'b0100100; //2
4'b0011: seg <= 7'b0110000; //3
4'b0100: seg <= 7'b0011001; //4
4'b0101: seg <= 7'b0010010; //5
4'b0110: seg <= 7'b0000010; //6
4'b0111: seg <= 7'b1111000; //7
4'b1000: seg <= 7'b0000000; //8
4'b1001: seg <= 7'b0010000; //9
endcase
end
4'b1011: begin //选择第三个数码管
case({min[3], sec[2], sec[1], sec[0]})
4'b0000: seg <= 7'b1000000; //0
4'b0001: seg <= 7'b1111001; //1
4'b0010: seg <= 7'b0100100; //2
4'b0011: seg <= 7'b0110000; //3
4'b0100: seg <= 7'b0011001; //4
4'b0101: seg <= 7'b0010010; //5
4'b0110: seg <= 7'b0000010; //6
4'b0111: seg <= 7'b1111000; //7
4'b1000: seg <= 7'b0000000; //8
4'b1001: seg <= 7'b0010000; //9
endcase
end
4'b0111: begin //选择第四个数码管
if(alarm_set[1]) begin //闹钟设置状态
case({alarm_hour[2], alarm_hour[1], alarm_hour[0]})
3'b000: seg <= 7'b1000000; //0
3'b001: seg <= 7'b1111001; //1
3'b010: seg <= 7'b0100100; //2
3'b011: seg <= 7'b0110000; //3
3'b100: seg <= 7'b0011001; //4
3'b101: seg <= 7'b0010010; //5
3'b110: seg <= 7'b0000010; //6
3'b111: seg <= 7'b1111000; //7
endcase
end else begin //正常显示状态
case({alarm_on[1], alarm_hour[3], alarm_min[2], alarm_min[1], alarm_min[0]})
5'b00000: seg <= 7'b0000000; //闹钟未开启,无闹钟设置
5'b00001: seg <= 7'b0000000; //闹钟未开启,有闹钟设置
5'b00010: seg <= 7'b1000000; //闹钟开启,无闹钟设置
5'b00011: seg <= 7'b1111001; //闹钟开启,有闹钟设置
5'b00100: seg <= 7'b0100100; //闹钟关闭,无闹钟设置
5'b00101: seg <= 7'b0010010; //闹钟关闭,有闹钟设置
5'b00110: seg <= 7'b0000010; //闹钟响铃,无闹钟设置
5'b00111: seg <= 7'b0001111; //闹钟响铃,有闹钟设置
endcase
end
end
endcase
end
end
endmodule
```
该代码实现了数字时钟的计时、暂停、闹钟设置、开关和响铃等功能,同时可以实现24小时制的时、分、秒计时。在Basys3开发板上,将该代码烧录到FPGA芯片中,即可完成数字时钟的实现。
阅读全文