Need Help With Switch De-Bounce
NAGCS
Posts: 2
Here is a project I started a few days ago for the XBOX 360 The circuit uses RGB LED's Port RA.3 is used for switching patterns or colors. 3 Lines on the RB Ports switch the grounds on the RGB LED's. Port RC Controls the common Postive terminal of the RGB LED. I am having a big problem with switch bounce when I press the button I want to go cleanly to the next pattern not jump around. I will show some pictures then show the code if any one can help It would be great.
'
' Device Settings
'
DEVICE········· SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ··········· 4_000_000
'
' IO Pins
'
Select··VAR·RA.3···' Pattern Select
TRIS_Sel·VAR·TRIS_A
Blue··VAR·RB.0
Green··VAR·RB.1
Red··VAR·RB.2
Light··VAR·RC
TRIS_Light·VAR·TRIS_C
'
' Constants
'
Delay·· ·CON·100···' 180 ms between steps
Delay1··CON·80···' 220 ms between steps
'
' Variables
'
maxSteps·VAR·Byte···' steps in sequence
idx··VAR·Byte···' step pointer
' =========================================================================
· PROGRAM Start
' =========================================================================
Pattern0:
· DATA· 8
· DATA·%11111110
· DATA·%11111101
· DATA·%11111011
· DATA·%11110111
· DATA·%11101111
· DATA·%11011111
· DATA·%10111111
· DATA·%01111111
Pattern1:
· DATA· 8
· DATA·%01111111
· DATA·%10111111
· DATA·%11011111
· DATA·%11101111
· DATA·%11110111
· DATA·%11111011
· DATA·%11111101
· DATA·%11111110
'
' Program Code
'
Start:
· Light = %00000000
· TRIS_Light = %00000000
Main:
· PAUSE Delay
· HIGH Red
· READ Pattern0. maxSteps
IF idx <= maxSteps THEN S2_Go
· idx = 1
S2_Go:
· READ Pattern0 + idx, Light
· idx + idx + 1
· TOGGLE Red
· PAUSE 200
· HIGH Green
· READ Pattern0. maxSteps
IF idx <= maxSteps THEN S3_Go
· idx = 1
S3_Go:
· READ Pattern0 + idx, Light
· idx + idx + 1
· TOGGLE Green
· PAUSE 200
· HIGH Blue
· READ Pattern0. maxSteps
IF idx <= maxSteps THEN S4_Go
· idx = 1
S4_Go:
· READ Pattern0 + idx, Light
· idx + idx + 1
· TOGGLE Blue
· PAUSE 200
· IF Select = 0 THEN Main0
· PAUSE 50
· GOTO Main
Main0:
· PAUSE Delay1··
· HIGH Green
· READ Pattern0, maxSteps
· IF idx <= maxSteps THEN S5_Go
· idx = 1
S5_Go:
· READ Pattern0 + idx, Light
· idx = idx + 1
· PAUSE 25
· IF Select = 0 Then Main1·········
· GOTO Main0
Main1:
· PAUSE Delay1··
· HIGH Blue
· READ Pattern0, maxSteps
· IF idx <= maxSteps THEN S6_Go
· idx = 1
S6_Go:
· READ Pattern0 + idx, Light
· idx = idx + 1
· PAUSE 25
· IF Select = 0 Then Main2········
· GOTO Main1
Main2:
· PAUSE Delay1··
· HIGH Red
· READ Pattern0, maxSteps
· IF idx <= maxSteps THEN S7_Go
· idx = 1
S7_Go:
· READ Pattern0 + idx, Light
· idx = idx + 1
· PAUSE 25
· IF Select = 0 Then Main3··········
· GOTO Main2
Main3:
· PAUSE 200
· If Select = 0 THEN Main
· Light = %11111111
· GOTO Main3
·
'
' Device Settings
'
DEVICE········· SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ··········· 4_000_000
'
' IO Pins
'
Select··VAR·RA.3···' Pattern Select
TRIS_Sel·VAR·TRIS_A
Blue··VAR·RB.0
Green··VAR·RB.1
Red··VAR·RB.2
Light··VAR·RC
TRIS_Light·VAR·TRIS_C
'
' Constants
'
Delay·· ·CON·100···' 180 ms between steps
Delay1··CON·80···' 220 ms between steps
'
' Variables
'
maxSteps·VAR·Byte···' steps in sequence
idx··VAR·Byte···' step pointer
' =========================================================================
· PROGRAM Start
' =========================================================================
Pattern0:
· DATA· 8
· DATA·%11111110
· DATA·%11111101
· DATA·%11111011
· DATA·%11110111
· DATA·%11101111
· DATA·%11011111
· DATA·%10111111
· DATA·%01111111
Pattern1:
· DATA· 8
· DATA·%01111111
· DATA·%10111111
· DATA·%11011111
· DATA·%11101111
· DATA·%11110111
· DATA·%11111011
· DATA·%11111101
· DATA·%11111110
'
' Program Code
'
Start:
· Light = %00000000
· TRIS_Light = %00000000
Main:
· PAUSE Delay
· HIGH Red
· READ Pattern0. maxSteps
IF idx <= maxSteps THEN S2_Go
· idx = 1
S2_Go:
· READ Pattern0 + idx, Light
· idx + idx + 1
· TOGGLE Red
· PAUSE 200
· HIGH Green
· READ Pattern0. maxSteps
IF idx <= maxSteps THEN S3_Go
· idx = 1
S3_Go:
· READ Pattern0 + idx, Light
· idx + idx + 1
· TOGGLE Green
· PAUSE 200
· HIGH Blue
· READ Pattern0. maxSteps
IF idx <= maxSteps THEN S4_Go
· idx = 1
S4_Go:
· READ Pattern0 + idx, Light
· idx + idx + 1
· TOGGLE Blue
· PAUSE 200
· IF Select = 0 THEN Main0
· PAUSE 50
· GOTO Main
Main0:
· PAUSE Delay1··
· HIGH Green
· READ Pattern0, maxSteps
· IF idx <= maxSteps THEN S5_Go
· idx = 1
S5_Go:
· READ Pattern0 + idx, Light
· idx = idx + 1
· PAUSE 25
· IF Select = 0 Then Main1·········
· GOTO Main0
Main1:
· PAUSE Delay1··
· HIGH Blue
· READ Pattern0, maxSteps
· IF idx <= maxSteps THEN S6_Go
· idx = 1
S6_Go:
· READ Pattern0 + idx, Light
· idx = idx + 1
· PAUSE 25
· IF Select = 0 Then Main2········
· GOTO Main1
Main2:
· PAUSE Delay1··
· HIGH Red
· READ Pattern0, maxSteps
· IF idx <= maxSteps THEN S7_Go
· idx = 1
S7_Go:
· READ Pattern0 + idx, Light
· idx = idx + 1
· PAUSE 25
· IF Select = 0 Then Main3··········
· GOTO Main2
Main3:
· PAUSE 200
· If Select = 0 THEN Main
· Light = %11111111
· GOTO Main3
·
Comments
That project looks awesome!
The easiest thing to do is wait for a millisecond between checking to see if a key is pressed.
That is a conservative value and should work fine, but you can play around with reducing the delay to find out what the actual timescale of bouncing is.
Good Luck.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I wonder if this wire is hot...
NAGCS,
I see you have PAUSE 200, but I don't quite follow your select logic ? Is select = 0 when the button is pressed or does it equal 1 when the button is pressed ?
BTW: If you download the latest version of SX/B you use IF...THEN...ENDIF blocks instead of all the GOTOs and labels. Also has DO...LOOP loops. Would make the code alot easier to follow.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheap 4-digit LED display with driver IC·www.hc4led.com
COMING SOON "SD DATA LOGGER" www.sddatalogger.com
"I reject your reality, and substitute my own." Mythbusters
·
After the PAUSE 25, you are saying if the button is pressed then goto Main3.
So let's assume the button is pressed, now we are at Main3, if the user releases
the button during the PAUSE 200 you will be stuck in the Main3 loop forever.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheap 4-digit LED display with driver IC·www.hc4led.com
COMING SOON "SD DATA LOGGER" www.sddatalogger.com
"I reject your reality, and substitute my own." Mythbusters
·