Button Library

The Button Library provides routines for detecting button presses and debouncing (eliminating the influence of contact flickering upon pressing a button)

Library Routines

Button

Prototype

function Button(var port : byte; pin, time, active_state : byte) : byte;

Returns

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

Description

Function eliminates the influence of contact flickering upon pressing a button (debouncing).

Parameter port specifies the PIN register of the desired port; parameter pin is the pin number on designated port and goes from 0..7; parameter time is a debounce period in milliseconds; parameter active_state can be either 0 or 1, and it determines if the button is active upon logical zero or logical one.

Requires

Button pin must be configured as input.

Example

On every PB0 one-to-zero transition PORTC is inverted :

program Button_Test;

var  oldstate : bit;

begin
    DDB0_bit := 0;                 // set PORTB to be input
    DDRC  := 0xFF;                                    // configure PORTC as output
    PORTC := 0xAA;                                    // initial PORTC value

    oldstate := 0;                                    // oldstate initial value

  while TRUE do
    begin
      if (Button(PINB, 0, 1, 1)) then                 // detect logical one on RB1 pin
          oldstate := 1;
      if (oldstate and Button(PINB, 0, 1, 0)) then
        begin                                         // detect one-to-zero transition on RB1 pin
          PORTC := not PORTC;
          oldstate := 0;
        end;
    end;                                              // endless loop
  end.
Copyright (c) 2002-2012 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