module clock_o//顶层
(
input clk,
input rst,
input key_starte,
input key_stop,
output [6:0]seg1,
output [6:0]seg2,
output [6:0]seg3,
output [6:0]seg4,
output [6:0]seg5,
output [6:0]seg6
);
pro_t
pro_t_inst
(
.clk(clk),
.rst(rst),
.key_starte(key_starte),
.key_stop(key_stop),
.key_starte_1(key_starte_1),
.key_stop_1(key_stop_1)
);
clv
clv_inst
(
.clk(clk),
.rst(rst),
.out1(out1)
);
wire [3:0]a1;
wire [3:0]a2;
wire [3:0]a3;
wire [3:0]a4;
wire [3:0]a5;
wire [3:0]a6;
snain_st
snain_st_inst
(
.clk(clk),
.rst(rst),
.key_starte(key_starte_1),
.key_stop(key_stop_1),
.key1(out1),
.a1(a1),
.a2(a2),
.a3(a3),
.a4(a4),
.a5(a5),
.a6(a6)
);
//1
clecate_1
clecate_1_1
(
.clk(clk),
.rst(rst),
.in(a1),
.seg(seg1)
);
//2
clecate_1
clecate_1_2
(
.clk(clk),
.rst(rst),
.in(a2),
.seg(seg2)
);
//3
clecate_1
clecate_1_3
(
.clk(clk),
.rst(rst),
.in(a3),
.seg(seg3)
);
//4
clecate_1
clecate_1_4
(
.clk(clk),
.rst(rst),
.in(a4),
.seg(seg4)
);
//5
clecate_1
clecate_1_5
(
.clk(clk),
.rst(rst),
.in(a5),
.seg(seg5)
);
//6
clecate_1
clecate_1_6
(
.clk(clk),
.rst(rst),
.in(a6),
.seg(seg6)
);
endmodule
//分频
module clv
(
input clk,
input rst,
output out1
);
parameter PI=13'd5_000-1;
reg [12:0]cont;
always@(posedge clk or negedge rst)
begin
if(~rst)
cont<=1'b0;
else if(cont==PI)
cont<=1'b0;
else
cont<=cont+1'b1;
end
reg clk1;
always@(posedge clk or negedge rst)
begin
if(~rst)
clk1<=1'b0;
else if(cont==PI)
clk1<=1'b1;
else
clk1<=1'b0;
end
assign out1=clk1;
endmodule
//按键输入(3个寄存器分频)
module pro_t
(
input clk,
input rst,
input key_starte,
input key_stop,
output key_starte_1,
output key_stop_1
);
reg pro;
wire key_starte_out;
always@(posedge clk or negedge rst)
begin
if(~rst)
pro<=1'b0;
else
pro<=key_starte;
end
reg pro_1;
always@(posedge clk or negedge rst)
begin
if(~rst)
pro_1<=1'b0;
else
pro_1<=pro;
end
reg pro_2;
always@(posedge clk or negedge rst)
begin
if(~rst)
pro_2<=1'b0;
else
pro_2<=pro_1;
end
reg pro_7;
always@(posedge clk or negedge rst)
begin
if(~rst)
pro_7<=1'b0;
else
pro_7<=pro_2&(~pro_1);
end
assign key_starte_out=pro_7;
reg pro_3;
wire key_stop_out;
always@(posedge clk or negedge rst)
begin
if(~rst)
pro_3<=1'b0;
else
pro_3<=key_stop;
end
reg pro_4;
always@(posedge clk or negedge rst)
begin
if(~rst)
pro_4<=1'b0;
else
pro_4<=pro_3;
end
reg pro_5;
always@(posedge clk or negedge rst)
begin
if(~rst)
pro_5<=1'b0;
else
pro_5<=pro_4;
end
reg pro_6;
always@(posedge clk or negedge rst)
begin
if(~rst)
pro_6<=1'b0;
else
pro_6<=pro_5&(~pro_4);
end
assign key_stop_out=pro_6;
assign key_starte_1=key_starte_out;
assign key_stop_1=key_stop_out;
endmodule
//状态机
module snain_st
(
input clk,
input rst,
input key_starte,
input key_stop,
input key1,
output [3:0]a1,
output [3:0]a2,
output [3:0]a3,
output [3:0]a4,
output [3:0]a5,
output [3:0]a6
);
parameter IDLE=3'b000;
parameter WORK=3'b001;
parameter STOP=3'b010;
reg [2:0]st;
reg [2:0]st_next;
always@(posedge clk or negedge rst)
begin
if(~rst)
st<=IDLE;
else
st<=st_next;
end
always@(*)
begin
if(~rst)
st_next=IDLE;
else
case(st)
IDLE:if(key_starte==1)
st_next=WORK;
else
st_next=IDLE;
WORK:if(key_stop==1)
st_next=STOP;
else
st_next=WORK;
STOP:if(key_starte==1)
st_next=WORK;
else
st_next=STOP;
default:st_next=IDLE;
endcase
end
//-----------------------------------------------
//1
reg [3:0]cont;
always@(posedge clk or negedge rst)
begin
if(~rst)
cont<=1'b0;
else if(st_next==WORK)
begin
if(key1==1)
begin
if(cont==4'd9)
cont<=0;
else if(key1==1)
cont<=cont+1'b1;
end
end
else
cont<=cont;
end
//2
reg vs;
always@(posedge clk or negedge rst)
begin
if(~rst)
vs<=0;
else if(cont==4'd9)
vs<=1'b1;
else
vs<=1'b0;
end
reg [3:0]cont1;
always@(posedge clk or negedge rst)
begin
if(~rst)
cont1<=4'b0;
else if(st_next==WORK)
begin
if(vs==1&&key1==1)
begin
if(cont1==4'd9&&cont==4'd9)
cont1<=0;
else if(vs==1&&cont==4'd9)
cont1<=cont1+1;
end
else
cont1<=cont1;
end
end
//3
reg vs1;
always@(posedge clk or negedge rst)
begin
if(~rst)
vs1<=1'b0;
else if(cont1==4'd9)
vs1<=1'b1;
else
vs1<=1'b0;
end
reg [3:0]cont2;
always@(posedge clk or negedge rst)
begin
if(~rst)
cont2<=1'b0;
else if(st_next==WORK)
begin
if(vs==1&&vs1==1&&key1==1)
begin
if(cont2==4'd9&&cont1==4'd9&&cont==4'd9)
cont2<=0;
else if(cont1==4'd9&&cont==4'd9&&vs==1)
cont2<=cont2+1'b1;
end
end
else
cont2<=cont2;
end
//4
reg vs2;
always@(posedge clk or negedge rst)
begin
if(~rst)
vs2<=0;
else if(cont2==4'd9)
vs2<=1'b1;
else
vs2<=1'b0;
end
reg [3:0]cont3;
always@(posedge clk or negedge rst)
begin
if(~rst)
cont3<=4'd0;
else if(st_next==WORK)
begin
if(vs2==1&&vs==1&&vs1==1&&key1==1)
begin
if(cont3==4'd9&&cont2==4'd9&&cont1==4'd9&&cont==4'd9)
cont3<=0;
else if(cont2==4'd9&&cont1==4'd9&&cont==9)
cont3<=cont3+1'b1;
end
end
else
cont3<=cont3;
end
//5
reg vs3;
always@(posedge clk or negedge rst)
begin
if(~rst)
vs3<=0;
else if(cont3==4'd9)
vs3<=1'b1;
else
vs3<=1'b0;
end
reg [3:0]cont4;
always@(posedge clk or negedge rst)
begin
if(~rst)
cont4<=1'b0;
else if(st_next==WORK)
begin
if(vs3==1&&vs2==1&&vs1==1&&vs==1&&key1==1)
begin
if(cont4==4'd9&&cont3==4'd9&&cont2==4'd9&&cont1==4'd9&&cont==4'd9)
cont4<=1'b0;
else if(cont3==4'd9&&cont2==4'd9&&cont1==4'd9&&cont==4'd9)
cont4<=cont4+1'b1;
end
end
else
cont4<=cont4;
end
//6
reg vs4;
always@(posedge clk or negedge rst)
begin
if(~rst)
vs4<=0;
else if(cont4==4'd9)
vs4<=1'b1;
else
vs4<=1'b0;
end
reg [3:0]cont5;
always@(posedge clk)
begin
if(~rst)
cont5<=1'b0;
else if(st_next==WORK)
begin
if(vs4==1&&vs3==1&&vs2==1&&vs1==1&&vs==1&&key1==1)
begin
if(cont5==4'd5&&cont4==4'd9&&cont4==4'd9&&cont3==4'd9&&cont2==4'd9&&cont1==4'd9&&cont==4'd9)
cont5<=1'b0;
else if(cont4==4'd9&&cont4==4'd9&&cont3==4'd9&&cont2==4'd9&&cont1==4'd9&&cont==4'd9)
cont5<=cont5+1'b1;
end
end
else
cont5<=cont5;
end
assign a1=cont;
assign a2=cont1;
assign a3=cont2;
assign a4=cont3;
assign a5=cont4;
assign a6=cont5;
endmodule
//译码
module clecate_1
(
input clk,
input rst,
input [3:0]in,
output [6:0]seg
);
parameter
S0=7'b100_0000,
S1=7'b111_1001,
S2=7'b010_0100,
S3=7'b011_0000,
S4=7'b001_1001,
S5=7'b001_0010,
S6=7'b000_0010,
S7=7'b111_1000,
S8=7'b000_0000,
S9=7'b001_0000;
reg [6:0]seg_1;
always@(posedge clk or negedge rst)
begin
if(~rst)
seg_1<=S0;
else
case(in)
4'b0000:seg_1<=S0;
4'b0001:seg_1<=S1;
4'b0010:seg_1<=S2;
4'b0011:seg_1<=S3;
4'b0100:seg_1<=S4;
4'b0101:seg_1<=S5;
4'b0110:seg_1<=S6;
4'b0111:seg_1<=S7;
4'b1000:seg_1<=S8;
4'b1001:seg_1<=S9;
default:seg_1<=S0;
endcase
end
assign seg=seg_1;
endmodule
//测试:
`timescale 1ns/1ns
module clock_o_tb;
reg clk;
reg rst;
reg key_starte;
reg key_stop;
wire [6:0]seg11;
wire [6:0]seg12;
wire [6:0]seg13;
wire [6:0]seg14;
wire [6:0]seg15;
wire [6:0]seg16;
initial
begin
clk=0;
rst=0;
#50 rst=1;
#200000000 $stop;
end
initial
begin
key_stop=1;
key_starte=1;
#1000 key_starte=0;
#100_000_000 key_stop=0;
end
always #10 clk<=~clk;
clock_o
clock_o_inst
(
.clk(clk),
.rst(rst),
.key_starte(key_starte),
.key_stop(key_stop),
.seg1(seg11),
.seg2(seg12),
.seg3(seg13),
.seg4(seg14),
.seg5(seg15),
.seg6(seg16)
);
endmodule