-
Better Pushbutton
I'm using a very good quality normally open switch to change HPWM for an array of LEDs. This switch has some sort of compound action where the contacts snap closed when the button is pressed. A lot of times I press the switch and nothing happens. I've played with the pause time in the code and changed it to different settings from 1 ms to 100 ms, but it doesn't seem to make much difference. Is there are more reliable code for a switch like this? Here's the code:
~ Dave
Code:
@ device pic16F628A, INTRC_OSC_NOCLKOUT, wdt_off, mclr_off, lvp_off, protect_off
CMCON = 7
CounterA Var Byte
CounterA=0
start:
HPWM 1,64,2000 ' hardware pulse width modulate channel 1, 25% duty, 2khz
goto Main
Main:
If PORTA.1=0 then
If CounterA<=1 then
CounterA=CounterA+1
else
CounterA=0
endif
endif
While PORTA.1=0 : Wend : pause 10
BRANCHL CounterA,[LabelOne,LabelTwo]
Goto Main
LabelOne:
HPWM 1,64,2000 ' hardware pulse width modulate channel 1, 25% duty, 2khz
goto Main
LabelTwo:
HPWM 1,254,2000 ' hardware pulse width modulate channel 1, 99.6% duty, 2khz
goto Main
-
What pull-up resistor value do you have?
Just for fun try this one
Code:
CMCON = 7
DutySelect var bit
Dutyselect = 0
gosub ChangeDuty
Main:
If PORTA.1=0 then
pause 20
dutyselect=dutyselect ^1
gosub ChangeDuty
while porta.1=0 : Wend
Pause 20
endif
goto main
ChangeDuty:
If DutySelect then
hpwm 1,254,2000
else
hpwm 1,64,2000
endif
return
-
Have you check contact resistance of the switch when it is pressed? What kind of contact surfaces the switch has?
With too strong pull-ups you might get this kind of problems if switch is unreliable (leave on some high ohmic state even when pressed).
BR,
-Gusse-
-
I'm using 10k pullups. How high should I go?
Contact resistance is 0.046 ohms and the switch is a very high quality, high reliability switch. The contacts are either closed or not.
OK, I'll try mister_e's version.
Thanks!
~ Dave
-
OK, mister_e's program worked MUCH better .... why is that?
Thank you once again!
~ Dave
-
Mine don't refresh the PWM duty cycle all the time, it will do IF a push button is pressed.
The main difference is how the switch debounce is made. Let's use a Google Image
http://www.micahcarrick.com/files/at...3/debounce.png
This is what the signal looks like when you press your switch, the same noise should also appear on the rising edge when you release the switch.
A little pause after you press on the switch is probably not necessary because your code need some time to execute, but 20mSec for safety sake that's nothing huh?
If you had more code after the switch detection, you could reduce the second PAUSE.
HTH