0~999_999计数器
时间:10-02
整理:3721RD
点击:
/*---------------------------------------------------------------*/
// 0~999_999计数器
///*******************/
//
/*---------------------------------------------------------------*/
module test(clk,rst,seg,bit);
input clk, rst;
output [7:0] seg, bit;
reg [0:7] seg;
reg [5:0] bit;
reg [19:0] dat;
reg [3:0] dat1;
reg [25:0] cnt;
reg [18:0] cnt10ms;
reg [2:0] sel;
reg [3:0] buf0, buf1, buf2, buf3, buf4, buf5;
//---------------从buf0~buf5,依次为最低位到最高位--------------//
always@(posedge clk or negedge rst)
begin
if(!rst)
cnt<=26'd0;
else if(cnt==26'd19_999_999)
cnt<=26'd0;
else
cnt<=cnt+1'b1;
end
always@(posedge clk or negedge rst)
begin
if(!rst)
cnt10ms<=19'd0;
else if(cnt10ms==19'd49_999)
cnt10ms<=19'd0;
else
cnt10ms<=cnt10ms+1'b1;
end
//*----------------------------------------------------------------------------*//
always@(posedge clk or negedge rst)
begin
if(!rst)
dat<=20'd0;
else if(cnt==26'd8_999_999)
dat<=dat+1'b1;
else if(dat==20'd999_999)dat<=20'b0;
else
dat<=dat;
end
/*-----------------------------------------------------------------------------------*/
always@(posedge clk )
begin
if(cnt==26'd8_999_999)
begin
buf0=dat%10;
buf1=((dat-buf0)%100)/10;
buf2=((dat%1000)-buf1*10-buf0)/100;
buf3=((dat%10000)-buf2*100-buf1*10-buf0)/1000;
buf4=((dat%100000)-buf3*1000-buf2*100-buf1*10-buf0)/10000;
buf5=((dat%1000000)-buf4*10000-buf3*1000-buf2*100-buf1*10-buf0)/100000;
end
else
begin
buf0<=buf0;
buf1<=buf1;
buf2<=buf2;
buf3<=buf3;
buf4<=buf4;
buf5<=buf5;
end
end
/*-------------------------------------------------------------------------------*/
always@(posedge cnt10ms)
begin
case(dat1)
4'h0:seg=8'hc0;
4'h1:seg=8'hf9;
4'h2:seg=8'ha4;
4'h3:seg=8'hb0;
4'h4:seg=8'h99;
4'h5:seg=8'h92;
4'h6:seg=8'h82;
4'h7:seg=8'hf8;
4'h8:seg=8'h80;
4'h9:seg=8'h90;
4'ha:seg=8'h88;
4'hb:seg=8'h83;
4'hc:seg=8'hc6;
4'hd:seg=8'ha1;
4'he:seg=8'h86;
4'hf:seg=8'h8e;
endcase
end
/*----------------------------------------------------------------------------*/
always@(posedge clk)
begin
if(cnt10ms==19'd49_999)
sel<=sel+1'b1;
else if(sel==3'h6)
sel<=3'b0;
else
sel<=sel;
end
/*---------------------------------------------------------------------------*/
always@(posedge clk)
begin
if(cnt10ms==19'd49_999)
case(sel)
3'h0:begin bit<=6'b111_110;dat1<=buf5;end//第一位 从前往后数
3'h1:begin bit<=6'b111_101;dat1<=buf4;end//第二位
3'h2:begin bit<=6'b111_011;dat1<=buf3;end//第三位
3'h3:begin bit<=6'b110_111;dat1<=buf2;end//第四位
3'h4:begin bit<=6'b101_111;dat1<=buf1;end//第五位
3'h5:begin bit<=6'b011_111;dat1<=buf0;end//第六位
endcase
end
endmodule
// 0~999_999计数器
///*******************/
//
/*---------------------------------------------------------------*/
module test(clk,rst,seg,bit);
input clk, rst;
output [7:0] seg, bit;
reg [0:7] seg;
reg [5:0] bit;
reg [19:0] dat;
reg [3:0] dat1;
reg [25:0] cnt;
reg [18:0] cnt10ms;
reg [2:0] sel;
reg [3:0] buf0, buf1, buf2, buf3, buf4, buf5;
//---------------从buf0~buf5,依次为最低位到最高位--------------//
always@(posedge clk or negedge rst)
begin
if(!rst)
cnt<=26'd0;
else if(cnt==26'd19_999_999)
cnt<=26'd0;
else
cnt<=cnt+1'b1;
end
always@(posedge clk or negedge rst)
begin
if(!rst)
cnt10ms<=19'd0;
else if(cnt10ms==19'd49_999)
cnt10ms<=19'd0;
else
cnt10ms<=cnt10ms+1'b1;
end
//*----------------------------------------------------------------------------*//
always@(posedge clk or negedge rst)
begin
if(!rst)
dat<=20'd0;
else if(cnt==26'd8_999_999)
dat<=dat+1'b1;
else if(dat==20'd999_999)dat<=20'b0;
else
dat<=dat;
end
/*-----------------------------------------------------------------------------------*/
always@(posedge clk )
begin
if(cnt==26'd8_999_999)
begin
buf0=dat%10;
buf1=((dat-buf0)%100)/10;
buf2=((dat%1000)-buf1*10-buf0)/100;
buf3=((dat%10000)-buf2*100-buf1*10-buf0)/1000;
buf4=((dat%100000)-buf3*1000-buf2*100-buf1*10-buf0)/10000;
buf5=((dat%1000000)-buf4*10000-buf3*1000-buf2*100-buf1*10-buf0)/100000;
end
else
begin
buf0<=buf0;
buf1<=buf1;
buf2<=buf2;
buf3<=buf3;
buf4<=buf4;
buf5<=buf5;
end
end
/*-------------------------------------------------------------------------------*/
always@(posedge cnt10ms)
begin
case(dat1)
4'h0:seg=8'hc0;
4'h1:seg=8'hf9;
4'h2:seg=8'ha4;
4'h3:seg=8'hb0;
4'h4:seg=8'h99;
4'h5:seg=8'h92;
4'h6:seg=8'h82;
4'h7:seg=8'hf8;
4'h8:seg=8'h80;
4'h9:seg=8'h90;
4'ha:seg=8'h88;
4'hb:seg=8'h83;
4'hc:seg=8'hc6;
4'hd:seg=8'ha1;
4'he:seg=8'h86;
4'hf:seg=8'h8e;
endcase
end
/*----------------------------------------------------------------------------*/
always@(posedge clk)
begin
if(cnt10ms==19'd49_999)
sel<=sel+1'b1;
else if(sel==3'h6)
sel<=3'b0;
else
sel<=sel;
end
/*---------------------------------------------------------------------------*/
always@(posedge clk)
begin
if(cnt10ms==19'd49_999)
case(sel)
3'h0:begin bit<=6'b111_110;dat1<=buf5;end//第一位 从前往后数
3'h1:begin bit<=6'b111_101;dat1<=buf4;end//第二位
3'h2:begin bit<=6'b111_011;dat1<=buf3;end//第三位
3'h3:begin bit<=6'b110_111;dat1<=buf2;end//第四位
3'h4:begin bit<=6'b101_111;dat1<=buf1;end//第五位
3'h5:begin bit<=6'b011_111;dat1<=buf0;end//第六位
endcase
end
endmodule