基于FPGA的数字电子时钟设计与实现 下载本文

内容发布更新时间 : 2024/5/20 7:37:57星期一 下面是文章的全部内容请认真阅读。

桂林电子科技大学课程设计(论文)报告用纸 第 10 页 共 18 页

assign LED7=({Minute,Second}==16'h5957);//定义LED7为59分57秒时灯亮 assign LED8=({Minute,Second}==16'h5958);//定义LED8为59分58秒时灯亮 assign LED9=({Minute,Second}==16'h5959);//定义LED9为59分59秒时灯亮 from0to9 U10(HEX0,Second[3:0]); //个位秒调用译码 from0to9 U9(HEX1,Second[7:4]); //十位秒调用译码 from0to9 U3(HEX2,Minute[3:0]); //个位分调用译码 from0to9 U4(HEX3,Minute[7:4]); //个位分调用译码 from0to9 U5(HEX4,Hour[3:0]); //个位时调用译码 from0to9 U6(HEX5,Hour[7:4]); //十位时调用译码 endmodule

3.2.2 counter24程序

module counter24(CntH,CntL,nCR,EN,CP); input CP,nCR,EN;

output [3:0] CntH,CntL; reg [3:0] CntH,CntL;

always @(posedge CP or negedge nCR) begin

if(~nCR) {CntH,CntL}<=8'h00;//当nCR=0时,计时器清零

else if(~EN) {CntH,CntL}<={CntH,CntL};//当EN=0时,停止计时,保持 else if((CntH>2)||(CntH>9)||((CntH==2)&&(CntL>=3))) {CntH,CntL}<=8'h00; else if((CntH==2)&&(CntL<3))

begin CntH<=CntH;CntL<=CntL+1'b1;end else if(CntL==9)

begin CntH<=CntH+1'b1;CntL<=4'b0000;end else

begin CntH<=CntH;CntL<=CntL+1'b1;end end

Endmodule

3.2.3 counter60程序

module counter60(Q1,Q2,Q3,Q4,Q5,Q6,Cnt,Cnt1,Cnt24,nCR,EN,CP,LED,LED1,LED2); input CP,nCR,EN; output [7:0] Cnt; output [7:0] Cnt1; output [7:0] Cnt24; output [6:0] Q1; output [6:0] Q2; output [6:0] Q3; output [6:0] Q4; output [6:0] Q5; output [6:0] Q6; output LED; output LED1; output LED2; wire [7:0] Cnt;

桂林电子科技大学课程设计(论文)报告用纸 第 11 页 共 18 页

wire [7:0] Cnt1; wire [7:0] Cnt24; wire [6:0] Q1; wire [6:0] Q2; wire [6:0] Q3; wire [6:0] Q4; wire [6:0] Q5; wire [6:0] Q6; wire LED; wire LED1; wire LED2; wire ENP; wire ENP1; wire ENP2; wire ENP3;

counter10 UC0(Cnt[3:0],nCR,EN,CP); counter6 UC1(Cnt[7:4],nCR,ENP,CP); counter10 UC2(Cnt1[3:0],nCR,ENP1,CP); counter6 UC3(Cnt1[7:4],nCR,ENP2,CP);

counter24 UC4(Cnt24[7:4],Cnt24[3:0],nCR,ENP3,CP); assign ENP=(Cnt[3:0]==4'h9); assign ENP1=(Cnt==8'h59);

assign ENP2=((Cnt1[3:0]==4'h9)&&(Cnt==8'h59)); assign ENP3=((Cnt1==8'h59)&&(Cnt==8'h59)); assign LED=~CP; assign LED1=~CP; assign LED2=~CP;

from0to9 UC5(Q1,Cnt[3:0]); from0to9 UC6(Q2,Cnt[7:4]); from0to9 UC7(Q3,Cnt1[3:0]); from0to9 UC8(Q4,Cnt1[7:4]); from0to9 UC9(Q5,Cnt24[3:0]); from0to9 UC10(Q6,Cnt24[7:4]); Endmodule

3.2.4 from0to9程序

module from0to9(HEX,D); output [6:0] HEX; input [3:0] D; reg [6:0] HEX; always @(D) begin case(D)

4'd0:{HEX[0],HEX[1],HEX[2],HEX[3],HEX[4],HEX[5],HEX[6]}=7'b0000001; 4'd1:{HEX[0],HEX[1],HEX[2],HEX[3],HEX[4],HEX[5],HEX[6]}=7'b1001111; 4'd2:{HEX[0],HEX[1],HEX[2],HEX[3],HEX[4],HEX[5],HEX[6]}=7'b0010010;

桂林电子科技大学课程设计(论文)报告用纸 第 12 页 共 18 页

