/********************************************************************
 * FileName:        Exam2_P1.c
 * Processor:       PIC18F4520
 * Compiler:        MPLAB C18 v.3.06
 *
 *       Author               Date              Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// Key

/**  Header Files **************************************************/
#include <p18f4520.h>
#include <stdio.h>
#include <adc.h>
#include "LCD Module.h"
#include <timers.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 ******************************************/

// 500 kHz -->  125 kHz instr cycle --> 1953 Hz
// Period = 0.000512 per tick
// Need 195 ticks  2^16-195
#define TIMER_START 65340

/** 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 count = 50;
char line2[17];

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

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

    // 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

    XLCDInit();
    XLCDClear();
    XLCDL1home();
    XLCDPutRomString("COUNTDOWN:");
    updateLcd();

    // Interrupt setup
    RCONbits.IPEN = 1; // Put the interrupts into Priority Mode
    // Add specific interrupts here...
    OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_64);
    INTCON2bits.TMR0IP = 1;

    INTCONbits.GIEH = 1; // Turn on high priority interrupts
    WriteTimer0(TIMER_START);

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

/*****************************************************************
 * 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.TMR0IF) { // Timer 0 interrupt
        INTCONbits.TMR0IF = 0;
        WriteTimer0(TIMER_START);
        count--;
        if (count == 0) {
            PORTC = 0xFF;
            INTCONbits.TMR0IE = 0;
        }
        // updateLcd();  // You could update here too if you wanted, but I like doing LCD work in the main while loop
    }
}

/******************************************************************
 * 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
/*****************************************************************
 * Additional Helper Functions
 ******************************************************************/

/*****************************************************************
 * Function:			void sample(void)
 * Input Variables:	none
 * Output Return:	none
 * Overview:			Use a comment block like this before functions
 ******************************************************************/
void updateLcd() {
    sprintf(line2, "%d ", count);  // Could use "%2d" instead, but not just "%d" as the 0 from 10 would be present when you print 9
    XLCDL2home();
    XLCDPutRamString(line2);
}