Forum Discussion
Altera_Forum
Honored Contributor
14 years agoSimple game using Max II starter kit
Hi!
I really need help to the expert since I am not really familiar with Max II and verilog programming. This would be our final project for this semester. The game is called "catch the L.E.D". :) so here's the scenario of the game. We are task to program the kit so that the 4 led will lit randomly and the player should press the buttons accordingly as the led "ON" state. If the user/player presses with timing OR able to catch the flashing LED, we will display his/her score on 7-segment display. The highest score will be 99. it has also a time limit of 1minute. since Max II dev kit has 4-digit 7-segment display, we will display the time limit and score 2-digits each. Max II development kit: http://admucoe.tripod.com/max2.bmp12 Replies
- Altera_Forum
Honored Contributor
sir, ovro.
I have quick question for you. i have running code below which I used switch as input. but i don't have any idea how to implement button debouncing. code: --- Quote Start --- /*game pattern(LED | BUTTON)*/ if(ButtonT==11000000) //button detection delay begin ledBlink = timer1; ButtonT = 1; if(ledBlink == 1) begin LED = D1; if(sw1==0 && sw4==0) begin score1 = score1+2; end else if (sw1==0 || sw4==0) begin score1 = score1+1'b1; end end else if(ledBlink == 2) begin LED = D2; if(sw2==0 && sw3==0) begin score1 = score1+2; end else if (sw2==0 || sw3==0) begin score1=score1+1'b1; end end else if(ledBlink == 3) begin LED = D3; if(sw3 == 0) begin score1 = score1+1'b1; end end else if(ledBlink == 4) begin LED = D4; if(sw4 == 0) begin score1 = score1+1'b1; end end else if(ledBlink == 5) begin LED = D8; if(sw4 == 0 ) begin score1 = score1+1'b1; end end else if(ledBlink == 6) begin LED = D7; if(sw3 == 0) begin score1 = score1+1'b1; end end else if(ledBlink == 7) begin LED = D6; if(sw2 == 0) begin score1 = score1+1'b1; end end else if(ledBlink == 8) begin LED = D5; if(sw1 == 0) begin score1 = score1+1'b1; end end else begin LED= off; end if(score1 == 10) begin score1 = 0; score2 = score2+1; end end else begin ButtonT=ButtonT+1'b1; end end --- Quote End --- I tested debouncing like on embdded C which I think cannot be implemented here in verilog programming, like this. --- Quote Start --- else if(ledBlink == 3) begin LED = D3; if(sw3 == 0) begin score1 = score1+1'b1; while (sw3 == 0); // I think it can't be implenmented here. :( end end --- Quote End --- THANK YOU! - Altera_Forum
Honored Contributor
--- Quote Start --- i don't have any idea how to implement button debouncing. --- Quote End --- When I debounce signals, I use a small statemachine and registers. I have the code in VHDL, not Verilog, so I'll just describe it. 1) Synchronize the button to the local clock using dual-DFF synchronizer. Lets call this signal 'button_sync' 2) Delay the synchronized signal using a single DFF Lets call this signal 'button_delay' 3) Define a signal that is the XOR of these two signals assign button_change = button_delay ^ button_sync; This signal will be high when there is a change in logic level. 4) Use a state machine and a counter to control a 'button_debounce' register. When the 'button_change' signal asserts, the state machine transitions from IDLE to the CHANGE state. On the transition load a counter with the debounce time (use a down-counter, so that carry-out can be used to detect termination). While in the CHANGE state, enable the counter. If the change signal asserts while in the CHANGE state, then reload the counter. Only when the timer terminates do you update the 'button_debounce' output. This logic ensures that the signal is in a valid asserted or deasserted state for at least the debounce time. Create a testbench for your debounce component, and edit/debug until you can debounce a signal. The above description should get you most of the way there. If you have trouble, I'll post the VHDL code and its testbench. You can use that as the reference. Cheers, Dave