本人菜鸟,写了一个
FPGA的时钟,但是不知道为什么秒上从0变为1 又变为0.。。。分跟时就根本没变化。。。不知道为什么啊。。。求指教
//分频子模块
module fenpin (clk,rst_n,en_1s,en_1ms); //产生1s,1ms的分频
input clk;
input rst_n;
output en_1s;
output en_1ms;
reg[31:0] jishu_1s;
reg[15:0] jishu_1ms;
parameter cnt_1s =49999999;
parameter cnt_1ms =49999;
always@(posedge clk or negedge rst_n)
begin
if(!rst_n)
jishu_1s<=32'b0;
else if(jishu_1s<cnt_1s)
jishu_1s<=jishu_1s+1'b1;
else
jishu_1s<=32'b0;
end
always@(posedge clk or negedge rst_n)
begin
if(!rst_n)
jishu_1ms<=16'b0;
else if(jishu_1ms<cnt_1ms)
jishu_1ms<=jishu_1ms+1'b1;
else
jishu_1ms<=16'b0;
end
assign en_1s=(jishu_1s==cnt_1s)? 1'b1 : 1'b0; //1s
assign en_1ms=(jishu_1ms==cnt_1ms)? 1'b1 : 1'b0; //1ms
endmodule
//时、分、秒
module shijian(clk,rst_n,en_1s,shi,fen,miao);
input clk;
input rst_n;
input en_1s;
output[5:0] shi;
output[5:0] fen;
output[5:0] miao;
reg [5:0] shi;
reg [5:0] fen;
reg [5:0] miao;
always@(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
shi<=6'b0;
fen<=6'b0;
miao<=6'b0;
end
else if(en_1s)
begin
//if(miao<60)
miao<=miao+1'b1;
if(miao==60)
begin
miao<=0;
//if(fen<60)
fen<=fen+1'b1;
if(fen==60)
begin
fen<=0;
//if(shi<24)
shi<=shi+1'b1;
if(shi==24)
shi<=0;
end
end
end
else
begin
shi<=shi;
fen<=fen;
miao<=miao;
end
end
endmodule
//显示部分
module xianshi(clk,rst_n,en_1ms,shi,fen,miao,led_bit,dataout);
input clk;
input rst_n;
input en_1ms;
input shi;
input fen;
input miao;
output[7:0] led_bit; //位选
output[7:0] dataout; //段选
//数码管显示 0~9 对应段选输出
parameter num0 = 8'b11000000,
num1 = 8'b11111001,
num2 = 8'b10100100,
num3 = 8'b10110000,
num4 = 8'b10011001,
num5 = 8'b10010010,
num6 = 8'b10000010,
num7 = 8'b11111000,
num8 = 8'b10000000,
num9 = 8'b10010000;
reg[3:0] shi1,shi2,fen1,fen2,miao1,miao2;
reg[7:0] led_bit; //位选
reg[7:0] dataout; //段选
reg[2:0] state; //状态寄存器
always@(posedge clk or negedge rst_n)
if(!rst_n)
begin
led_bit<=8'b1;
state<=3'b0;
end
else if(en_1ms)
begin
state<=state+1'b1;
shi1=shi/10;
shi2=shi%10;
fen1=fen/10;
fen2=fen%10;
miao1=miao/10;
miao2=miao%10;
if(state==3'b000)
begin
//state<=3'b001;
led_bit=8'b11111110;
case(miao2)
0: dataout<=num0;
1: dataout<=num1;
2: dataout<=num2;
3: dataout<=num3;
4: dataout<=num4;
5: dataout<=num5;
6: dataout<=num6;
7: dataout<=num7;
8: dataout<=num8;
9: dataout<=num9;
default :dataout<=num0;
endcase
end
else if(state==3'b001)
begin
//state<=3'b010;
led_bit=8'b11111101;
case(miao1)
0: dataout<=num0;
1: dataout<=num1;
2: dataout<=num2;
3: dataout<=num3;
4: dataout<=num4;
5: dataout<=num5;
// 6: dataout<=num6;
// 7: dataout<=num7;
// 8: dataout<=num8;
// 9: dataout<=num9;
default :dataout<=num0;
endcase
end
else if(state==3'b010)
begin
//state<=3'b011;
led_bit=8'b11110111;
case(fen2)
0: dataout<=num0;
1: dataout<=num1;
2: dataout<=num2;
3: dataout<=num3;
4: dataout<=num4;
5: dataout<=num5;
6: dataout<=num6;
7: dataout<=num7;
8: dataout<=num8;
9: dataout<=num9;
default :dataout<=num0;
endcase
end
else if(state==3'b011)
begin
//state<=3'b100;
led_bit=8'b11101111;
case(fen1)
0: dataout<=num0;
1: dataout<=num1;
2: dataout<=num2;
3: dataout<=num3;
4: dataout<=num4;
5: dataout<=num5;
// 6: dataout<=num6;
// 7: dataout<=num7;
// 8: dataout<=num8;
// 9: dataout<=num9;
endcase
end
else if(state==3'b100)
begin
//state<=3'b101;
led_bit=8'b10111111;
case(shi2)
0: dataout<=num0;
1: dataout<=num1;
2: dataout<=num2;
3: dataout<=num3;
4: dataout<=num4;
// 5: dataout<=num5;
// 6: dataout<=num6;
// 7: dataout<=num7;
// 8: dataout<=num8;
// 9: dataout<=num9;
default :dataout<=num0;
endcase
end
else if(state==3'b101)
begin
//state<=3'b110;
led_bit=8'b01111111;
case(shi1)
0: dataout<=num0;
1: dataout<=num1;
2: dataout<=num2;
3: dataout<=num3;
// 4: dataout<=num4;
// 5: dataout<=num5;
// 6: dataout<=num6;
// 7: dataout<=num7;
// 8: dataout<=num8;
// 9: dataout<=num9;
endcase
end
else if(state==3'b110)
begin
//state<=3'b000;
led_bit=8'b11011011;
dataout<=8'b10111111;
end
end
else
begin
dataout<=dataout;
led_bit<=led_bit;
end
endmodule
//顶层模块
module Shizhong(clk,rst_n,led_bit,dataout);
input clk;
input rst_n;
output[7:0] led_bit;
output[7:0] dataout;
wire en_1s;
wire en_1ms;
fenpin fenpin_int(.clk(clk),
.rst_n(rst_n),
.en_1s(en_1s),
.en_1ms(en_1ms)
);
wire shi,fen,miao;
shijian shijian_int(.clk(clk),
.rst_n(rst_n),
.en_1s(en_1s),
.shi(shi),
.fen(fen),
.miao(miao)
);
xianshi xianshi_int(.clk(clk),
.rst_n(rst_n),
.en_1ms(en_1ms),
.shi(shi),
.fen(fen),
.miao(miao),
.led_bit(led_bit),
.dataout(dataout)
);
endmodule
一周热门 更多>