(1)掌握时分秒电子钟的设计方法
(2)明白时分秒电子钟的组成原理及工作原理
(3)通过综合性实验项目的设计与实现,进一步加深理论教学与实验软硬件平台的实践训练,为设计性实验做好充分准备。
(1)准确计时,以数字形式显示时、分、秒的时间。
(2)小时的计时以“23翻0”的形式。
(3)增添显示星期功能。
(4)增添闹钟功能,闹钟时间由使用者自己设置。
module clock(h_alarm1,h_alarm2,m_alarm1,m_alarm2,s_alarm1,s_alarm2,data,hour1,hour2,minute1,minute2,sec1,sec2,reset,sign1,sign2,sign3,sign4,sign5,sign6,clk,alarm,adapt1,adapt2,adapt3,adapt4,adapt5,adapt6);
input reset,clk,adapt1,adapt2,adapt3,adapt4,adapt5,adapt6;
//输入reset清零端,clk脉冲
//adapt1~6设置闹钟时分秒的高低位
output [4:1]sec2,minute2,hour2,h_alarm2,s_alarm2,m_alarm2;
output [3:1]sec1,minute1,hour1,data,h_alarm1,s_alarm1,m_alarm1;
//输出时分秒的高低位,星期
//输出设置的闹钟时间
output sign1,sign2,sign3,sign4,sign5,sign6,alarm;
//sign1~6进位判断,alarm闹钟信号显示
reg [4:1]sec2,minute2,hour2,h_alarm2,s_alarm2,m_alarm2;
reg [3:1]sec1,minute1,hour1,data,h_alarm1,s_alarm1,m_alarm1;
wire sign1,sign2,sign3,sign4,alarm,sign5,sign6;
assign sign5=(sec2==4'b1001)?1:0;
//秒的低位满9向上进位信号
assign sign6=((sec1==3'b101)&&sign5)?1:0;
//下级传来进位信号后,秒的高位向上进位的信号
assign sign1=((minute2==4'b1001)&&sign6)?1:0;
//分的低位向上进位信号
assign sign2=((minute1==3'b101)&&sign1)?1:0;
//分的高位向上进位信号
assign sign3=(((hour2==4'b1001)||((hour1==3'b010)&&(hour2==4'b0011)))&&sign2)?1:0;
//时的低位向上进位信号(此时有两种情况①hour2满9进位,②23小时的情况下进位)
assign sign4=((hour1==3'b010)&&sign3)?1:0;
//时的高位向上进位信号
assign alarm=((hour1==h_alarm1)&&(hour2==h_alarm2)&&(minute1==m_alarm1)&&(minute2==m_alarm2)&&(sec1==s_alarm1)&&(sec2==s_alarm2))?1:0;
//判断是否为闹钟时间?若是,则发送闹钟信号alarm=1
//对sec2(秒低位)进行的操作
always @(posedge clk)
begin
if(reset)//高有效,清零
sec2<=4'b0000;
else if(sec2==4'b1001)//如果满9,sec2清零
sec2<=4'b0000;
else sec2<=sec2+1;//否则+1
end
//对sec1(秒高位)进行的操作
always @(posedge clk)
begin
if(reset)
sec1<=3'b000;
else if(sign5)//秒低位传来进位信号
if(sec1==3'b101)//若此时秒高位=5,则清零,反之则+1
sec1<=3'b000;
else sec1<=sec1+1;
end
//对min2(分低位)进行的操作
always @ (posedge clk)
begin
if(reset)
minute2<=4'b0000;
else if(sign6)//秒高位传来进位信号
if(minute2==4'b1001)//若此时分低位=9,则清零,反之则+1
minute2<=4'b0000;
else
minute2<=minute2+1;
end
//对min1(分高位)进行的操作
always @ (posedge clk)
begin
if(reset)
minute1<=3'b000;
else if(sign1)//分低位传来进位信号
if(minute1==3'b101)//若此时分高位=5,则清零,反之则+1
minute1<=3'b000;
else
minute1<=minute1+1;
end
//对hour2(时低位)进行的操作
always @ (posedge clk)
begin
if(reset)
hour2<=4'b0000;
else if(sign2) //分高位传来进位信号
case(hour1)//按照hour1(时高位)来分情况讨论
3'b000://时高位为0时
begin
if(hour2==4'b1001)//若时低位满9,则清零,反之+1
hour2<=4'b0000;
else
hour2<=hour2+1;
end
3'b001://时高位为1时
begin
if(hour2==4'b1001)//若时低位满9,则清零,反之+1
hour2<=4'b0000;
else
hour2<=hour2+1;
end
3'b010://时高位为2时
begin
if(hour2==4'b0011)//若时低位满3,则清零,反之+1
hour2<=4'b0000;
else
hour2<=hour2+1;
end
endcase
end
//对hour1(时高位)进行的操作
always @ (posedge clk)
begin
if(reset)
hour1<=3'b000;
else if(sign3)//时低位传来进位信号
if(hour1==3'b010)//若时高位满2,则清零,反之+1
hour1<=3'b000;
else
hour1<=hour1+1;
end
//对data(星期)进行的操作
always @ (posedge clk)
begin
if(reset)//清零时,data初始化为星期一
data<=3'b001;
else if(sign4)//时高位传来进位信号
if(data==3'b111)//若此时data为星期日,则置为星期一,反之+1
data<=3'b001;
else
data<=data+1;
end
//根据传来的adapt1信号,设置h_alarm1(闹钟时高位)的数值
always @ (posedge clk)
begin
if(adapt1&&(h_alarm1==3'b010))
//传来adapt1信号,当此时闹钟时高位为2时,将闹钟的时高位数值置为0,反之继续+1
h_alarm1<=3'b000;
else if(adapt1)
h_alarm1<=h_alarm1+1;
end
//根据传来的adapt2信号,设置h_alarm2(闹钟时低位)的数值
always @ (posedge clk)
begin
if(adapt2&&(h_alarm2==4'b1001))
//传来adapt2信号,当此时闹钟时低位为9时,将闹钟的时低位数值置为0,反之继续+1
h_alarm2<=4'b0000;
else if(adapt2)
h_alarm2<=h_alarm2+1;
end
//根据传来的adapt3信号,设置m_alarm1(闹钟分高位)的数值
always @ (posedge clk)
begin
if(adapt3&&(m_alarm1==3'b101))
//传来adapt3信号,当此时闹钟分高位为5时,将闹钟的分高位数值置为0,反之继续+1
m_alarm1<=3'b000;
else if(adapt3)
m_alarm1<=m_alarm1+1;
end
//根据传来的adapt4信号,设置m_alarm2(闹钟分低位)的数值
always @ (posedge clk)
begin
if(adapt4&&(m_alarm2==4'b1001))
//传来adapt4信号,当此时闹钟分低位为9时,将闹钟的分低位数值置为0,反之继续+1
m_alarm2<=4'b0000;
else if(adapt4)
m_alarm2<=m_alarm2+1;
end
//根据传来的adapt5信号,设置s_alarm1(闹钟秒高位)的数值
always @(posedge clk)
begin
if(adapt5&&(s_alarm1==3'b101))
//传来adapt5信号,当此时闹钟秒高位为5时,将闹钟的秒高位数值置为0,反之继续+1
s_alarm1<=3'b000;
else if(adapt5)
s_alarm1<=s_alarm1+1;
end
//根据传来的adapt6信号,设置s_alarm2(闹钟秒低位)的数值
always @(posedge clk)
begin
if(adapt6&&(s_alarm2==4'b1001))
//传来adapt6信号,当此时闹钟秒低位为9时,将闹钟的秒低位数值置为0,反之继续+1
s_alarm2<=4'b0000;
else if(adapt6)
s_alarm2<=s_alarm2+1;
end
endmodule
图略
由图1可以看出sec2->sec1->minute2进位成功,星期data=1。在输入时设置了两个脉冲的adapt6(闹钟秒低位)和一个脉冲的adapt5(闹钟秒高位),即设置闹钟为第12秒,图1中alarm在第12秒时显示出波形变化。
由图2可以看出minute2->minute1->hour2进位成功。
由图3可以看出hour2->hour1->data进位成功。
由图4可以看出实现星期功能,每经过一天data+1,第八天时data重置为1。
本次实验中,我完成了基本计时,显示星期,设置闹钟这三个功能。功能实现正常可行,但我觉得仍然有需要改进的地方。在设置闹钟时间时定义了多个变量,虽然在调控闹钟时可以做到清晰明了,但从时钟的实际应用方面会很浪费成本,弊大于利。
这次的专题实验将平时的理论性知识活学活用,不但加深了我对时钟工作原理的理解,也激发了我的创造力,让我意识到设计器件要从多方面综合考虑,尤其是要考虑到时钟的实用性。