Switch Debounce Code

65 posts / 0 new
Last post
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

Can anyone suggest me a sample code for Switch debounce ?

Iam a newbie to C programming in AVR.
Iam using AT90CAN128 and AVRStudio 4.12.460.

Thank You!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here is a fat one:

char debouncePA1(void)
{
  char ones=0, zeroes=0, i;
  for(i=0;i<9;i++){
    if(PINA&0x01){ // read pin == 1
      ones++;
    } else { // read pin == 0
      zeroes++;
    }
  _delay_ms(10);
  }
  return ones>zeroes;
}

I really prefer a simple hardware debouncer (RC filter). :?

___(°)^(°)___
A(VR)staroth

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here is the best de-bouncing guide I managed to find:
www.ganssle.com/debouncing.pdf
HTH,

Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Astaroth wrote:
Here is a fat one:

char debouncePA1(void)
{
  char ones=0, zeroes=0, i;
  for(i=0;i<9;i++){
    if(PINA&0x01){ // read pin == 1
      ones++;
    } else { // read pin == 0
      zeroes++;
    }
  _delay_ms(10);
  }
  return ones>zeroes;
}


This code will overflow. :(

---------------------
John Firestone

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Peter Fleury has these examples on his homepage

http://jump.to/fleury

http://homepage.hispeed.ch/peterfleury/avr-software.html

http://homepage.hispeed.ch/peterfleury/avrgcc-examples.zip

The debounce is based on code from Peter Danegger if i remember correctly , and is quite neat.

Btw: His Uart & LCD lcd libs are also worth a look.

/Bingo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
/*

THE ULTIMATE SOFTWARE DEBOUNCER			

Program: debounce.asm
Version: 1.0
Date:    5/5/2005
Author:  Elio Mazzocca
E-mail:  jnz9876@adam.co.uk

micro used: ATmega8 with internal 4 MHz oscillator
assembled with: AVR Studio 4.0
Timer0 interrupt period: 4 mSEC

Description: This is a 1st order recursive digital filter
	with Schmitt trigger output that filters noisy
	digital inputs to ATmega8 microcontroller.
	The formula employed for the recursive filter:

	ynew = 1/4 xnew + 3/4 yold

	Formula for an inverting Schmitt trigger:

	if(ynew>hi) and (flag=0) then flag=1; vout=1;
	if(ynew

Sorry, dont have original link.

J.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello!
Here is my code for two switches (UP and DOWN).

// ---------- In header file ----------
#define KEY_UP			6
#define KEY_DN			7

// Pins
#define KEY_PINS       PIND

// Macros
#define KEY_DN_PRESS	(~(KEY_PINS) & (1<<KEY_DN))
#define KEY_UP_PRESS	(~(KEY_PINS) & (1<<KEY_UP))

// ---------- In main file ----------
int8_t read_keys(void) {
	if(KEY_UP_PRESS) {
		_delay_ms(10);
		while(KEY_UP_PRESS);		// Wait for key to be released
		_delay_ms(10);
		return 1;
	}
	if(KEY_DN_PRESS) {
		_delay_ms(10);
		while(KEY_DN_PRESS);
		_delay_ms(10);
		return -1;
	}
	else return 0;
}

Simple code, but the AVR will hang in the read_keys function as long as one of the buttons is pressed.
That's no problem in my app, interrupts are doing the housekeeping.

Regards,
Thomas

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Thomas!
Maybe the code you posted works ok for your app., but it's definitely NOT the way to go for a switch debounce code...
I recommend Jack G. Ganssle de-bouncing guide (it has also C-code examples) and the code suggested by xjedlins (asm code by Elio Mazzocca).

Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

FYI :idea:

http://www.edn.com/article/CA621638.html?spacedesc=DesignIdeas&taxid=10577

link to asm posted previously and also some other interesting links at the bottom of the article :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Most versions of debouncers are only able to debounce a single key.

My approach need extremely less code and time to debounce 1 ... 8 keys simultaneosly and independent and bulletproofed:

/************************************************************************
;*                                                                      *
;*              Testing Read and debounce up to 8 keys                  *
;*              Bulletproof: 4 equal samples needed                     *
;*                                                                      *
;*              Author: P. Dannegger                                    *
;*                                                                      *
;***********************************************************************/
#include 
#include 
#include 

#define KEY_INPUT       PINC
#define LED_OUTPUT      PORTB
#define LED_DIR         DDRB

char key_state;                         // debounced and inverted key state:
                                        // bit = 1: key pressed
char key_press;                         // key press detect


SIGNAL (SIG_OVERFLOW0)
{
  static char ct0, ct1;
  char i;

  i = key_state ^ ~KEY_INPUT;           // key changed ?
  ct0 = ~( ct0 & i );                   // reset or count ct0
  ct1 = ct0 ^ (ct1 & i);                // reset or count ct1
  i &= ct0 & ct1;                       // count until roll over ?
  key_state ^= i;                       // then toggle debounced state
                                        // now debouncing finished
  key_press |= key_state & i;           // 0->1: key press detect
}


char get_key_press( char key_mask )
{
  cli();
  key_mask &= key_press;                // read key(s)
  key_press ^= key_mask;                // clear key(s)
  sei();
  return key_mask;
}


int main( void )
{
  key_state = 0;
  key_press = 0;

  TCCR0 = 1<<CS02;                      //divide by 256 * 256
  TIMSK = 1<<TOIE0;                     //enable timer interrupt

  LED_DIR = 0xFF;
  LED_OUTPUT = 0xFF;
  sei();
  for(;;)                                       // main loop
    LED_OUTPUT ^= get_key_press( 0xFF );        // toggle LEDs on key press
}

And the same in Assembler:

;*************************************************************************
;*                                                                       *
;*              Testing Read and debounce up to 8 keys                   *
;*              Bulletproof: 4 equal samples needed                      *
;*                                                                       *
;*              Author: P. Dannegger                                     *
;*                                                                       *
;*************************************************************************
.nolist
.include "c:\avr\inc\1200def.inc"
.list
.def    save_sreg       = r0
.def    iwr0            = r1

.def    key_ct0         = r2
.def    key_ct1         = r3
.def    key_state       = r4
.def    key_press       = r5

.def    leds            = r16
.def    wr0             = r17

.equ    key_port        = pind
.equ    led_port        = portb

        rjmp    init
        .org    OVF0addr                ;timer interrupt 6ms
        in      save_sreg, SREG
get8key:
        in      iwr0, key_port
        com     iwr0
        eor     iwr0, key_state
        and     key_ct0, iwr0
        and     key_ct1, iwr0
        com     key_ct0
        eor     key_ct1, key_ct0
        and     iwr0, key_ct0
        and     iwr0, key_ct1
        eor     key_state, iwr0
        and     iwr0, key_state
        or      key_press, iwr0
;
;                       insert other timer functions here
;
        out     SREG, save_sreg
        reti
;-------------------------------------------------------------------------
init:
        ldi     wr0, 0xFF
        out     ddrb, wr0
        ldi     wr0, 1<<CS02^1<<CS00            ;divide by 256 * 256
        out     TCCR0, wr0
        ldi     wr0, 1<<TOIE0           ;enable timer interrupt
        out     TIMSK, wr0

        clr     key_state
        clr     key_press
        ldi     wr0, 0xFF
        mov     key_ct0, wr0
        mov     key_ct1, wr0
        ldi     leds, 0xFF
main:   cli
        eor     leds, key_press         ;toggle LEDs
        clr     key_press               ;clear, if key press action done
        sei
        out     led_port, leds
        rjmp    main
;-------------------------------------------------------------------------

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Peter!
Now this is a very *clean* de-bouncing code!
Ursprüngliche Deutsche Qualität?

Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies.

Pages