Interrupt question
Hello everybody,
First Happy new year. I'm new with SX chip and try to figured out interrupt and weak instructions. In the SX-B manual found *winner* tuts. I wondering how to change the code that will reset automatically (display winner while buttons are pressed and reset when the buttons are released). Something similarly I need for my POV-CLOCK project (Calibration - start point of the POV clock for each cycle).
Thank you
First Happy new year. I'm new with SX chip and try to figured out interrupt and weak instructions. In the SX-B manual found *winner* tuts. I wondering how to change the code that will reset automatically (display winner while buttons are pressed and reset when the buttons are released). Something similarly I need for my POV-CLOCK project (Calibration - start point of the POV clock for each cycle).
' ------------------------------------------------------------------------- ' Program Description ' ------------------------------------------------------------------------- ' ' On reset the 7-segment LED is cleared to a dash and the program waits ' for a button press (RB.0 - RB.3) -- the "winner" (first pressed) will ' be displayed on the display until reset. ' ------------------------------------------------------------------------- ' Device Settings ' ------------------------------------------------------------------------- DEVICE SX28, OSC4MHZ, TURBO, STACKX, OPTIONX FREQ 4_000_000 ID "ISRPORTB" ' ------------------------------------------------------------------------- ' IO Pins ' ------------------------------------------------------------------------- Buttons VAR RB ' button inputs TRIS_Btns VAR TRIS_B Display VAR RC ' 7-segment LED TRIS_Disp VAR TRIS_C ' ------------------------------------------------------------------------- ' Constants ' ------------------------------------------------------------------------- Dash CON %01000000 ' - Dig1 CON %00000110 ' 1 Dig2 CON %01011011 ' 2 Dig3 CON %01001111 ' 3 Dig4 CON %01100110 ' 4 LtrE CON %01111001 ' E(rror) ' ------------------------------------------------------------------------- ' Variables ' ------------------------------------------------------------------------- winner VAR Byte ' button pressed first ' ------------------------------------------------------------------------- INTERRUPT ' ------------------------------------------------------------------------- ISR_Start: WKPND_B = winner ' get winner Ch1: ' check channel IF winner <> %0001 THEN Ch2 ' if not, try next Display = Dig1 ' otherwise display GOTO ISR_Exit Ch2: IF winner <> %0010 THEN Ch3 Display = Dig2 GOTO ISR_Exit Ch3: IF winner <> %0100 THEN Ch4 Display = Dig3 GOTO ISR_Exit Ch4: IF winner <> %1000 THEN Uh_Oh Display = Dig4 GOTO ISR_Exit Uh_Oh: Display = LtrE ' something went wrong ISR_Exit: WKEN_B = %11111111 ' no ISR until reset RETURNINT ' ========================================================================= PROGRAM Start ' ========================================================================= ' ------------------------------------------------------------------------- ' Program Code ' ------------------------------------------------------------------------- Start: Display = Dash ' indicate ready with dash TRIS_Disp = %00000000 ' display port --> outputs WKPND_B = %00000000 ' clear pending WKED_B = %11111111 ' falling edge detect WKEN_B = %11110000 ' use bits 0..3 for inputs END
Thank you
Comments
To restart the game, simply run the Start section of code again.
To know when to restart the game you will want to wait until someone wins, then make sure all of the buttons are released.
To know when someone wins the game, set a bit flag. Try adding “AlreadyWon VAR BIT” to your variable declaration section. Then add “AlreadyWon = 1” to the ISR_Exit section. (I think it is ok to treat an error condition as a winner in this case.)
Now all you need to do is continually check for a winner and then check for all the buttons to be released. Try adding this section of code right before the final “END” statement.
This might work one time. (I have not actually tested this. I am just sharing my thoughts to help point you toward one possible solution.) After that you might realize that you have not yet included a means to reset the winner flag once it has been set. To do this add “AlreadyWon = 0” to the Start section of code so that each time the game is (re)started the winner flag is cleared.
I do not know if this will actually work, but it should get you close. Let us know if you need more help.
- Sparks