ms,s,min均两位,一键实现reset,start,stop 3个功能,20Mhz时钟信号20万分频成100hz的时钟信号,我的程序如下,在器材上运行的时候stop功能不能实现,请大神帮忙找出错误和修改方法,谢谢了。
module
timer(clk,button,cnt1,cnt2,cnt3,cnt4,cnt5,cnt6);
input clk,button;
reg[3:0]cnt1,cnt2,cnt3,cnt4,cnt5,cnt6;
output[3:0]cnt1,cnt2,cnt3,cnt4,cnt5,cnt6;
reg [17:0]cnt;
reg[1:0]z;
reg x,y,full,full1,full2,full3,full4,full5;
reg[2:0]cs,ns;
parameter[2:0]
idle=000,
reset=001,
start=010,
stop=100;
always@(posedge clk)
begin
if(cnt==199999) //分频
begin
cnt=0;
full=1;
end
else
begin
cnt=cnt+1;
full=0;
end
end
always@(posedge full)
begin
cs=ns;
end
always@(button or cs) //状态机
begin
case(cs)
idle:
begin
if(button)
ns=reset;
else
ns=idle;
end
reset:
begin
if(button)
ns=start;
else
ns=reset;
end
start:
begin
if(button)
ns=stop;
else
ns=start;
end
stop:
begin
if(button)
ns=reset;
else
ns=stop;
end
default:ns=idle;
endcase
end
always@(ns)
begin
case(ns)
idle:z=2'b00;
reset:z=2'b00;
start:z=2'b10;
stop:z=2'b11;
default:z=2'b00;
endcase
end
always@(posedge full) //异步计数及工作状态
begin
if(z==2'b11) //ms的个位之stop功能
begin
x=0;
y=1;
cnt1=cnt1;
end
else if(z==2'b00) //reset功能
begin
cnt1=4'b0000;
x=1;
y=0;
end
else if(cnt1==4'b1001) //start功能
begin
x=0;
cnt1=4'b0000;
full1=1;
y=0;
end
else
begin
x=0;
cnt1=cnt1+4'b0001;
full1=0;
y=0;
end
end
always@(posedge full1) //个位的进位信号驱动十位
begin
if((y==1)&&(x==0)) //ms的十位 stop
cnt2=cnt2;
else if((x==1)&&(y==0)) //reset
cnt2=4'b0000;
else if(cnt2==4'b1001) //start
begin
cnt2=4'b0000;
full2=1;
end
else
begin
cnt2=cnt2+4'b0001;
full2=0;
end
end
always@(posedge full2)
begin
if((y==1)&&(x==0))
cnt3=cnt3;
else if((x==1)&&(y==0))
cnt3=4'b0000;
else if(cnt3==4'b1001)
begin
cnt3=4'b0000;
full3=1;
end
else
begin
cnt3=cnt3+4'b0001;
full3=0;
end
end
always@(posedge full3)
begin
if((y==1)&&(x==0))
cnt4=cnt4;
else if((x==1)&&(y==0))
cnt4=4'b0000;
else if(cnt4==4'b0101)
begin
cnt4=4'b0000;
full4=1;
end
else
begin
cnt4=cnt4+4'b0001;
full4=0;
end
end
always@(posedge full4)
begin
if((y==1)&&(x==0))
cnt5=cnt5;
else if((x==1)&&(y==0))
cnt5=4'b0000;
else if(cnt5==4'b1001)
begin
cnt5=4'b0000;
full5=1;
end
else
begin
cnt5=cnt5+4'b0001;
full5=0;
end
end
always@(posedge full5)
begin
if((y==1)&&(x==0))
cnt6=cnt6;
else if((x==1)&&(y==0))
cnt6=4'b0000;
else if(cnt6==4'b0101)
cnt6=4'b0000;
else
cnt6=cnt6+4'b0001;
end
endmodule
一周热门 更多>