We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I have done software debounce logic with 2/3 voter logic and kindly verify the coding i don't no whether it is correct or not. Please help me
Coding:
void inputRoutine() { uint16_t temp; InputP1_Temp=P1&0x00; for(Index1=0;Index1<MAXINPUTS;Index1++) { temp=debounce(Index1); if(temp==1) { Input_TableP1[Index1]=1; } else if(temp==0) { Input_TableP1[Index1]=0; } } } /***************** -- Debounce Port P1 -- *****************/ bit debounce(uint16_t IN_INDEX) { uint16_t INDEX2,ONE=0,ZERO=0; char TEMP,SHIFT_RESULT[3]=0x00; for(INDEX2=0;INDEX2<3;INDEX2++) { TEMP= P1; SHIFT_RESULT[INDEX2]=(TEMP>>IN_INDEX)& 0x01; delay_10micro_sec(); } /* Comparision of 3 values retreived from same pin*/ for(INDEX2=0;INDEX2<3;INDEX2++) { if(SHIFT_RESULT[INDEX2]==1) ONE++; else if(SHIFT_RESULT[INDEX2]==0) ZERO++; } return (ONE>ZERO); }
With Thanks, G.Karthik Ragunath
[continued]
/* **====================================================================== ** Check_Button [ ACCESSOR ] **====================================================================== ** ** Checks if the signal selected is asserted by simply accessing the ** Button_Scan assigned data-stores. ** **---------------------------------------------------------------------- ** ** Parameters Passed: (u8) button number #defined ** Parameters Returned: (bit) TRUE = Asserted ** Notes: ** 1) The lack of new-lines and open/close braces were ** imposed due to the easy addition/deletion of 'buttons' ** ** 2) Doesn't return TRUE upon detection of a new assertion. ** (modify if you need that included) ** **---------------------------------------------------------------------- */ bit Check_Button( u8 button ) { u8 pressed; pressed = FALSE; // although assigned in the switch( ), ensure it here /*------------------------------------------------------------------. ; If the selected input is TRUE, return TRUE (else FALSE) ; '------------------------------------------------------------------*/ switch( button ) { case BUTTON_LOCK: if( Lock_Signal == TRUE ) pressed = TRUE; break; case BUTTON_BEEP: if( Beep_Signal == TRUE ) pressed = TRUE; break; default: pressed = FALSE; break; } /*------------------------------------------------------------------. ; (bit) { TRUE | FALSE } ; '------------------------------------------------------------------*/ return( pressed ); } /* **====================================================================== ** main **====================================================================== ** ** The Ultra-Lame main-line routine to illustrate the [very common] ** debounce algorithm. ** **---------------------------------------------------------------------- ** ** Parameters Passed: <void> ** Parameters Returned: <void> ** Notes: ** ** This was extracted from prior artwork and NOT compiled, ** so if this doesn't compile without errors, I don't care: its ** an example of the algorithm and its usage. This algorithm ** as worked for me in the past, so it might work for you. ** **---------------------------------------------------------------------- */ void main( void ) { bit button; // for testing the state of the button /*------------------------------------------------------------------. ; Initialize the system's states ; '------------------------------------------------------------------*/ Lock_Signal = FALSE; Beep_Signal = FALSE; New_Lock = FALSE; New_Beep = FALSE; /*------------------------------------------------------------------. ; Do it forever ; '------------------------------------------------------------------*/ while( GOD_PARTICLE != FOUND ) { /*--------------------------------------------------------------. ; This is typically put into a timer ISR for a predictable dt ; '--------------------------------------------------------------*/ Delay_X_mS( KEYBOARD_DEBOUNCE_5mS_TICKS ); // you do the routine Scan_Buttons( ); /*--------------------------------------------------------------. ; Is there somebody at the door? ; '--------------------------------------------------------------*/ button = Check_Button( BUTTON_BEEP ); if( button != FALSE ) { beep( ); // Wake up the sentry } /*--------------------------------------------------------------. ; If they are to be 'buzzed in' then do it. ; '--------------------------------------------------------------*/ button = Check_Button( BUTTON_LOCK ); if( button != FALSE ) { Unlock( ); // Button must be held while opening door } else { Lock( ); // make sure it returns to the secured position } } } /*=========================================================================== * END OF MODULE ( Debounce_Example.C ) ===========================================================================*/
(if you do find errors, please let me know).
--Cpt. Vince Foster 2nd Cannon Place Fort Marcy Park, VA
hmmmm...you know...there is a lot to be said for a resistor and a cap next to the switch!
R
A resistor and a cap will change the behaviour. But if the input pin doesn't have schmitt-trigger, then the bounces can still get the filtered signal to vary enough to make the input signal toggle multiple times between low and high.
As a matter of fact - all uses of external RC filters should be avoided unless the input has hysterese. Not only can you get multiple toggles, you can also get the input to consume a lot of power when floating in the dead zone between a logic high and a logic low.
Besides: A software bug can be corrected by a firmware update. A hw problem - such as too high/low value on a component - is very hard to compensate for. You may have to send the unit in for modification.
The important thing here is that most mechanical switches are quite easy to debounce since the bounce time is short in relation to the expected number of switch activations/second. And since the debounce is so easy to do in software, there is no real need to figure out ways to let the hardware help.
what about this debounce logic for 3 inputs.
#include<stdio.h> #include <REGX52.H> /* This code is for button debouncing.Here 3 buttons are considered and each button have individual buffer and each buffer is compared for 2/3 logic and stored in input table. */ void inputRoutine(); void debounceLogic(); unsigned int compareBuffer(unsigned int[]); unsigned int inputBuffer1[3],inputBuffer2[3],inputBuffer3[3],debounceStatus; unsigned int bufSize,bufIndex,shiftIndex,inputTable[3],inputIndex; void main() { bufSize=3; bufIndex=0; shiftIndex=0; count=0; while(1) { inputRoutine(); } } void inputRoutine() { debounceLogic(); if(bufSize==bufIndex) { bufIndex=0; } else { bufIndex++; } } /*Here Port P0 is used and Pins P0.0 ,P0.1 and P0.2 is used*/ /*Each Pinvalues are stored in inputBuffer. And compared for 2/3 logic*/ void debounceLogic() { inputBuffer1[bufIndex]=(P0>>shiftIndex)&0x01; inputTable[shiftIndex]=compareBuffer(inputBuffer1); shiftIndex++; inputBuffer2[bufIndex]=(P0>>shiftIndex)&0x01; inputTable[shiftIndex]=compareBuffer(inputBuffer2); shiftIndex++; inputBuffer3[bufIndex]=(P0>>shiftIndex)&0x01; inputTable[shiftIndex]=compareBuffer(inputBuffer3); shiftIndex++; shiftIndex=0; inputIndex=0; } unsigned int compareBuffer(unsigned int tempBuffer[]) { unsigned int one=0,zero=0,i; for(i=0;i<3;i++) { if(tempBuffer[i]==1) { one++; } else { zero++; } } if(one>zero) { return(1); } else { return(0); } }
thanks,
As a matter of fact - all uses of external RC filters should be avoided unless the input has hysterese. one more reason to avoid RC filters is that it slows down the input. Many moons ago (before the micro, with a mini) I did some investigation on RC filtering and found that (for this particular app, how general I do not know) sufficient RC filtering made the thingy fail to respnd to the "fastest fingers"
And since the debounce is so easy to do in software, there is no real need to figure out ways to let the hardware help. I second that opinion
Erik
Debounce? I see no such thing (unless the switch has a bounce time in nano or microseconds.
the surefire way to debounce is
timer reloading at bounce time set flag47
in the main
if flag47 read the switches to newswitches
compare newswitches to oldswitches if identical --compare oldswitch to sentswitch --if different ----set newswitch flag (to be reset in switch processor) -------- switch processor reads sentswitch ----set sentswitch to oldswitch if different set oldswitch to newswitch