Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

I can't solve the wrong with a stop watch desiging

I was designing a stop watch this week,but get some troubles.

There are four keys---reset,stop,minset(minute set),and secset(second set) in the watch.The reset and stop fuctions are right,but the minset and the secset won't make the minute or the second +1 successfully,and show some error numbers.I don't know why.

This is my code:

module SECLOCK(clk,sclk,rundir,key);

input clk,sclk;//clk--system clk,sclk---percentage second

input rundir;//run direction

input[3:0] key;// 4 keys

reg[32:0] j;//the total percentage seconds,to generate min,sec,percentage second

reg run;//stop mark

reg keyclk;

reg ssclk;

always @(posedge clk)//scan the keys' value,when key!=0,keyclk set high

begin

if(key!=0) keyclk<=1;

else keyclk<=0;

end

always @(sclk)//avoid the conflict between keyclk and ssclk

begin

if(keyclk!=0) ssclk=0;

else ssclk=sclk;

end

always @(posedge keyclk or posedge ssclk)

begin

if(keyclk)

begin

case(key0)

4'b0001://reset

begin

j<=0;

run<=0;

end

4'b0010://stop

begin

run<=~run;

end

4'b0100://set second + 1

begin

j<=j+33'd100;

end

4'b1000://set min + 1

begin

j<=j+33'd6000;

end

endcase

end

else

begin

if(run==1 && rundir==1 ) //forward count

j<=j+1;

else if(run==1 && rundir==0 && j>0) //reward count

j<=j-1;

end

end

endmodule

1 Reply