|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
这是一段数字钟的代码,输入时钟是50Mhz的,写的不太好,但我测试波形文件出来了啊,下载到板子上就只在一位上显示一个8,求高手指点一二,不胜感激。里面的按键只声明了没有使用,管脚约束应该也没问题。我基本都是自学的,希望能有高人给指正一下。
//`timescale 1ns / 1ps
module test(clk,n_clk,d_clk,rst,k_s,k_add,k_sub,seg_sel,seg_8,minute,second);
input clk,rst,k_s,k_add,k_sub;
output reg [4:1]seg_sel;
output reg [8:1]seg_8;
output reg n_clk,d_clk;
reg [25:0]count,count1;
output reg [7:0]minute,second;
reg [4:1]second_h,second_l,minute_h,minute_l;
reg [4:1]state;
parameter s1=4'b0001,s2=4'b0010,s3=4'b0100,s4=4'b1000;
initial
begin
n_clk<=0;d_clk<=0;
count<=0;count1<=0;
minute<=0;
second<=0;
seg_sel<=4'b0000;
seg_8<=8'hff;
end
always @(posedge clk)begin //时钟分频器
if(rst)begin
count<=0;
end
else if(clk)begin
if(count>25'h17d7840)//1hz
begin
count<=0;
n_clk=~n_clk;
end
else
count<=count+1;
end
end
always @(posedge clk)begin //数码管分频器
if(rst)begin
count1<=0;
end
else if(clk)begin
if(count1>17'h1312d0)//200hz
begin
count1<=0;
d_clk=~d_clk;
end
else
count1<=count1+1;
end
end
always @(negedge n_clk)begin //计时器
if(rst)begin
minute<=0;
second<=0;
end
if(second[3:0]==4'h9)begin
second[3:0]<=0;
if(second==8'h59)begin
second<=0;
if(minute[3:0]==4'h9)begin
minute[3:0]<=0;
if(minute==8'h59)
minute<=0;
else
minute[7:4]<=minute[7:4]+1;
end
else
minute[3:0]<=minute[3:0]+1;
end
else
second[7:4]<=second[7:4]+1;
end
else
second[3:0]<=second[3:0]+1;
end
always @(negedge clk)begin //译码器
if(rst)begin
second_h<=0;
second_l<=0;
minute_h<=0;
minute_l<=0;
end
else begin
second_h<=second[7:4];
second_l<=second[3:0];
minute_h<=minute[7:4];
minute_l<=minute[3:0];
end
end
always @(negedge d_clk)begin //显示器位选
case (state)
0:begin state=s1;end
s1:begin state=s2;end
s2:begin state=s3;end
s3:begin state=s4;end
s4:begin state=s1;end
default begin state=0;end
endcase
end
task shuma; //数码管译码
input [4:1]shu_in;
output [8:1]seg;
case(shu_in)
0:begin seg=7'b10111111;end
1:begin seg=7'b10000110;end
2:begin seg=7'b11011011;end
3:begin seg=7'b11001111;end
4:begin seg=7'b11100110;end
5:begin seg=7'b11101101;end
6:begin seg=7'b11111101;end
7:begin seg=7'b10000111;end
8:begin seg=7'b11111111;end
9:begin seg=7'b11101111;end
default:seg=7'b1111111;
endcase
endtask
always @(state)begin//显示器段选
case (state)
s1:begin seg_sel=s1;shuma(second_l,seg_8);end
s2:begin seg_sel=s2;shuma(second_h,seg_8);end
s3:begin seg_sel=s3;shuma(minute_l,seg_8);end
s4:begin seg_sel=s4;shuma(minute_h,seg_8);end
default:begin seg_sel=s1;end
endcase
end
endmodule |
|