Button Library

Button library contains miscellaneous routines useful for project development.

External dependecies of Button Library

The following variable must be defined in all projects using Button library: Description: Example :
dim Button_Pin as sbit external Declares Button_Pin, which will be used by Button Library. dim Button_Pin as sbit at P0_0

Library Routines

Button

Prototype

sub function Button(dim time_ms as byte, dim active_state as byte) as byte

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_ms: 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.

Button pin must be configured as input.

Example

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

program Button

' Button connections
dim Button_Pin as sbit at P0.B0;           ' Declare Button_Pin. It will be used by Button Library.
' End Button connections

bit oldstate;                       ' Old state flag

main: 
  P0 = 255                         ' Configure PORT0 as input
  P2 = 0xAA                        ' Initial PORT2 value

  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