0-9计数器
module cnt_0to9(clk,ov);
input clk;
output ov;
reg ov;
reg [27:0] con;
always@(posedge clk)
begin
if(con==50000000)
begin
ov<=1;
con<=0;
end
else
begin
ov<=0;
con<=con+1;
end
end
endmodule
module cnt_1to9(CNTVAL,nv,clk1);
input clk1;
output [3:0]CNTVAL;
reg [3:0]CNTVAL;
output nv;
reg nv;
reg [3:0]count;
always@(posedge clk1)
begin
if(count==9)
begin
nv=1;
end
else
begin
count=count+1;
nv=0;
end
CNTVAL=count;
end
endmodule
module yima(in,out);
input[3:0] in;
output[7:0] out;
reg[7:0] out;
always@(in)
begin
case(in)
4'b0000:out=8'b11000000;
4'b0001:out=8'b11111001;
4'b0010:out=8'b10100100;
4'b0011:out=8'b10110000;
4'b0100:out=8'b10011001;
4'b0101:out=8'b10010010;
4'b0110:out=8'b10000010;
4'b0111:out=8'b11111000;
4'b1000:out=8'b10000000;
4'b1001:out=8'b10010000;
4'b1010:out=8'b10001000;
4'b1011:out=8'b10000011;
4'b1100:out=8'b11000110;
4'b1101:out=8'b10100001;
4'b1110:out=8'b10000110;
4'b1111:out=8'b11001110;
endcase
end
endmodule
注:当数码管计够9个数时,会产生一个高电平信号,使得LED灯点亮
RTL视图及内部框图
设计一个0-15计数器,0-9时ov输出0,10-15ov输出1
module cnt_0to9_2(clk,ov);
input clk;
output ov;
reg ov;
reg [27:0] con;
always@(posedge clk)
begin
if(con==50000000)
begin
ov<=1;
con<=0;
end
else
begin
ov<=0;
con<=con+1;
end
end
endmodule
module cnt_1to9(CNTVAL,nv,clk1);
input clk1;
output [3:0]CNTVAL;
reg [3:0]CNTVAL;
output nv;
reg nv;
reg [3:0]count;
always@(posedge clk1)
begin
if(count==17)
begin
count=0;
end
else
begin
count=count+1;
end
CNTVAL=count;
end
always@(count)
begin
if(count>=9)
nv=1;
else
nv=0;
end
endmodule
module yima(in,out);
input[3:0] in;
output[7:0] out;
reg[7:0] out;
always@(in)
begin
case(in)
4'b0000:out=8'b11000000;
4'b0001:out=8'b11111001;
4'b0010:out=8'b10100100;
4'b0011:out=8'b10110000;
4'b0100:out=8'b10011001;
4'b0101:out=8'b10010010;
4'b0110:out=8'b10000010;
4'b0111:out=8'b11111000;
4'b1000:out=8'b10000000;
4'b1001:out=8'b10010000;
4'b1010:out=8'b10001000;
4'b1011:out=8'b10000011;
4'b1100:out=8'b11000110;
4'b1101:out=8'b10100001;
4'b1110:out=8'b10000110;
4'b1111:out=8'b10001110;
endcase
end
endmodule
设计一个0-17的计数器,当为0-9时ov=0,10-17ov=1;
module cnt_1to9(CNTVAL,nv,clk1);
input clk1;
output [3:0]CNTVAL;
reg [3:0]CNTVAL;
output nv;
reg nv;
reg [3:0]count;
always@(posedge clk1)
begin
if(count==17)
begin
count=0;
end
else
begin
count=count+1;
end
CNTVAL=count;
end
always@(count)
begin
if(count>=9)
nv=1;
else
nv=0;
end
endmodule
module cnt_0to9_2(clk,ov);
input clk;
output ov;
reg ov;
reg [27:0] con;
always@(posedge clk)
begin
if(con==50000000)
begin
ov<=1;
con<=0;
end
else
begin
ov<=0;
con<=con+1;
end
end
endmodule