fdiv28_31.v
来自「用verlog HDL写的电子日历,可以显示年,月,日和时间,具有闹铃的功能」· Verilog 代码 · 共 68 行
V
68 行
//=====对输入时钟28或31分频,如1d->1mon(月)=====//
// 采用if-else语句,比用case语句简洁!
module fdiv28_31(clki,clr,month,leapyear,clko,cnt);
input clki,clr,leapyear; // leapyear是闰年信号
input[7:0] month;
output clko,cnt;
reg clko;
reg[7:0] cnt;
always @(posedge clki or posedge clr )
begin
if(clr) // 异步清零
cnt<=0;
else
begin
// (1)当为二月时
if(month==8'h2)
begin
if(leapyear==1) // ----若为闰年,二月有29天
begin
if(cnt>=8'h29)
cnt<=1; // 初值为1,而不是0
else if(cnt[3:0]==9)
cnt<=cnt+7;
else
cnt<=cnt+1;
clko<=(cnt==8'h29); // 当cnt从29变为1时,产生月进位信号
end
else // ----若不为闰年,二月只有28天
begin
if(cnt>=8'h28)
cnt<=1; // 初值为1,而不是0
else if(cnt[3:0]==9)
cnt<=cnt+7;
else
cnt<=cnt+1;
clko<=(cnt==8'h28);
end
end
//(2)4、6、9、11月每月有30天
else if((month==8'h4)||(month==8'h6) ||(month==8'h9) ||(month==8'h11))
begin
if(cnt>=8'h30)
cnt<=1;
else if(cnt[3:0]==9)
cnt<=cnt+7;
else
cnt<=cnt+1;
clko<=(cnt==8'h30);
end
//(3)1、3、5、7、8、10、12月每月有31天
else
begin
clko<=(cnt==8'h31);
if(cnt>=8'h31)
cnt<=1;
else if(cnt[3:0]==9)
cnt<=cnt+7;
else
cnt<=cnt+1;
end
end
end
endmodule
// 采用if-else语句,可将month等于多个值用逻辑或放在if后面的括号中,比用case语句简洁!
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?