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 :
var Button_Pin : sbit; bdata; sfr; external; Declares Button_Pin, which will be used by Button Library. var Button_Pin: sbit at P0_0_bit;

Library Routines

Button

Prototype

function Button(time_ms : byte; active_state : byte) : 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_Test;

// button connections
var Button_Pin : sbit at P0_0_bit;           // declare Button_Pin. It will be used by Button Library.
// end Button connections
   oldstate : bit;

begin
  P0 := 255;                              // configure PORT0 as input
  P2 := 0xAA;                             // initial PORT2 value

  while TRUE do
  begin
    if (Button(1, 1) <> 0) then           // detect logical one
      oldstate := 1;                      // update flag
    if (oldstate and Button(1, 0)) then   // detect one-to-zero transition
      begin
        P2 := not P2;                     // invert PORT2
        oldstate := 0;                    // update flag
      end;
  end;                                    // endless loop
end.

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