Jump to content
43oh

trying to understand software debounce


Recommended Posts

I found this topic - http://forum.43oh.com/topic/2025-software-debouncing/?hl=debounce - which lead me to this pdf - http://www.eng.utah.edu/~cs5780/debouncing.pdf - from which the code on this page - https://github.com/lucsmall/MSP430-Launchpad-Clock/blob/master/main.c - seems to take it's debounce routine.

 

I've read that through it and I don't understand something. Going on the code in the clock example...

 

inline void update_switches(void)
{
   static unsigned char state[2] = {0xFF, 0xFF}; // Current debounce status
  state[0] = (state[0] << 1) | ((SWITCH_IN & SWITCH_0) ? 1 : 0);
   state[1] = (state[1] << 1) | ((SWITCH_IN & SWITCH_1) ? 1 : 0);
   if (state[0] == 0x80) {
   switch0_state = 1;
   }
   if (state[1] == 0x80) {
   switch1_state = 1;
   }
}

 

...every time the function is called, the variable state is initialized and set to 255.  Depending on the state of the switch, either a 1 or a 0 is shifted into the variable state. That variable is then tested against a preset value to determine the edge. That I understand.

 

What I don't understand is if the variable is reset to 255 every time the routine is called, how does the value of the variable ever reach 0x80?

 

I hope this isn't too stupid of a question... Thanks.

Link to post
Share on other sites

It is declared as a "static" variable inside the function; this is a special keyword denoting that it's persistent across subsequent calls to the function (like a global variable, but private only to that function). I think it only gets initialized the first time it's called.

 

Sent from my C3PO via Tapatalk

 

 

Link to post
Share on other sites

It is declared as a "static" variable inside the function; this is a special keyword denoting that it's persistent across subsequent calls to the function (like a global variable, but private only to that function). I think it only gets initialized the first time it's called. Sent from my C3PO via Tapatalk

Correct.

Link to post
Share on other sites

In general, that's not very good code, because it isn't reusable. Declaring your state as static variables inside the function means that the state is shared. So if one day you decide to add another set of buttons, you won't be able to use the same function for both sets.

 

A better solution is to have the state passed as a function parameter, to be modified by the function. My debouncing function looks like this:

 

unsigned int debounce(unsigned int signal, unsigned int *integrator, unsigned int previous_output);

 

where *integrator is a pointer to a state variable, supplied by the caller. This function may be called from multiple places with no conflicts.

Link to post
Share on other sites

It is declared as a "static" variable inside the function; this is a special keyword denoting that it's persistent across subsequent calls to the function (like a global variable, but private only to that function). I think it only gets initialized the first time it's called. Sent from my C3PO via Tapatalk

If you disassemble the function you probably won't see any conditional initialization code.  For C code like this normal practice is to initialize the values during startup, same as with any global variable that has an initializer, so it's actually initialized long before the function is called.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...