module ctl( clk,S1,S2,S3,S4,RST,Hour1,Minute1,Second1,Hour2,Minute2,Second2,LED );
input clk,S1,S2,S3,S4,RST;
//CPout=50M,Cpout=1kHz,S1和S2分别用来调整小时和分钟,RST用来进行复位
output [3:0] Hour1,Hour2; //小时显示数
output [3:0] Minute1,Minute2;//分显示数
output [3:0] Second1,Second2;//秒显示数
output LED;//整点报时灯
reg [3:0] Hour11,Hour21;
reg [3:0] Minute11,Minute21;
reg [3:0] Second11,Second21;
reg LED1;
reg S1_r,S1_n,S2_r,S2_n,RST_r,RST_n;//用于按键去抖动
reg S3_r,S3_n,S4_r,S4_n;
reg [19:0] cnt; //按键去抖动计数用
reg [25:0] Cout;
reg Clk_En;//用于分频,产生1HZ的记时时钟
always @ (posedge clk)
cnt <= cnt+1'b1; //循环计数
always @(posedge clk)
if(cnt==20'hfffff)
begin
S1_n<=S1;
S2_n<=S2;
RST_n<=RST;
S3_n<=S3;
S4_n<=S4;
end
always @(posedge clk)
begin
S1_r<=S1_n;
S2_r<=S2_n;
RST_r<=RST_n;
S3_r<=S3_n;
S4_r<=S4_n;
end
wire S1_f=(~S1_n)&S1_r;
wire S2_f=(~S2_n)&S2_r;
wire S3_f=(~S3_n)&S3_r;
wire S4_f=(~S4_n)&S4_r;
wire RST_f=(~RST_n)&RST_r;//按键去抖动所用
always@(posedge clk)
begin
if(S1_f==1)//对小时的控制
begin
if((Hour11<4'b1001)&&(Hour21<4'b0010))
begin
Hour11<=Hour11+4'b0001;
end
else if((Hour11<4'b0011)&&(Hour21==4'b0010))
Hour11<=Hour11+4'b0001;
else
begin
Hour11<=4'b0000;
if(Hour21<4'b0010)
Hour21<=Hour21+4'b0001;
else
begin
Hour21<=4'b0000;
end
end
end
if(S3_f==1)//对小时的控制
begin
if(Hour11>4'b0000)
begin
Hour11<=Hour11-4'b0001;
end
else if((Hour11==4'b0000)&&(Hour21>4'b0000))
begin
Hour11<=4'b1001;
Hour21<=Hour2-4'b0001;
end
else
begin
Hour11<=4'b0011;
Hour21<=4'b0010;
end
end
if(S2_f==1)//对分钟的控制
begin
if(Minute11<4'b1001)
Minute11<=Minute11+4'b0001;
else
begin
Minute11<=4'b0000;
if(Minute21<4'b0101)
Minute21<=Minute21+4'b0001;
else
Minute21<=4'b0000;
end
end
if(S4_f==1)//对分钟的控制
begin
if(Minute11>4'b0000)
Minute11<=Minute11-4'b0001;
else
if((Minute21>4'b0000)&&(Minute11==4'b0000))
begin
Minute21<=Minute21-4'b0001;
Minute11<=4'b1001;
end
else
begin
Minute21<=4'b0101;
Minute11<=4'b1001;
end
end
if(RST_f==1)//清零
begin
Hour11<=4'b0000;
Minute11<=4'b0000;
Second11<=4'b0000;
Hour21<=4'b0000;
Minute21<=4'b0000;
Second21<=4'b0000;
end
Cout<=(Cout==26'd5000_0000)?26'd0:(Cout + 26'd1);
Clk_En<=(Cout==26'd5000_0000)?1'b1:1'b0;//分频,产生1Hz的时钟信号
if(Clk_En)
begin
begin
if(Second11<4'b1001)//时间自动运行
begin
Second11<=Second11+4'b0001;
end
else
begin
Second11<=4'b0000;
if(Second21<4'b0101)
begin
Second21<=Second21+4'b0001;
end
else
begin
Second21<=4'b0000;
end
end
end
begin
if((Second21==4'b0101)&&(Second11==4'b1001))
begin
if(Minute11<4'b1001)
begin
Minute11<=Minute11+4'b0001;
end
else
begin
Minute11<=4'b0000;
if(Minute21<4'b0101)
begin
Minute21<=Minute21+4'b0001;
end
else
begin
Minute21<=4'b0000;
end
end
end
end
begin
if((Second21==4'b0101)&&(Second11==4'b1001)&&(Minute21==4'b0101)&&(Minute11==4'b1001))
begin
if((Hour11<4'b1001)&&(Hour21<4'b0010))
begin
Hour11<=Hour11+4'b0001;
end
else if((Hour11<4'b0011)&&(Hour21==4'b0010))
Hour11<=Hour11+4'b0001;
else
begin
Hour11<=4'b0000;
if(Hour21<4'b0010)
Hour21<=Hour21+4'b0001;
else
Hour21<=4'b0000;
end
end
end
if((Minute21==4'b0101)&&(Minute11==4'b1001)&&(Second11>4'b0101)&&(Second21==4'b0101))//整点倒计时
begin
if(LED1==1)
LED1<=0;
else
LED1<=1;
end
else
LED1<=0;
end
end
assign Second1=Second11;
assign Second2=Second21;
assign Minute1=Minute11;
assign Minute2=Minute21;
assign Hour1=Hour11;
assign Hour2=Hour21;
assign LED=LED1;
endmodule
|