{*
 * Project name:
     Button (Demonstration of using Button Library)
 * Copyright:
     (c) Mikroelektronika, 2012.
 * Revision History:
     20120116:
       - initial release (FJ);
 * Description:
     This program demonstrates usage on-board button as PORTB input.
     On every RB0 one-to-zero transition PORTD is inverted.
 * Test configuration:
     MCU:             PIC18F87K22
                      http://ww1.microchip.com/downloads/en/DeviceDoc/39960b.pdf
     Dev.Board:       EasyPIC PRO v7 - ac:Buttons
                      http://www.mikroe.com/easypic-pro/
     Oscillator:      HS-PLL 64.0000 MHz, 16.0000 MHz Crystal
     Ext. Modules:    None.
     SW:              mikroPascal PRO for PIC
                      http://www.mikroe.com/mikropascal/pic/
 * NOTES:
     - Turn ON the PORTD LEDs at SW6.4. (board specific)
     - Put PB Button Level Switch into VCC position and pull-down PORTB. (board specific)
 *}

program Button_Test;

var oldstate : byte;

begin
  ANCON0 := 0;            // Configure PORTB pins as digital
  ANCON1 := 0;            // Configure PORTC pins as digital
  ANCON2 := 0;  
  
  TRISB0_bit := 1;        // set RB0 pin as input

  TRISD := 0x00;          // Configure PORTD as output
  LATD := 0xAA;           // Initial PORTD value

  oldstate := 0;

  while TRUE do
    begin
      if (Button(PORTB, 0, 1, 1)) then                     // detect logical one on RB0 pin
        oldstate := 1;
      if (oldstate and Button(PORTB, 0, 1, 0)) then
        begin                                              // detect one-to-zero transition on RB0 pin
          LATD := not LATD;
          oldstate := 0;
        end;
    end;                                                   // endless loop
end.