Button Library

The Button library contains button routine useful for a project development.

External dependencies of Button Library

The following variable must be defined in all projects using Button library: Description: Example :
extern sfr sbit bdata Button_Pin; Declares Button_Pin, which will be used by Button Library. sbit Button_Pin at P0_0;

Library Routines

Button

Prototype

unsigned short Button(unsigned short time, unsigned short active_state)

Returns

  • 255 if the pin was in the active state for given period.
  • 0 otherwise

Description

The function eliminates the influence of contact flickering upon pressing a button (debouncing). The Button pin is tested just after the function call and then again after the debouncing period has expired. If the pin was in the active state in both cases then the function returns 255 (true).

Parameters :

  • time: debouncing period in milliseconds
  • active_state: determines what is considered as active state. Valid values: 0 (logical zero) and 1 (logical one)

Requires

Button_Pin variable must be defined before using this function.

Example

P2 is inverted on every P0.B0 one-to-zero transition :


// Button connections
sbit Button_Pin at P0_0_bit;        // Declare Button_Pin. It will be used by Button Library.
// End Button connections

bit oldstate;                       // Old state flag

void main() {

  P0 = 255;                         // Configure PORT0 as input
  P2 = 0xAA;                        // Initial PORT2 value
  oldstate = 0;
  
  do {
    if (Button(1, 1))               // Detect logical one
      oldstate = 1;                 // Update flag
    if (oldstate && Button(1, 0)) { // Detect one-to-zero transition
      P2 = ~P2;                     // Invert PORT2
      oldstate = 0;                 // Update flag
      }
  } while(1);                       // Endless loop
}

Copyright (c) 2002-2013 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code