Debounce Code ? one post to rule them all

Diablo

New member
debounce-waveform.jpg


Last month we asked you to send in your debounce code. You didn’t disappoint and it’s time to share the code received. There were some guideline for sending in code so if you don’t see yours here, it probably didn’t follow the rules, sorry. We also tried to weed out code that using delay loops for debounce. These tend to be a poor way to handle inputs because they monopolize the processor.

We wanted to add upvote/downvote buttons to each set of code to give some idea of a group consensus on code quality but there’s no good system available for multiple up/down vote widgets on one wordpress page. This results in a huge code dump for any one person to go through. If you’ve got any ideas on how to better organize this let us know: [email protected].

We make no guarantees that this code is safe to use, or that it even works. Test it carefully before using for important tasks.

Join us after the break for a whirlwind of code examples.

Ned’s Debounce Code

Ned sent in a package of debounce code that monitors multiple buttons, debounces them, and detects button hold and release.

Main.c:

/***************************************************************************************** * Project: Button Code * Version: V1.0 * Client: ProTechNZ * Date Created: 22/07/2010 * Date Modified: 23/07/2010 * Author: Neil van Geffen * Company: ProTech NZ * Micro: ATMega128 * Speed: 8MHz * Clock Source: Internal *****************************************************************************************//************************* Defines *******************************************************/#include #include #include #include #include "NedsStandardHeader.h"#include "C Files\buttons.h"#define BTN_UP 0x01#define BTN_DOWN 0x02#define BTN_ENTER 0x04#define BTN_CLEAR 0x08/************************* Structs and Enums *********************************************//************************* Function Prototypes *******************************************/unsigned char TimeYet(unsigned int time);unsigned int TimeDiff(unsigned int past, unsigned int future);void USART0SendByte(unsigned char byteToSend);void USART1SendByte(unsigned char byteToSend);void USART0SendArray(unsigned char *array, unsigned char noOfBytes);void USART0SendString(unsigned char *string);void USART0SendStringF(flash unsigned char *string);/************************* Global Variables***********************************************/unsigned char tempByte;unsigned int tempWord;unsigned char pA, pB, pC, pD;unsigned char releaseCounter;volatile unsigned int isrTime;unsigned int currentTime;unsigned int timeButtons;unsigned int clearPressed;/************************* Setup Code ****************************************************/void SetupPorts (void) { PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x04; // RXD1 (2) PORTE = 0x01; // RXD0 (0) PORTF = 0x00; DDRA = 0xF0; DDRB = 0xFF; DDRC = 0xFF; DDRD = 0xFF; // TXD1 (3) DDRE = 0x02; // TXD0 (1) DDRF = 0xFF;}// 1mS timervoid SetupTimers (void) { TCCR0 = (0 5) { //the button is ON } else { //the button is OFF } }}Ganesh Krishna’s Debounce Code

Can be scaled to any number of buttons, sample rate is configurable, and this aims to use as little RAM as possible

dbounce.c:

#include /* Scalable software debounce for buttons * * This is quick implementation of software debounce with as little RAM as possible. * stress is given to scalability of number of buttons * and scalability of number of samples per debounce cycle. * * While this file is written for PIC Microcontroller and Hi-tech compiler, * the debounce function itself is just C, and not hardware dependant. * * Use the functions at your own risk * Author: [email protected] * * Assume BSD like license. * Acknowledge with an email if it works for you. **//*Number of buttons in the system*/#define MAX_BUTTONS 4/* MAJORITY_VOTE Number of samples for which the button must remain in pressed state * to consider that the button is pressed. * */#define MAJORITY_VOTE 5/* STABILITY_BITS is the number of final samples (last few samples at the end of debounce) * after which we consider the input stable * */#define STABILITY_BITS 2/* Convert Stability bits to a bit mask, i.e STABILITY_MASK has STABILITY_BITS bits set in its LSB * */#define STABILITY_MASK ((1> 2); // 1 - (1/4) = .75 old += ((*port) & (1 0x70) ) { flag = 0; } // Newly detected falling edge else if ( (flag == 0) && (old < 0x07) ){ flag = 1; } // Update the state variable *state = (old & 0x7F) | ((flag & 0x01)
 
Back
Top