?? dianzirili.v.bak
字號:
/* 信號定義:
GW48 SOPC系統(GW48-PK2)采用模式5
clk: 標準時鐘信號,本例中,其頻率為4Hz,接179引腳;
clk_1k: 產生報時音的時鐘信號,本例中其頻率為1024Hz,接177引腳;
mode: 功能控制信號; 為0:計時功能;
為1:調星期功能;
為2:手動校時功能;
為3:調日期功能;
為4:日期顯示功能;接236引腳,按鍵4;
turn: 接按鍵,在手動校時功能時,選擇是調整小時,還是分鐘;
在調日期時,選擇是調整日,還是月.接237引腳,按鍵5;
若長時間按住該鍵,還可使秒信號清零,用于精確調時;
change:接按鍵,手動調整時,每按一次,計數器加1.接235引腳,按鍵3
reset: 復位信號,接238引腳,按鍵6
hour,min,sec:此三信號分別輸出并顯示時、分、秒信號, 皆采用BCD碼計數,分別驅動1~6六個
數碼管顯示時間;min接引腳21,41,128,132~136;hour接引腳137~141,158~160;驅動數碼管5、6亮
驅動數碼管3、4亮;sec接引腳13~20;驅動數碼管1、2亮
week:為輸出星期的信號,采用BCD碼計數;驅動數碼管8,接引腳165~168
alert: 輸出到揚聲器的信號,用于報時音,接引腳174;
LD_hour: 接發光二極管,指示當前調整的是小時信號,接引腳1;
LD_min: 接發光二極管,指示當前調整的是分鐘信號。接引腳2;
LD_mon: 接發光二極管,指示當前調整的是月份信號.接引腳3;
LD_day: 接發光二極管,指示當前調整的是日子信號.接引腳4;
LD_week: 接發光二極管,指示當前調整的是星期信號.接引腳6;
*/
module clock(clk,clk_1k,mode,change,turn,reset,alert,week,
hour,min,sec, LD_hour,LD_min,LD_mon,LD_day,LD_week);
input clk,clk_1k,mode,change,turn,reset;
output alert,LD_hour,LD_min,LD_mon,LD_day,LD_week;
output[7:0] hour,min,sec;
output[3:0] week;
reg [3:0] week,week1;
reg[7:0] hour,min,sec,mon1,day1,hour1,min1,sec1;
reg[1:0] fm,num1,num2,num3,num4,num5;
reg[2:0] m,wm;
reg[1:0] loop1,loop2,loop3,loop4,loop5,sound;
reg LD_hour,LD_min,LD_mon,LD_day,LD_week;
reg clk_1hz,clk_2hz,minclk,hclk,dclk,monclk,wclk;
reg alert1,alert2,ear;
reg count1,count2,count3,count4,count5;
wire ct1,ct2,ct3,ct4,ct5,m_clk,h_clk,d_clk,mon_clk,w_clk;
always @(posedge clk)
begin clk_2hz<=~clk_2hz;//分頻為2HZ
if(sound==3) begin sound<=0; ear<=1; end
//ear信號用于產生或屏蔽聲音
else begin sound<=sound+1; ear<=0; end
end
always@(posedge clk_2hz)//分頻為1HZ
clk_1hz<=~clk_1hz;
always @(posedge mode) //mode信號控制系統在五種功能間轉換
begin if(m==4) m<=0; else m<=m+1; end
always @(posedge turn)
fm<=~fm;
always //該進程產生count1,count2,count3,count4,count5信號
begin
case(m)
3:begin if(fm)
begin count3<=change;{LD_mon,LD_day}<=1;end //調日
else begin count4<=change;{LD_mon,LD_day}<=2;end //調月
{count1,count2,count5}<=0;
end
2: begin if(fm) //調時間
begin count1<=change; {LD_min,LD_hour}<=2; end //調分
else begin count2<=change; {LD_min,LD_hour}<=1; end//調時
{count3,count4,count5}<=0;
end
1: begin if(fm)
begin count5<=change;LD_week<=1;end //調星期
else {count5,LD_week}<=0;
{count1,count2,count3,count4}<=0;
end
default: {count1,count2,count3,count4,count5,
LD_min,LD_hour,LD_mon,LD_day,LD_week}<=0;
endcase
end
always @(negedge clk)
//生成"num1"信號用于連續加1
if(count1) begin
if(loop1==3) num1<=1;
else
begin loop1<=loop1+1; num1<=0; end
end
else begin loop1<=0; num1<=0; end
always @(negedge clk) //產生num2信號
if(count2) begin
if(loop2==3) num2<=1;
else
begin loop2<=loop2+1; num2<=0; end
end
else begin loop2<=0; num2<=0; end
always @(negedge clk)
if(count3) begin
if(loop3==3) num3<=1;
else
begin loop3<=loop3+1; num3<=0; end
end
else begin loop3<=0; num3<=0; end
always @(negedge clk)
if(count4) begin
if(loop4==3) num4<=1;
else
begin loop4<=loop4+1; num4<=0; end
end
else begin loop4<=0; num4<=0; end
always @(negedge clk)
if(count5) begin
if(loop5==3) num5<=1;
else
begin loop5<=loop5+1; num5<=0; end
end
else begin loop5<=0; num5<=0; end
assign ct1=(num1&clk)|(!num1&m_clk); //ct1用于計時、校時中的分鐘計數
assign ct2=(num2&clk)|(!num2&h_clk); //ct2用于計時、校時中的小時計數
assign ct3=(num3&clk)|(!num3&d_clk); //ct3用于日期與調日期中的日計數
assign ct4=(num4&clk)|(!num4&mon_clk); //ct4用于日期與調日期中的月計數
assign ct5=(num5&clk)|(!num5&w_clk); //ct5用于星期與調星期中的星期計數
always @(posedge clk_1hz or posedge reset) //秒計時和秒調整進程
if(reset)
begin sec1=0;end
else begin
if(!(sec1^8'h59)|turn&(!m))
begin
sec1<=0; if(!(turn&(!m))) minclk<=1;
end
//按住"turn"按鍵一段時間,秒信號可清零,該功能用于手動精確調時
else begin
if(sec1[3:0]==4'b1001)
begin sec1[3:0]<=4'b0000; sec1[7:4]<=sec1[7:4]+1; end
else sec1[3:0]<=sec1[3:0]+1; minclk<=0;
end
end
assign m_clk=minclk||count1;
always @(posedge ct1 or posedge reset) //分計時和分調整進程
if(reset)
begin min1=0;end
else
begin
if(min1==8'h59) begin min1<=0; hclk<=1; end
else begin
if(min1[3:0]==4'h9)
begin min1[3:0]<=0; min1[7:4]<=min1[7:4]+1; end
else min1[3:0]<=min1[3:0]+1; hclk<=0;
end
end
assign h_clk=hclk||count2;
always @(posedge ct2 or posedge reset)
if(reset)
begin hour1=0;end
else
begin //小時計時和小時調整進程
if(hour1==8'h23) begin hour1<=0; dclk<=1; end
else begin
if(hour1[3:0]==9)
begin hour1[7:4]<=hour1[7:4]+1; hour1[3:0]<=0; end
else hour1[3:0]<=hour1[3:0]+1; dclk<=0;
end
end
assign d_clk=dclk||count3;
always@(posedge ct3 or posedge reset) //日計時和日調整進程
if(reset)
begin day1=1;end
else
begin case(mon1)
8'h2:begin if(day1==8'h28)
begin day1<=8'h1;monclk<=1;end
else begin
if(day1[3:0]==4'h9)
begin day1[7:4]<=day1[7:4]+1;day1[3:0]<=0;end
else day1[3:0]<=day1[3:0]+1;monclk<=0;
end
end
8'h4,8'h6,8'h9,8'h11:begin if(day1==8'h30)
begin day1<=1;monclk<=1;end
else begin
if(day1[3:0]==4'h9)
begin day1[7:4]<=day1[7:4]+1;day1[3:0]<=0;end
else day1[3:0]<=day1[3:0]+1;monclk<=0;
end
end
default:begin if(day1==8'h31)
begin day1<=1;monclk<=1;end
else begin
if(day1[3:0]==4'h9)
begin day1[7:4]<=day1[7:4]+1;day1[3:0]<=0;end
else day1[3:0]<=day1[3:0]+1;monclk<=0;
end
end
endcase
end
assign mon_clk=monclk||count4;
always@(posedge ct4 or posedge reset) //月計時和月調整進程
if(reset) mon1=1;
else
begin
if(mon1==8'h12)
begin mon1<=1;end
else begin
if(mon1[3:0]==4'h9)
begin mon1[7:4]<=mon1[7:4]+1;mon1[3:0]<=0;end
else mon1[3:0]<=mon1[3:0]+1;
end
end
always@(posedge ct3 or posedge reset) //產生星期時鐘
if(reset)
wm=0;
else begin
if(wm==6) begin
wm<=0;wclk<=1;
end
else
wm<=wm+1;
end
assign w_clk=wclk||count5;
always@(posedge ct5 or posedge reset) //星期計時和星期調整進程
if(reset)
week1=1;
else begin
if (week1==4'h7)
week1<=1;
else week1<=week1+1;
end
always //時、分、秒、日期的顯示控制
case(m)
3'b000: begin week<=week1;hour<=hour1; min<=min1; sec<=sec1; end
//計時狀態下的時、分、秒顯示
3'b010: begin week<=0;hour<=hour1; min<=min1; sec<=8'hzz; end
//校時狀態下的時、分、秒顯示
3'b001: begin week<=week1;hour<=0;min<=0;sec<=0;end //調星期的顯示
3'b011: begin min<=mon1;sec<=day1; end //調日期的顯示
3'b100: begin hour<=mon1;min<=0;sec<=day1;week<=week1;end //日期顯示
endcase
assign alert=((alert1)?clk_1k&clk:0)|alert2; //產生鬧鈴音或整點報時音
always //產生整點報時信號alert2
begin
if((min1==8'h59)&&(sec1>8'h54)||(!(min1|sec1)))
if(sec1>8'h54) alert2<=ear&clk_1k; //產生短音
else alert2<=!ear&clk_1k; //產生長音
else alert2<=0;
end
endmodule
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -