#include <xc.h>

#define _XTAL_FREQ 20000000UL

// LED moves RB0 -> RB7
void Shift_LSB_to_MSB(void)
{
    unsigned char led = 0x01;           // start at LSB (RB0)
    for(unsigned char i = 0; i < 8; i++)
    {
        LATB = led;                     // only one LED ON
        __delay_ms(300);
        led = led << 1;                 // shift left: 01 -> 02 -> 04 ... -> 80
    }
    LATB = 0x00;                        // all OFF after shifting
}

// LED moves RB7 -> RB0
void Shift_MSB_to_LSB(void)
{
    unsigned char led = 0x80;           // start at MSB (RB7)
    for(unsigned char i = 0; i < 8; i++)
    {
        LATB = led;                     // only one LED ON
        __delay_ms(300);
        led = led >> 1;                 // shift right: 80 -> 40 -> 20 ... -> 01
    }
    LATB = 0x00;                        // all OFF after shifting
}

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

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

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

    while(1)
    {
        if(PORTAbits.RA2 == 0)          // SW18 pressed
        {
            __delay_ms(30);             // debounce
            if(PORTAbits.RA2 == 0)
            {
                while(PORTAbits.RA2 == 0);   // wait for release
                Shift_LSB_to_MSB();
            }
        }

        if(PORTAbits.RA3 == 0)          // SW19 pressed
        {
            __delay_ms(30);             // debounce
            if(PORTAbits.RA3 == 0)
            {
                while(PORTAbits.RA3 == 0);   // wait for release
                Shift_MSB_to_LSB();
            }
        }
    }
}
