/********************************************************************
 * FileName:        (change filename of template).c
 * Processor:       PIC18F4520
 * Compiler:        MPLAB C18 v.3.06
 *
 * This file does the following....
 *
 *
 *       Author               Date              Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// (Your name here) 

/**  Header Files **************************************************/
#include <p18f4520.h>
#include <stdio.h>
#include <delays.h>
#include <adc.h>
#include <portb.h>
#include "LCD Module.h"

/** Configuration Bits *********************************************/
#pragma config OSC = INTIO67
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config BOREN = OFF
#pragma config XINST = OFF

/** Define Constants Here ******************************************/
#define PRESSED 0
#define UNPRESSED 1

/** Local Function Prototypes **************************************/
void low_isr(void);
void high_isr(void);
void updateLcd(void);

/** Declare Interrupt Vector Sections ****************************/
#pragma code high_vector=0x08

void interrupt_at_high_vector(void) {
    _asm goto high_isr _endasm
}

#pragma code low_vector=0x18

void interrupt_at_low_vector(void) {
    _asm goto low_isr _endasm
}

/** Global Variables ***********************************************/
int lettersToDisplay = 0;

/*******************************************************************
 * Function:        void main(void)
 ********************************************************************/
#pragma code

void main(void) {
    // Set the clock to 4 MHz
    OSCCONbits.IRCF2 = 1;
    OSCCONbits.IRCF1 = 1;
    OSCCONbits.IRCF0 = 0;

    // Pin IO Setup
    OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST & ADC_12_TAD,
            ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VSS,
            0x0B); // Four analog pins
    TRISA = 0xFF; // All of PORTA input
    TRISB = 0xFF; // All of PORTB input
    TRISC = 0x00; // All of PORTC output
    TRISD = 0x00; // All of PORTD output
    PORTC = 0x00; // Turn off all 8 Port C LEDs

    PORTC = 0x01; // Turn off all 8 Port C LEDs

    // Interrupt setup
    RCONbits.IPEN = 1; // Put the interrupts into Priority Mode
    // Turn on the RB0 interrupt INT0, falling edge, always high priority
    OpenRB0INT(PORTB_CHANGE_INT_ON & FALLING_EDGE_INT & PORTB_PULLUPS_OFF);

    OpenRB1INT(PORTB_CHANGE_INT_ON & FALLING_EDGE_INT & PORTB_PULLUPS_OFF);
    INTCON3bits.INT1IP = 1;

    INTCONbits.GIEH = 1; // Turn on high priority interrupts

    XLCDInit();
    XLCDClear();

    while (1) {
        // This area loops forever
    }
}

/*****************************************************************
 * Additional Helper Functions
 ******************************************************************/

/*****************************************************************
 * Function:			void sample(void)
 * Input Variables:	none
 * Output Return:	none
 * Overview:			Use a comment block like this before functions
 ******************************************************************/
void updateLcd() {
    // Some function that does a specific task
    XLCDL1home();
    switch (lettersToDisplay) {
        case 0:
            XLCDPutRomString("      ");
            break;
        case 1:
            XLCDPutRomString("A     ");
            break;
        case 2:
            XLCDPutRomString("AB    ");
            break;
        case 3:
            XLCDPutRomString("ABC   ");
            break;
        case 4:
            XLCDPutRomString("ABCD  ");
            break;
        case 5:
            XLCDPutRomString("ABCDE ");
            break;
        case 6:
            XLCDPutRomString("ABCDEF");
            break;
    }
}

/*****************************************************************
 * Function:        void high_isr(void)
 ******************************************************************/
#pragma interrupt high_isr

void high_isr(void) {
    // Add code here for the high priority Interrupt Service Routine (ISR)


    if (INTCONbits.INT0IF) {
        INTCONbits.INT0IF = 0;
        Delay1KTCYx(30);
        if (PORTBbits.RB0 == PRESSED) {
            lettersToDisplay++;
            if (lettersToDisplay > 6) {
                lettersToDisplay = 6;
            }
            updateLcd();
        }

    }

    if (INTCON3bits.INT1IF) {
        INTCON3bits.INT1IF = 0;
        Delay1KTCYx(30);
        if (PORTBbits.RB1 == PRESSED) {
            lettersToDisplay--;
            if (lettersToDisplay < 0) {
                lettersToDisplay = 0;
            }
            updateLcd();

        }
    }

}

/******************************************************************
 * Function:        void low_isr(void)
 ********************************************************************/
#pragma interruptlow low_isr

void low_isr(void) {
    // Add code here for the low priority Interrupt Service Routine (ISR)
}
#pragma code