I'm sure everybody here has at some stage dealt with switch debounce...
I tried my first switch (membrane) a while back with method 1, using a pull-up (10k) across switch and +5V and other switch side to RB4
now I want to use portB weak internal pull-ups and pull down ( 10k) to gnd on other microswitch side, method 2.
I understand the concept of switch debounce and have writen code (MICROCHIP .asm) that would use a set count value to debounce the switch, not working.
To avoid switch bounce, I recommend scanning the key switches at a very slow sampling rate from 10-40 times per second. The slower the better but not too slow so that the buttons appear to be sluggish. A person will not perceive a 50msec delay in response from the buttons.
Code:
READ_KEYPORT:
DECFSZ KEY_TIMER,F
RETURN
;
COMF KEYBUFF,W
MOVWF KEYEDGE
;
MOVF KEY_PORT,W
MOVWF KEYBUFF
ANDWF KEYEDGE,F
;
; <--- Insert code here to process key press
;
RETURN
;
motion your code looks amazingly simple, are you intimating that the KEY_TIMER register is set beforehand, or is it on a 1/256 loop at which point it takes the compliment of the KEYBUFF and moves it into KEYEDGE, reads port activity to KEYBUFF then AND's it with KEYEDGE, saving it in KEYEDGE.
I havn't figured out what part of the STATUS register I should go hunting in yet but I think I get it, it needs to have identical consecutive readings which over a 25 to 100 ms interval, constitutes a valid read. :wink:
I implemented this routine and it's the Z flag in STATUS that one checks.
motion how do the RB weak internall pull-ups operate? Should one set the TRISA input first, or the RPBU' pull-ups...and when set, does it continue to try and hold the RB pins high exept when grounded, 'cause I want to understand the methodology of connecting the switch to gnd, does one need a 10k pull-down to gnd :?:
motion your code looks amazingly simple, are you intimating that the KEY_TIMER register is set beforehand, or is it on a 1/256 loop at which point it takes the compliment of the KEYBUFF and moves it into KEYEDGE, reads port activity to KEYBUFF then AND's it with KEYEDGE, saving it in KEYEDGE.
The READ_KEYPORT routine is called every 256usec from the main program loop which polls the TIMER0 for overflow. Since this is too fast for key scanning, the KEY_TIMER further scales this down to 65msec.
The routine looks for the falling edge and sets the corresponding bit in KEYEDGE. This allows simultaneous scanning of 8 switches.
Below, illustrates how the routine filters out key bounce. The scanning rate is slower than the frequency of bounce and checks the switches after the key switch has stopped bouncing.
I know I am going on about this, but it is realy annoying me...here are the two methds that I proposed previously, method 2 being the preferred option using weak internal pull-ups inside '84a. I have tried setting the TRISB and OPTION RPBU' bit's in all combinations, but I cannot read an input with this method, at all. Could it be due to the battery pack I am prototyping with (low power) or is it most likely that my chip is rooted :cry:
I've read everything on the net and tried so many things, I have tons of code I've already writen that is weeping for these buttons...damn electrons.