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

sub function Button(dim byref port as word, dim pin as byte, dim time as word, dim ActiveState as byte) as word

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
  • port: button port address
  • pin: button pin
  • time: debouncing period in milliseconds
  • active_state: determines what is considered as active state. Valid values: 0 (logical zero) and 1 (logical one)
Returns
  • 255 if the pin was in the active state for given period.
  • 0 otherwise
Requires

Nothing.

Example
program Button_Test

dim oldstate as bit

main:
  oldstate = 0
  ADPCFG = 0xFFFF                            ' initialize AN pins as digital
  TRISD = 0xFFFF                             ' initialize portd as input
  TRISB = 0x0000                             ' initialize portb as output

  while TRUE
    if (Button(PORTD, 0, 1, 1)) then                    ' detect logical one on RB0 pin
      oldstate = 1
    end if
    if (oldstate and Button(PORTD, 0, 1, 0)) then
      LATB = not LATB                                   ' invert value of PORTB
      oldstate = 0
    end if
  wend                                                  ' endless loop
end.
Notes

None.

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