I am looking for the perfect way to scan switches with pic 16f84
I am using this program :
SCAN btfss porta,0
goto SCAN
I faced the problem with this subroutine, the problem is : when I turn on the lamp of my bedroom then pic 16f84 achieve the scannig prosses without any push of the switch that lead me to say that the scaning routines affected by external sparks like turn on lamp and turning on TV, would you please help me to cover this problem.
This is the way you scan the switches.
But do you have a strong enough pullup or pulldown on that input?
Sometimes an RC filter on the pin can help, too.
Search EDA board for "debounce" for more info but this is short explanation:
This code is written from head and it's not tested
Code:
CNT res 1
movlw 255 ;time for debounce
movf CNT,w
SCAN
btfss PORTA,0
goto SCAN
DEB
decfsz CNT ;wait for debounce time to run out
goto DEB
btfss PORTA,0 ;test again
goto SCAN ;if not, it's false signal, goto SCAN
; here you do job for presed button
If you want to scan an input attatched with a switch, you have to do two things primarily:
1st: You have to attach a pull-up, or pul-down resistor with the input for stability, you can use 10Kohm, the first end of it with 5Vdc and the other to the PIC input, now R is called pull-up resistor, and your switch must be attached with the input pin and the ground.
2nd: You have to take into your account the debounce problem, which it comes when you push the switch, so you have to detect the switch after an enough delay time, maybe 20 ms, then decide if the switch in on or off.
Lastly, I suggest you to use the RB7-RB4, as on-change interrupt pins...
This is the way you scan the switches.
But do you have a strong enough pullup or pulldown on that input?
Sometimes an RC filter on the pin can help, too.
I attached 10K resistor to +5V then I attached the other side of resistor to the pic and the first pin of the switch on the other hand I attached the second side of the switch to Ground, but the problem is still.
the problem is when I turn on the lamp of my bedroom pic 16f84 achieve scaning without any press of the switch.
Would you please let me know how to make RC Filter and how to conect it?
hmm, btfss skips the next instruction if the specified pin is at logic 1, that should loop while your switch is held in and continue through the program when it is released.
Do you have the specified decouping capacitors close to the pic?
hmm, btfss skips the next instruction if the specified pin is at logic 1, that should loop while your switch is held in and continue through the program when it is released.
Do you have the specified decouping capacitors close to the pic?
The PIC power needs to be decoupled: use at least a 0.1uF cap between its Vdd and Vss pins, as close to the package as possible.
Do not forget to tie the /MCLR pin to Vcc through a resistor and maybe add a 0.1uF cap to GND. Perhaps the PIC is resetting, but you think it's just sensing the button as being pressed.
As for the RC filter, try first adding a cap 10nF-100nF from the pin to GND.