#include <xc.h>

#define _XTAL_FREQ 20000000UL

unsigned char count = 0;

void main(void)
{
    ADCON1 = 0x0F;              // make PORTA/PORTB pins digital

    TRISB = 0x00;               // PORTB = output (LEDs)
    LATB  = 0x00;               // all LEDs OFF

    TRISAbits.TRISA2 = 1;       // RA2 = input (SW18, UP)
    TRISAbits.TRISA3 = 1;       // RA3 = input (SW19, DOWN)

    while(1)
    {
        // UP button
        if(PORTAbits.RA2 == 0)
        {
            __delay_ms(30);                 // debounce
            if(PORTAbits.RA2 == 0)
            {
                count++;                    // increment count
                LATB = count;               // show on LEDs
                while(PORTAbits.RA2 == 0);  // wait for release
                __delay_ms(30);
            }
        }

        // DOWN button
        if(PORTAbits.RA3 == 0)
        {
            __delay_ms(30);                 // debounce
            if(PORTAbits.RA3 == 0)
            {
                count--;                    // decrement count
                LATB = count;               // show on LEDs
                while(PORTAbits.RA3 == 0);  // wait for release
                __delay_ms(30);
            }
        }
    }
}
