View previous topic :: View next topic |
Author |
Message |
nerdnicky
Joined: 02 Aug 2013 Posts: 13
|
button debounce not working |
Posted: Fri Aug 02, 2013 12:43 pm |
|
|
hey guys ...
i have been trying this code for debouncing button but its not working ....
///////////////////////////////////////////////////////////////////////////////////
#include <16f684.h>
#fuses NOWDT,NOMCLR
#use delay(clock=400000)
int i;
const int twentyms =1150;
main()
{ while(1)
i=0;
while(i<twenty
{ if(input(PIN_A0)==0)
{ i=0;
}
else
{ i=i+1;
}
i=0;
while(i<twentyms)
if(input(PIN_A0)==0)
i=0;
else
i=i+1;
output_toggle(PIN_C0);
}
}
////////////////////////////////////////////////////////////////////////////////
can anybody help me to correct this code...
may god bless you.... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Aug 02, 2013 1:00 pm |
|
|
Your program isn't even compilable. You have missing parentheses.
It's not worth working on. Instead, look at the links in this post.
It has links to several programs for debouncing switches or buttons:
http://www.ccsinfo.com/forum/viewtopic.php?t=42285 |
|
|
nerdnicky
Joined: 02 Aug 2013 Posts: 13
|
|
Posted: Fri Aug 02, 2013 1:50 pm |
|
|
a million Thanks sir...
i used your code for the processor 16f684 its working but...
i am a little confused about the behavior of output_toggle()
function ...my button is active-low and uses a pullup...
the first time i press the button the led lights up and remains on ..next time when i press the button it switches off... is the same behavior expected of the functon output_toggle()......please help me out....
Code: |
#include <16F684.H>
#fuses NOWDT, NOPROTECT
#use delay(clock = 4000000)
#define BUTTON_PIN PIN_A1 // This pin must have a pull-up.
#define DEBOUNCE_PERIOD_IN_MS 10
#define DEBOUNCE_COUNT 2
void my_function(void);
void wait_for_keypress(void);
//====================================
void main()
{
while(1)
{
wait_for_keypress();
my_function();
}
while(1);
}
//==================================
void my_function(void)
{ output_toggle(PIN_C0);
}
//-------------------------------
void wait_for_keypress(void)
{
char count;
// First, wait for the button to be released. With the debounce
// values as given above, the button must be in the "up" state
// for two consecutive readings, spaced 10 ms apart.
count = 0;
while(1)
{
if(input(BUTTON_PIN) == 1)
count++;
else
count = 0;
if(count == DEBOUNCE_COUNT)
break;
delay_ms(DEBOUNCE_PERIOD_IN_MS);
}
// Now that the button is up, wait until the user presses it.
// In order for the keypress to be considered valid, based
// on the debounce values listed at the beginning of the
// program, the button must be held down for two consecutive
// readings, spaced 10 ms apart.
count = 0;
while(1)
{
if(input(BUTTON_PIN) == 0)
count++;
else
count = 0;
if(count == DEBOUNCE_COUNT)
break;
delay_ms(DEBOUNCE_PERIOD_IN_MS);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Aug 02, 2013 2:06 pm |
|
|
With the output_toggle() function, every time you call it, the LED should
switch to the opposite state. If it was on, it will go off. If the led was off,
then it will turn on. |
|
|
nerdnicky
Joined: 02 Aug 2013 Posts: 13
|
|
Posted: Fri Aug 02, 2013 2:30 pm |
|
|
sir.... i ..am very weak at coding ... sorry i disturbed you again..
i wanted to ask that whether the output_toggle().. automatically
( like blinking) toggles the output at a rate determined by the clock frequency...on pressing the button....or it makes the output behave like a 1_bit latch... when the button is pressed once ,a state is entered, and this state flips over when the button is pressed the next time... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Fri Aug 02, 2013 3:07 pm |
|
|
As PCM_P said in his last post.
It does NOT 'blink' or 'flash'..
It is either solidly ON or solidly OFF.
'toggle' means to go to the alternate state from previous.
same as a 'toggle type flipflop'.
0-->1, 1-->0.
if it was a zero(off) it becomes a one(on), if it was a one(on) it becomes a zero(off).
hth
jay |
|
|
|