4'd3:{HEX[0],HEX[1],HEX[2],HEX[3],HEX[4],HEX[5],HEX[6]}=7'b0000110; 4'd4:{HEX[0],HEX[1],HEX[2],HEX[3],HEX[4],HEX[5],HEX[6]}=7'b1001100; 4'd5:{HEX[0],HEX[1],HEX[2],HEX[3],HEX[4],HEX[5],HEX[6]}=7'b0100100; 4'd6:{HEX[0],HEX[1],HEX[2],HEX[3],HEX[4],HEX[5],HEX[6]}=7'b0100000; 4'd7:{HEX[0],HEX[1],HEX[2],HEX[3],HEX[4],HEX[5],HEX[6]}=7'b0001111; 4'd8:{HEX[0],HEX[1],HEX[2],HEX[3],HEX[4],HEX[5],HEX[6]}=7'b0000000; 4'd9:{HEX[0],HEX[1],HEX[2],HEX[3],HEX[4],HEX[5],HEX[6]}=7'b0000100; default:{HEX[0],HEX[1],HEX[2],HEX[3],HEX[4],HEX[5],HEX[6]}=7'b1111111; endcase end

endmodule

3.2.5 counter6程序

module counter6(Q,nCR,EN,CP); input CP,nCR,EN; output [3:0] Q; reg [3:0] Q;

always @(posedge CP or negedge nCR) begin

if(~nCR) Q<=4'b0000;//当nCR=0时,计时器清零 else if(~EN) Q<=Q;//当EN=0时,停止计时,保持 else if(Q==4'b0101) Q<=4'b0000; else Q<=Q+1'b1;//计时器正常计时 end

Endmodule

3.2.6 counter10程序

module counter10(Q,nCR,EN,CP); input CP,nCR,EN; output [3:0] Q; reg [3:0] Q;

always @(posedge CP or negedge nCR) begin

if(~nCR) Q<=4'b0000;//当nCR=0时,计时器清零 else if(~EN) Q<=Q;//当EN=0时,停止计时,保持 else if(Q==4'b1001) Q<=4'b0000; else Q<=Q+1'b1;//计时器正常计时 end

Endmodule

3.2.7 分频程序

module Divided_Frequency(_1HzOut,nCR,_5kHzIN); input _5kHzIN,nCR; output _1HzOut;

supply1 Vdd;//定义 Vdd 是高电平

wire [15:0] Q;//计时器的输出信号(中间变量 ) wire EN1,EN2,EN3;// 计时器的使能信号(中间变量 ) counter10 DU0(Q[3:0],nCR,Vdd,_5kHzIN);//调用十进制

桂林电子科技大学课程设计(论文)报告用纸 第 13 页 共 18 页

counter10 DU1(Q[7:4],nCR,EN1,_5kHzIN); counter10 DU2(Q[11:8],nCR,EN2,_5kHzIN); counter10 DU3(Q[15:12],nCR,EN3,_5kHzIN); assign EN1=(Q[3:0]==4'h9);

assign EN2=(Q[7:4]==4'h9)&(Q[3:0]==4'h9);

assign EN3=(Q[11:8]==4'h9)&(Q[7:4]==4'h9)&(Q[3:0]==4'h9); assign _1HzOut=Q[15]; //assign _500HzOut=Q[0]; endmodule

3.2.8 校时模块程序

module top_clock(Second,Minute,Hour,_1Hz,nCR,AdjMinKey,AdjHrkey); input _1Hz,nCR,AdjMinKey,AdjHrkey; output [7:0] Second,Minute,Hour; wire [7:0] Hour,Minute,Second; supply1 Vdd; wire MinCP,HrCP;

counter60 UT1(Second,nCR,Vdd,_1Hz);//调用counter60,对秒计时 counter60 UT2(Minute,nCR,Vdd,~MinCP);// 调用counter60,对分校时

counter24 UT3(Hour[7:4],Hour[3:0],nCR,Vdd,~HrCP);//调用counter24,对时校时 assign MinCP=AdjMinKey ? _1Hz:(Second==8'h59);//当MinCP=AdjMinKey时,执行校时, 当MinCP≠AdjMinKey时,秒计时至59 时向分进位 assign HrCP=AdjHrkey?_1Hz:({Minute,Second}==16'h5959);//当HrCP=AdjHrkey时,执 行校时,当HrCP≠AdjHrkey时,分

秒计时至59分59秒时向时进位 Endmodule

四.调试及结果

4.1 模块仿真

4.1.1 counter10模块仿真

CP为时钟脉冲;当CP↑,EN和nCR为高电平时Q计数;Q的计数范围为[0,9]

23 counter10仿真波形图

桂林电子科技大学课程设计(论文)报告用纸 第 14 页 共 18 页

4.1.2 counter24模块仿真

CP为时钟脉冲;当CP↑,EN和nCR为高电平时Q计数;Q的计数范围为[0,23]

图24 counter24仿真波形图

4.1.3 counter60模块仿真

CP为时钟脉冲;当CP↑,EN和nCR为高电平时Q计数;Q的计数范围为[0,59]

图25 counter60仿真波形图

4.1.4 分频模块仿真

分频器是将输入的基本时钟信号分频为其他模块需要的5kHZ 和1HZ 的信号. 其中CLK 输入为5kHZ , CLK2 输出1HZ 信号,分频实际也是用了计数器的原理。

图26 分频模块仿真波形图

4.1.5 top_clock计时模块仿真