PDA

View Full Version : Debounce routine comments?



gebillpap
- 20th February 2008, 14:51
I needed to have some short of debouncing of an input in order to accept it in my program.
In my program I am looking if the input has a High(+5V) or Low(0V).
The input is driven by the contacts of a relay which cannot oscillate.(If this happened I believe this could cause the program to hang)

INPUTPORTBIT VAR PORTx.x
COUNTERA VAR BYTE
INPUTPORTBITFLAG VAR BIT
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' subroutine CHECKINPUTPORTBIT
CHECKINPUTPORTBIT:
COUNTERA=10
CKCK:
Pause 100
IF INPUTPORTBIT=0 Then
COUNTERA=COUNTERA+1
IF COUNTERA=20 Then WAYOUT1
Else
COUNTERA=COUNTERA-1
IF COUNTERA=0 Then WAYOUT2
EndIF
GoTo CKCK
WAYOUT1:
INPUTPORTBITFLAG=1 ''OK OUR INPUT WAS STABLE FOR AT LEAST 1 SEC
GoTo WAYOUT3
WAYOUT2:
INPUTPORTBITFLAG=0 ''NOT OK
WAYOUT3:
Return

I used this routine(it is working for me Ok since yesterday) which ,I think, has some nice features like making the debouncing ''light'' or ''heavy'' by altering the PAUSE and/or the COUNTERA start and limit value.
Perhaps someone can improve it or make it with less code?
Please comment
Bill

Acetronics2
- 20th February 2008, 15:03
Why not simply use the BUTTON Command ??? ... even in a loop for long debouncing times ...

Alain

Archangel
- 20th February 2008, 17:32
Hi Bill,
I agree with ACETRONICS about the button command, I have a thought about making it easier to follow your code, instead of all caps you might try InputPortBitFlag or Input_Port_Bit_Flag.
Ultimately only you have to look at it, I just thought it might be easier. :)
JS

gebillpap
- 20th February 2008, 19:56
Joe S your suggestion will be given a trial because it started annoying me also having only capitals but sometimes habits are difficult to change.
The reason for reinventing the wheel ,as you put it, is that from the very early days of PicBasicPro ther were some bugs and problems with this Button command and I was avoiding it to the point I realize now that I forgotten all about it.
Having a look though in the manual I think it is not providing the same things as the "wheel".
It is also peculiar that the manual ''prompts'' you not to use it but use an IF THEN instead.
Now that I "apologized" I am waiting for the verdict.
Bill