设计内容:万年历
设计一个基于FPGA的电子万年历。设计的主要任务是在Quartus II开发环境中完成电子万年历系统FPGA内部各电路模块的设计,包括各个模块的设计输入、编译、仿真、验证和硬件测试任务。具体要求如下:
- 能实现24小时、60分、60秒的基本计时功能,格式为08-56-36:时-分-秒;可以通过按键设置定时和调整时间,并通过数码管显示时间;
- 能实现年月日的日期功能,格式为05-16-2018:月-日-年;
- 上板复位(拨码键SW0)后从2000年1月1号0时0分0秒开始计时;
- 闰年:每400年整一闰,或每4年且不为百年的一闰。即能被400整除,或不能被100整除但能被4整除的年份为闰年;
- 平年365天(52周+1天),闰年366天(52周+2天),其中平年2月28天,闰年2月29天。
- 拨码键SW1用于选择是日历模式还是时间模式(高电平);SW2用于决定是时间设置模式(高电平),还是计数模式;SW3用于设置秒,SW4用于设置分,SW5用于设置小时,SW17用于设置天,SW16用于设置月,SW15,SW14, SW13, SW12分别用于设置年的千位、百位、十位、各位,高电平是选中设置状态;
- 按键要有防抖功能,按下按键0时任务(6)中的设置位自增1。
- 设计思路:计数器记够一秒,秒加一;秒记够59秒且计数器记够1秒,,分钟加1;分钟记够59分且秒记够59秒且计数器记够1秒,小时加1;小时记够23小时且分钟记够59分且秒记够59秒且计数器记够1秒,天加1;当1.3.5.7.8.10.12月时天记够31天且小时记够23小时且分钟记够59分且秒记够59秒且计数器记够1秒,月加1;当4.6.9.11月时天记够30天且小时记够23小时且分钟记够59分且秒记够59秒且计数器记够1秒,月加1;当2月时平年天记够28天且小时记够23小时且分钟记够59分且秒记够59秒且计数器记够1秒,月加1;当月记够12月且天记够31天且小时记够23小时且分钟记够59分且秒记够59秒且计数器记够1秒,年加1。
- 注意事项:顶层文件中有两种计数方式,一种是仿真使用,另一种加//的是上板使用(板子时钟频率为50Mkz)
-
代码:
-
顶层文件:
module wnl (
input clk,
input rst_n,
input sw1,
input sw2,
input sw3,//秒
input sw4,//分钟
input sw5,//小时
input sw6,//年[3:0]最低位
input sw7,//年[7:4]次低位
input sw8,//年[11:8]次高位
input sw9,//年[15:12]最高位
input sw10,//日
input sw11,//月
input key_plus,input key_sub,
output reg [7:0] s,output reg [7:0] m,
output reg [7:0] h,output reg [7:0] r,
output reg [7:0] y,output reg [15:0] n,
output reg [6:0] data_out1,output reg [6:0] data_out2,
output reg [6:0] data_out3,output reg [6:0] data_out4,
output reg [6:0] data_out5,output reg [6:0] data_out6,
output reg [6:0] data_out7,output reg [6:0] data_out8
);文章来源:https://www.toymoban.com/news/detail-760964.html
wire up_flag;wire down_flag;
wire key_plus_flag;wire key_sub_flag;
wire key_plus_value;wire key_sub_value;
reg [32:0] cnt;
assign up_flag = key_plus_flag && (~key_plus_value);
assign down_flag = key_sub_flag && (~key_sub_value);
always@(posedge clk or negedge rst_n)begin
if(!rst_n)begin
cnt<=32'd0;end
//else if (cnt <= 32'd49_999_999)begin
else if(cnt <= 32'd2)begin
cnt<=cnt+32'd1;end
else begin
cnt<=32'd0;end
end
always@(posedge clk or negedge rst_n)begin//s
if(!rst_n)begin
s<=0;end
else if(sw2==0)begin
//if(cnt == 32'd49_999_999)begin
if(cnt == 32'd2)begin
if((s[3:0]<9) && (s[7:4]<5))begin
s[3:0]<=s[3:0]+1;s[7:4]<=s[7:4];end
else if((s[3:0]==9) && (s[7:4]<5))begin
s[3:0]<=0;s[7:4]<=s[7:4]+1;end
else if((s[3:0]<9) && (s[7:4]==5))begin
s[3:0]<=s[3:0]+1;s[7:4]<=s[7:4];end
else if((s[3:0]==9) && (s[7:4]==5))begin
s[3:0]<=0;s[7:4]<=0;end
end
end
else if(sw2==1 && sw3==1)begin
if(up_flag)begin
if((s[3:0]<9) && (s[7:4]<5))begin
s[3:0]<=s[3:0]+1;s[7:4]<=s[7:4];end
else if((s[3:0]==9) && (s[7:4]<5))begin
s[3:0]<=0;s[7:4]<=s[7:4]+1;end
else if((s[3:0]<9) && (s[7:4]==5))begin
s[3:0]<=s[3:0]+1;s[7:4]<=s[7:4];end
else if((s[3:0]==9) && (s[7:4]==5))begin
s[3:0]<=0;s[7:4]<=0;end
end
end
else if(sw2==1 && sw3==0)begin
s[3:0]<=s[3:0];s[7:4]<=s[7:4];end
end
always@(posedge clk or negedge rst_n)begin//m
if(!rst_n)begin
m<=0;end
else if(sw2==0)begin
//if((s[3:0]==9) && (s[7:4]==5) && (cnt == 32'd49_999_999))begin
if((s[3:0]==9) && (s[7:4]==5) && (cnt == 32'd2))begin
if((m[3:0]<9) && (m[7:4]<5))begin
m[3:0]<=m[3:0]+1;m[7:4]<=m[7:4];end
else if((m[3:0]==9) && (m[7:4]<5))begin
m[3:0]<=0;m[7:4]<=m[7:4]+1;end
else if((m[3:0]<9) && (m[7:4]==5))begin
m[3:0]<=m[3:0]+1;m[7:4]<=m[7:4];end
else if((m[3:0]==9) && (m[7:4]==5))begin
m[3:0]<=0;m[7:4]<=0;end
end
end
else if(sw2==1 && sw4==1)begin
if(up_flag)begin
if((m[3:0]<9) && (m[7:4]<5))begin
m[3:0]<=m[3:0]+1;m[7:4]<=m[7:4];end
else if((m[3:0]==9) && (m[7:4]<5))begin
m[3:0]<=0;m[7:4]<=m[7:4]+1;end
else if((m[3:0]<9) && (m[7:4]==5))begin
m[3:0]<=m[3:0]+1;m[7:4]<=m[7:4];end
else if((m[3:0]==9) && (m[7:4]==5))begin
m[3:0]<=0;m[7:4]<=0;end
end
end
else if(sw2==1 && sw4==0)begin
m[3:0]<=m[3:0];m[7:4]<=m[7:4];end
end
always@(posedge clk or negedge rst_n)begin//h
if(!rst_n)begin
h<=0;end
else if(sw2==0)begin
//if((m[3:0]==9) && (m[7:4]==5) && (s[3:0]==9) && (s[7:4]==5) && (cnt == 32'd49_999_999))begin
if((m[3:0]==9) && (m[7:4]==5) && (s[3:0]==9) && (s[7:4]==5) && (cnt == 32'd2))begin
if((h[3:0]<9) && (h[7:4]<2))begin
h[3:0]<=h[3:0]+1;h[7:4]<=h[7:4];end
else if((h[3:0]==9) && (h[7:4]<2))begin
h[3:0]<=0;h[7:4]<=h[7:4]+1;end
else if((h[3:0]<3) && (h[7:4]==2))begin
h[3:0]<=h[3:0]+1;h[7:4]<=h[7:4];end
else if((h[3:0]==3) && (h[7:4]==2))begin
h[3:0]<=0;h[7:4]<=0;end
end
end
else if(sw2==1 && sw5==1)begin
if(up_flag)begin
if((h[3:0]<9) && (h[7:4]<2))begin
h[3:0]<=h[3:0]+1;h[7:4]<=h[7:4];end
else if((h[3:0]==9) && (h[7:4]<2))begin
h[3:0]<=0;h[7:4]<=h[7:4]+1;end
else if((h[3:0]<3) && (h[7:4]==2))begin
h[3:0]<=h[3:0]+1;h[7:4]<=h[7:4];end
else if((h[3:0]==3) && (h[7:4]==2))begin
h[3:0]<=0;h[7:4]<=0;end
end
end
else if(sw2==1 && sw5==0)begin
h[3:0]<=h[3:0];h[7:4]<=h[7:4];end
end
always@(posedge clk or negedge rst_n)begin//r
if(!rst_n)begin
r[3:0]<=1;r[7:4]<=0;end
else if(sw2==0)begin
//if((h[3:0]==3 && h[7:4]==2) && (m[3:0]==9 && m[7:4]==5) && (s[3:0]==9 && s[7:4]==5) && (cnt == 32'd49_999_999))begin
if((h[3:0]==3 && h[7:4]==2) && (m[3:0]==9 && m[7:4]==5) && (s[3:0]==9 && s[7:4]==5) && (cnt == 32'd42))begin
if((y[3:0]==1 && y[7:4]==0)|(y[3:0]==3 && y[7:4]==0)|(y[3:0]==5 && y[7:4]==0)|(y[3:0]==7 && y[7:4]==0)|(y[3:0]==8 && y[7:4]==0)|(y[3:0]==0 && y[7:4]==1)|(y[3:0]==2 && y[7:4]==1))begin
if((r[3:0]<9) && (r[7:4]<3))begin
r[3:0]<=r[3:0]+1;r[7:4]<=r[7:4];end
else if((r[3:0]==9) && (r[7:4]<3))begin
r[3:0]<=0;r[7:4]<=r[7:4]+1;end
else if((r[3:0]==1) && (r[7:4]==3))begin
r[3:0]<=1;r[7:4]<=0;end
end
else if((y[3:0]==4 && y[7:4]==0)|(y[3:0]==6 && y[7:4]==0)|(y[3:0]==9 && y[7:4]==0)|(y[3:0]==1 && y[7:4]==1))begin
if((r[3:0]<9) && (r[7:4]<3))begin
r[3:0]<=r[3:0]+1;r[7:4]<=r[7:4];end
else if((r[3:0]==9) && (r[7:4]<3))begin
r[3:0]<=0;r[7:4]<=r[7:4]+1;end
else if((r[3:0]==0) && (r[7:4]==3))begin
r[3:0]<=1;r[7:4]<=0;end
end
else if((y[3:0]==2 && y[7:4]==0) && ((n%4)==0))begin
if((r[3:0]<9) && (r[7:4]<2))begin
r[3:0]<=r[3:0]+1;r[7:4]<=r[7:4];end
else if((r[3:0]==9) && (r[7:4]<2))begin
r[3:0]<=0;r[7:4]<=r[7:4]+1;end
else if((r[3:0]==9) && (r[7:4]==2))begin
r[3:0]<=1;r[7:4]<=0;end
end
else if((y[3:0]==2 && y[7:4]==0) && ((n%4)!=0))begin
if((r[3:0]<9) && (r[7:4]<2))begin
r[3:0]<=r[3:0]+1;r[7:4]<=r[7:4];end
else if((r[3:0]==9) && (r[7:4]<2))begin
r[3:0]<=0;r[7:4]<=r[7:4]+1;end
else if((r[3:0]==8) && (r[7:4]==2))begin
r[3:0]<=1;r[7:4]<=0;end
end
end
end
else if(sw2==1 && sw10==1)begin
if(up_flag)begin
if((y[3:0]==1 && y[7:4]==0)|(y[3:0]==3 && y[7:4]==0)|(y[3:0]==5 && y[7:4]==0)|(y[3:0]==7 && y[7:4]==0)|(y[3:0]==8 && y[7:4]==0)|(y[3:0]==0 && y[7:4]==1)|(y[3:0]==2 && y[7:4]==1))begin
if((r[3:0]<9) && (r[7:4]<3))begin
r[3:0]<=r[3:0]+1;r[7:4]<=r[7:4];end
else if((r[3:0]==9) && (r[7:4]<3))begin
r[3:0]<=0;r[7:4]<=r[7:4]+1;
end
else if((r[3:0]<1) && (r[7:4]==3))begin
r[3:0]<=r[3:0]+1;r[7:4]<=r[7:4];end
else if((r[3:0]==1) && (r[7:4]==3))begin
r[3:0]<=1;r[7:4]<=0;end
end
else if((y[3:0]==4 && y[7:4]==0)|(y[3:0]==6 && y[7:4]==0)|(y[3:0]==9 && y[7:4]==0)|(y[3:0]==1 && y[7:4]==1))begin
if((r[3:0]<9) && (r[7:4]<3))begin
r[3:0]<=r[3:0]+1;r[7:4]<=r[7:4];end
else if((r[3:0]==9) && (r[7:4]<3))begin
r[3:0]<=0;r[7:4]<=r[7:4]+1;end
else if((r[3:0]==0) && (r[7:4]==3))begin
r[3:0]<=1;r[7:4]<=0;end
end
else if((y[3:0]==2 && y[7:4]==0) && ((n%4)==0))begin
if((r[3:0]<9) && (r[7:4]<2))begin
r[3:0]<=r[3:0]+1;r[7:4]<=r[7:4];end
else if((r[3:0]==9) && (r[7:4]<2))begin
r[3:0]<=0;r[7:4]<=r[7:4]+1;end
else if((r[3:0]<9) && (r[7:4]==2))begin
r[3:0]<=r[3:0]+1;r[7:4]<=r[7:4];end
else if((r[3:0]==9) && (r[7:4]==2))begin
r[3:0]<=1;r[7:4]<=0;end
end
else if((y[3:0]==2 && y[7:4]==0) && ((n%4)!=0))begin
if((r[3:0]<9) && (r[7:4]<2))begin
r[3:0]<=r[3:0]+1;r[7:4]<=r[7:4];end
else if((r[3:0]==9) && (r[7:4]<2))begin
r[3:0]<=0;r[7:4]<=r[7:4]+1;end
else if((r[3:0]<8) && (r[7:4]==2))begin
r[3:0]<=r[3:0]+1;r[7:4]<=r[7:4];end
else if((r[3:0]==8) && (r[7:4]==2))begin
r[3:0]<=1;r[7:4]<=0;end
end
end
end
else if(sw2==1 && sw10==0)begin
r[3:0]<=r[3:0];r[7:4]<=r[7:4];end
end
always@(posedge clk or negedge rst_n)begin//y
if(!rst_n)begin
y[3:0]<=1;y[7:4]<=0;end
else if(sw2==0)begin
if((y[3:0]==1 && y[7:4]==0)|(y[3:0]==3 && y[7:4]==0)|(y[3:0]==5 && y[7:4]==0)|(y[3:0]==7 && y[7:4]==0)|(y[3:0]==8 && y[7:4]==0)|(y[3:0]==0 && y[7:4]==1))begin
//if((r[3:0]==1) && (r[7:4]==3) && (h[3:0]==3 && h[7:4]==2) && (m[3:0]==9 && m[7:4]==5) && (s[3:0]==9 && s[7:4]==5) && (cnt == 32'd49_999_999))begin
if((r[3:0]==1) && (r[7:4]==3) && (h[3:0]==3 && h[7:4]==2) && (m[3:0]==9 && m[7:4]==5) && (s[3:0]==9 && s[7:4]==5) && (cnt == 32'd2))begin
y[3:0]<=y[3:0]+1;y[7:4]<=y[7:4];end
end
else if(y[3:0]==2 && y[7:4]==1)begin
//if((r[3:0]==1) && (r[7:4]==3) && (h[3:0]==3 && h[7:4]==2) && (m[3:0]==9 && m[7:4]==5) && (s[3:0]==9 && s[7:4]==5) && (cnt == 32'd49_999_999))begin
if((r[3:0]==1) && (r[7:4]==3) && (h[3:0]==3 && h[7:4]==2) && (m[3:0]==9 && m[7:4]==5) && (s[3:0]==9 && s[7:4]==5) && (cnt == 32'd2))begin
y[3:0]<=1;y[7:4]<=0;end
end
else if((y[3:0]==4 && y[7:4]==0)|(y[3:0]==6 && y[7:4]==0)|(y[3:0]==9 && y[7:4]==0)|(y[3:0]==1 && y[7:4]==1))begin
//if((r[3:0]==0) && (r[7:4]==3) && (h[3:0]==3 && h[7:4]==2) && (m[3:0]==9 && m[7:4]==5) && (s[3:0]==9 && s[7:4]==5) && (cnt == 32'd49_999_999))begin
if((r[3:0]==0) && (r[7:4]==3) && (h[3:0]==3 && h[7:4]==2) && (m[3:0]==9 && m[7:4]==5) && (s[3:0]==9 && s[7:4]==5) && (cnt == 32'd2))begin
y[3:0]<=y[3:0]+1;y[7:4]<=y[7:4];end
end
else if((y[3:0]==2 && y[7:4]==0) && ((n%4)==0))begin
//if((r[3:0]==9) && (r[7:4]==2) && (h[3:0]==3 && h[7:4]==2) && (m[3:0]==9 && m[7:4]==5) && (s[3:0]==9 && s[7:4]==5) && (cnt == 32'd49_999_999))begin
if((r[3:0]==9) && (r[7:4]==2) && (h[3:0]==3 && h[7:4]==2) && (m[3:0]==9 && m[7:4]==5) && (s[3:0]==9 && s[7:4]==5) && (cnt == 32'd2))begin
y[3:0]<=y[3:0]+1;y[7:4]<=y[7:4];end
end
else if((y[3:0]==2 && y[7:4]==0) && ((n%4)!=0))begin
//if((r[3:0]==8) && (r[7:4]==2) && (h[3:0]==3 && h[7:4]==2) && (m[3:0]==9 && m[7:4]==5) && (s[3:0]==9 && s[7:4]==5) && (cnt == 32'd49_999_999))begin
if((r[3:0]==8) && (r[7:4]==2) && (h[3:0]==3 && h[7:4]==2) && (m[3:0]==9 && m[7:4]==5) && (s[3:0]==9 && s[7:4]==5) && (cnt == 32'd2))begin
y[3:0]<=y[3:0]+1;y[7:4]<=y[7:4];end
end
end
else if(sw2==1 && sw11==1)begin
if(up_flag)begin
if((y[3:0]<9) && (y[7:4]<1))begin
y[3:0]<=y[3:0]+1;y[7:4]<=y[7:4];end
else if((y[3:0]==9) && (y[7:4]<1))begin
y[3:0]<=0;y[7:4]<=y[7:4]+1;end
else if((y[3:0]<2) && (y[7:4]==1))begin
y[3:0]<=y[3:0]+1;y[7:4]<=y[7:4];end
else if((y[3:0]==2) && (y[7:4]==1))begin
y[3:0]<=1;y[7:4]<=0;end
end
end
else if(sw2==1 && sw11==0)begin
y[3:0]<=y[3:0];y[7:4]<=y[7:4];end
end
always@(posedge clk or negedge rst_n)begin//n
if(!rst_n)begin
n[3:0]<=0;n[7:4]<=0;n[11:8]<=0;n[15:12]<=2;end
else if(sw2==0)begin
//if((y[3:0]==2) && (y[7:4]==1) && (r[3:0]==1) && (r[7:4]==3) && (h[3:0]==3 && h[7:4]==2) && (m[3:0]==9 && m[7:4]==5) && (s[3:0]==9 && s[7:4]==5) && (cnt == 32'd49_999_999))begin
if((y[3:0]==2) && (y[7:4]==1) && (r[3:0]==1) && (r[7:4]==3) && (h[3:0]==3 && h[7:4]==2) && (m[3:0]==9 && m[7:4]==5) && (s[3:0]==9 && s[7:4]==5) && (cnt == 32'd2))begin
if((n[3:0]<9) && (n[7:4]<9) && (n[11:8]<9) && (n[15:12]<9))begin
n[3:0]<=n[3:0]+1;n[7:4]<=n[7:4];n[11:8]<=n[11:8];n[15:12]<=n[15:12];end
else if((n[3:0]==9) && (n[7:4]<9) && (n[11:8]<9) && (n[15:12]<9))begin
n[3:0]<=0;n[7:4]<=n[7:4]+1;n[11:8]<=n[11:8];n[15:12]<=n[15:12];end
else if((n[3:0]<9) && (n[7:4]==9) && (n[11:8]<9) && (n[15:12]<9))begin
n[3:0]<=n[3:0]+1;n[7:4]<=n[7:4];n[11:8]<=n[11:8];n[15:12]<=n[15:12];end
else if((n[3:0]==9) && (n[7:4]==9) && (n[11:8]<9) && (n[15:12]<9))begin
n[3:0]<=0;n[7:4]<=0;n[11:8]<=n[11:8]+1;n[15:12]<=n[15:12];end
else if((n[3:0]<9) && (n[7:4]<9) && (n[11:8]==9) && (n[15:12]<9))begin
n[3:0]<=n[3:0]+1;n[7:4]<=n[7:4];n[11:8]<=n[11:8];n[15:12]<=n[15:12];end
else if((n[3:0]==9) && (n[7:4]<9) && (n[11:8]==9) && (n[15:12]<9))begin
n[3:0]<=0;n[7:4]<=n[7:4]+1;n[11:8]<=n[11:8];n[15:12]<=n[15:12];end
else if((n[3:0]==9) && (n[7:4]==9) && (n[11:8]==9) && (n[15:12]<9))begin
n[3:0]<=0;n[7:4]<=0;n[11:8]<=0;n[15:12]<=n[15:12]+1;end
else if((n[3:0]==9) && (n[7:4]==9) && (n[11:8]==9) && (n[15:12]==9))begin
n[3:0]<=0;n[7:4]<=0;n[11:8]<=0;n[15:12]<=0;end
end
end
else if((sw2==1) && (sw6==1))begin
if(up_flag)begin
if(n[3:0]<9)begin
n[3:0]<=n[3:0]+1;end
else if(n[3:0]==9)begin
n[3:0]<=0;end
end
end
else if((sw2==1) && (sw7==1))begin
if(up_flag)begin
if(n[7:4]<9)begin
n[7:4]<=n[7:4]+1;end
else if(n[7:4]==9)begin
n[7:4]<=0;end
end
end
else if((sw2==1) && (sw8==1))begin
if(up_flag)begin
if(n[11:8]<9)begin
n[11:8]<=n[11:8]+1;end
else if(n[11:8]==9)begin
n[11:8]<=0;end
end
end
else if((sw2==1) && (sw9==1))begin
if(up_flag)begin
if(n[15:12]<9)begin
n[15:12]<=n[15:12]+1;end
else if(n[15:12]==9)begin
n[15:12]<=0;end
end
end
end
always @(s[3:0] or n[3:0] or sw1)begin
data_out1=7'b11111111;
if(sw1==0)begin
case (s[3:0])
4'b0000: data_out1 = 7'b1000000; 4'b0001: data_out1 = 7'b1111001;
4'b0010: data_out1 = 7'b0100100;4'b0011: data_out1 = 7'b0110000;
4'b0100: data_out1 = 7'b0011001;4'b0101: data_out1 = 7'b0010010;
4'b0110: data_out1 = 7'b0000010;4'b0111: data_out1 = 7'b1111000;
4'b1000: data_out1 = 7'b0000000;4'b1001: data_out1 = 7'b0010000;
default: data_out1 = 7'b1111111;
endcase
end
else if(sw1==1)begin
case (n[3:0])
4'b0000: data_out1 = 7'b1000000;4'b0001: data_out1 = 7'b1111001;
4'b0010: data_out1 = 7'b0100100;4'b0011: data_out1 = 7'b0110000;
4'b0100: data_out1 = 7'b0011001;4'b0101: data_out1 = 7'b0010010;
4'b0110: data_out1 = 7'b0000010;4'b0111: data_out1 = 7'b1111000;
4'b1000: data_out1 = 7'b0000000;4'b1001: data_out1 = 7'b0010000;
default: data_out1 = 7'b1111111;
endcase
end
end
always @(s[7:4] or n[7:4] or sw1)begin
data_out2=7'b11111111;
if(sw1==0)begin
case (s[7:4])
4'b0000: data_out2 = 7'b1000000;4'b0001: data_out2 = 7'b1111001;
4'b0010: data_out2 = 7'b0100100;4'b0011: data_out2 = 7'b0110000;
4'b0100: data_out2 = 7'b0011001;4'b0101: data_out2 = 7'b0010010;
4'b0110: data_out2 = 7'b0000010;4'b0111: data_out2 = 7'b1111000;
4'b1000: data_out2 = 7'b0000000;4'b1001: data_out2 = 7'b0010000;
default: data_out2 = 7'b1111111;
endcase
end
else if(sw1==1)begin
case (n[7:4])
4'b0000: data_out2 = 7'b1000000;4'b0001: data_out2 = 7'b1111001;
4'b0010: data_out2 = 7'b0100100;4'b0011: data_out2 = 7'b0110000;
4'b0100: data_out2 = 7'b0011001;4'b0101: data_out2 = 7'b0010010;
4'b0110: data_out2 = 7'b0000010;4'b0111: data_out2 = 7'b1111000;
4'b1000: data_out2 = 7'b0000000;4'b1001: data_out2 = 7'b0010000;
default: data_out2 = 7'b1111111;
endcase
end
end
always @(m[3:0] or n[11:8] or sw1)begin
data_out3=7'b11111111;
if(sw1==0)begin
case (m[3:0])
4'b0000: data_out3 = 7'b1000000;4'b0001: data_out3 = 7'b1111001;
4'b0010: data_out3 = 7'b0100100;4'b0011: data_out3 = 7'b0110000;
4'b0100: data_out3 = 7'b0011001;4'b0101: data_out3 = 7'b0010010;
4'b0110: data_out3 = 7'b0000010;4'b0111: data_out3 = 7'b1111000;
4'b1000: data_out3 = 7'b0000000;4'b1001: data_out3 = 7'b0010000;
default: data_out3 = 7'b1111111;
endcase
end
else if(sw1==1)begin
case (n[11:8])
4'b0000: data_out3 = 7'b1000000;4'b0001: data_out3 = 7'b1111001;
4'b0010: data_out3 = 7'b0100100;4'b0011: data_out3 = 7'b0110000;
4'b0100: data_out3 = 7'b0011001;4'b0101: data_out3 = 7'b0010010;
4'b0110: data_out3 = 7'b0000010;4'b0111: data_out3 = 7'b1111000;
4'b1000: data_out3 = 7'b0000000;4'b1001: data_out3 = 7'b0010000;
default: data_out3 = 7'b1111111;
endcase
end
end
always @(m[7:4] or n[15:12] or sw1)begin
data_out4=7'b11111111;
if(sw1==0)begin
case (m[7:4])
4'b0000: data_out4 = 7'b1000000;4'b0001: data_out4 = 7'b1111001;
4'b0010: data_out4 = 7'b0100100;4'b0011: data_out4 = 7'b0110000;
4'b0100: data_out4 = 7'b0011001;4'b0101: data_out4 = 7'b0010010;
4'b0110: data_out4 = 7'b0000010;4'b0111: data_out4 = 7'b1111000;
4'b1000: data_out4 = 7'b0000000;4'b1001: data_out4 = 7'b0010000;
default: data_out4 = 7'b1111111;
endcase
end
else if(sw1==1)begin
case (n[15:12])
4'b0000: data_out4 = 7'b1000000;4'b0001: data_out4 = 7'b1111001;
4'b0010: data_out4 = 7'b0100100;4'b0011: data_out4 = 7'b0110000;
4'b0100: data_out4 = 7'b0011001;4'b0101: data_out4 = 7'b0010010;
4'b0110: data_out4 = 7'b0000010;4'b0111: data_out4 = 7'b1111000;
4'b1000: data_out4 = 7'b0000000;4'b1001: data_out4 = 7'b0010000;
default: data_out4 = 7'b1111111;
endcase
end
end
always @(h[3:0] or r[3:0] or sw1)begin
data_out5=7'b11111111;
if(sw1==0)begin
case (h[3:0])
4'b0000: data_out5 = 7'b1000000;4'b0001: data_out5 = 7'b1111001;
4'b0010: data_out5 = 7'b0100100;4'b0011: data_out5 = 7'b0110000;
4'b0100: data_out5 = 7'b0011001;4'b0101: data_out5 = 7'b0010010;
4'b0110: data_out5 = 7'b0000010;4'b0111: data_out5 = 7'b1111000;
4'b1000: data_out5 = 7'b0000000;4'b1001: data_out5 = 7'b0010000;
default: data_out5 = 7'b1111111;
endcase
end
else if(sw1==1)begin
case (r[3:0])
4'b0000: data_out5 = 7'b1000000;4'b0001: data_out5 = 7'b1111001;
4'b0010: data_out5 = 7'b0100100;4'b0011: data_out5 = 7'b0110000;
4'b0100: data_out5 = 7'b0011001;4'b0101: data_out5 = 7'b0010010;
4'b0110: data_out5 = 7'b0000010;4'b0111: data_out5 = 7'b1111000;
4'b1000: data_out5 = 7'b0000000;4'b1001: data_out5 = 7'b0010000;
default: data_out5 = 7'b1111111;
endcase
end
end
always @(h[7:4] or r[7:4] or sw1)begin
data_out6=7'b11111111;
if(sw1==0)begin
case (h[7:4])
4'b0000: data_out6 = 7'b1000000;4'b0001: data_out6 = 7'b1111001;
4'b0010: data_out6 = 7'b0100100;4'b0011: data_out6 = 7'b0110000;
4'b0100: data_out6 = 7'b0011001;4'b0101: data_out6 = 7'b0010010;
4'b0110: data_out6 = 7'b0000010;4'b0111: data_out6 = 7'b1111000;
4'b1000: data_out6 = 7'b0000000;4'b1001: data_out6 = 7'b0010000;
default: data_out6 = 7'b1111111;
endcase
end
else if(sw1==1)begin
case (r[7:4])
4'b0000: data_out6 = 7'b1000000;4'b0001: data_out6 = 7'b1111001;
4'b0010: data_out6 = 7'b0100100;4'b0011: data_out6 = 7'b0110000;
4'b0100: data_out6 = 7'b0011001;4'b0101: data_out6 = 7'b0010010;
4'b0110: data_out6 = 7'b0000010;4'b0111: data_out6 = 7'b1111000;
4'b1000: data_out6 = 7'b0000000;4'b1001: data_out6 = 7'b0010000;
default: data_out6 = 7'b1111111;
endcase
end
end
always @(y[3:0] or sw1)begin
data_out7=7'b11111111;
if(sw1==1)begin
case (y[3:0])
4'b0000: data_out7 = 7'b1000000;4'b0001: data_out7 = 7'b1111001;
4'b0010: data_out7 = 7'b0100100;4'b0011: data_out7 = 7'b0110000;
4'b0100: data_out7 = 7'b0011001;4'b0101: data_out7 = 7'b0010010;
4'b0110: data_out7 = 7'b0000010;4'b0111: data_out7 = 7'b1111000;
4'b1000: data_out7 = 7'b0000000;4'b1001: data_out7 = 7'b0010000;
default: data_out7 = 7'b1111111;
endcase
end
end
always @(y[7:4] or sw1)begin
data_out8=7'b11111111;
if(sw1==1)begin
case (y[7:4])
4'b0000: data_out8 = 7'b1000000;4'b0001: data_out8 = 7'b1111001;
4'b0010: data_out8 = 7'b0100100;4'b0011: data_out8 = 7'b0110000;
4'b0100: data_out8 = 7'b0011001;4'b0101: data_out8 = 7'b0010010;
4'b0110: data_out8 = 7'b0000010;4'b0111: data_out8 = 7'b1111000;
4'b1000: data_out8 = 7'b0000000;4'b1001: data_out8 = 7'b0010000;
default: data_out8 = 7'b1111111;
endcase
end
end
key_debounce u1_key_debounce(
.sys_clk (clk),
.sys_rst_n (rst_n),
.key (key_plus),
.key_flag (key_plus_flag),
.key_value (key_plus_value)
);
key_debounce u2_key_debounce(
.sys_clk (clk),
.sys_rst_n (rst_n),
.key (key_sub),
.key_flag (key_sub_flag),
.key_value (key_sub_value)
);
endmodule文章来源地址https://www.toymoban.com/news/detail-760964.html
按键消抖:
//***************************************************************************//
module key_debounce(
input sys_clk, //外部50M时钟
input sys_rst_n, //外部复位信号,低有效
input key, //外部按键输入
output reg key_flag, //按键数据有效信号
output reg key_value //按键消抖后的数据
);
reg [31:0] delay_cnt;reg key_reg;
always @(posedge sys_clk or negedge sys_rst_n) begin
if (!sys_rst_n) begin
key_reg <= 1'b1;delay_cnt <= 32'd0;end
else begin
key_reg <= key;
if(key_reg != key)begin //一旦检测到按键状态发生变化(有按键被按下或释放)
delay_cnt <= 32'd1000000; //给延时计数器重新装载初始值(计数时间为20ms)
//delay_cnt <= 32'd10; //给延时计数器重新装载初始值(计数时间为20ms)//仿真时改为10
end
else if(key_reg == key) begin //在按键状态稳定时,计数器递减,开始20ms倒计时
if(delay_cnt > 32'd0)begin
delay_cnt <= delay_cnt - 1'b1;end
else begin
delay_cnt <= delay_cnt;end
end
end
end
always @(posedge sys_clk or negedge sys_rst_n) begin
if (!sys_rst_n) begin
key_flag <= 1'b0;key_value <= 1'b1;end
else begin
if(delay_cnt == 32'd1) begin //当计数器递减到1时,说明按键稳定状态维持了20ms
key_flag <= 1'b1; //此时消抖过程结束,给出一个时钟周期的标志信号
key_value <= key; //并寄存此时按键的值
end
else begin
key_flag <= 1'b0;key_value <= key_value;end
end
end
endmodule
仿真代码:
`timescale 1ns/1ns
module tb;
reg clk;reg rst_n;
reg sw2;reg sw3;
reg sw4;reg sw5;
reg sw6;reg sw7;
reg sw8;reg sw9;
reg sw10;reg sw11;
reg key_plus;reg key_sub;
reg [4:0] i;wire [7:0] s;
wire [7:0] m;wire [7:0] h;
wire [7:0] r;wire [7:0] y;
wire [15:0] n;
initial begin
i=0;clk=0;
rst_n=1;key_plus=1;
key_sub=1;
#20
rst_n=0;
#20
rst_n=1;
#20
sw2=1;
#20
sw3=1;
#20
sw3=0;sw4=1;
#20
sw4=0;sw5=1;
#20
sw5=0;sw6=1;
#20
sw6=0;sw7=1;
#20
sw7=0;sw8=1;
#20
sw8=0;sw9=1;
#20
sw9=0;sw10=1;
#20
sw10=0;sw11=1;
#20
sw11=0;
#20
sw2=0;
repeat(3)begin
key_plus=1;
for(i=0;i<10;i=i+1)
#20
key_plus=~key_plus;key_plus=0;
#1000
key_plus=1;
for(i=0;i<10;i=i+1)
#20
key_plus=~key_plus;key_plus=1;
end
#1000
key_sub=1;
repeat(3)begin
for(i=0;i<10;i=i+1)
#20
key_sub=~key_sub;key_sub=0;
#1000
key_sub=1;
for(i=0;i<10;i=i+1)
#20
key_sub=~key_sub;key_sub=1;
#1000
key_sub=0;end
#10000
$stop;
end
always #10 clk=~clk;
wnl u1(
.clk(clk),
.rst_n(rst_n),
.s(s),
.m(m),
.h(h),
.r(r),
.y(y),
.n(n),
.key_plus(key_plus),
.key_sub(key_sub),
.sw2(sw2),
.sw3(sw3),//s
.sw4(sw4),//m
.sw5(sw5),//h
.sw6(sw6),//n[3:0]
.sw7(sw7),//n[7:4]
.sw8(sw8),//n[11:8]
.sw9(sw9),//n[15:12]
.sw10(sw10),//r
.sw11(sw11)//y
);
endmodule
-
实验结果:
- 实验时间为2021年12月21日18时42分12秒
到了这里,关于FPGA开发(基于Quartus II)万年历,简单代码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!