Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
21602 Discussions

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

Altera_Forum
Honored Contributor II
1,323 Views

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
0 Kudos
1 Reply
Altera_Forum
Honored Contributor II
615 Views

I hope somebody can help solve it.Thanks.

0 Kudos
Reply