/********************************************************************
 * FileName:        (change filename of template).c
 * Processor:       PIC18F4520
 * Compiler:        MPLAB C18 v.3.06
 *
 * This file displays the timer 0 on the LCD screen.
 *
 *  As a bonus it increments the PORTC LEDs using an interrupt when
 *   the timer overflows.
 *
 *       Author               Date              Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// (Your name here) 

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


/** Local Function Prototypes **************************************/
void low_isr(void);
void high_isr(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 ***********************************************/
char line1[20];
char line2[20];
unsigned int result0, result1;

/*******************************************************************
 * 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
    ADCON1 = 0x0F;
    TRISC = 0x00; // All of PORTC output
    PORTC = 0x00; // Turn off all 8 Port C LEDs

    XLCDInit();
    XLCDClear();

    OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_128);

    // Clock Frequency = 4 MHz
    // Instruction cycle Frequency = 1 MHz
    // Timer 0 Frequency = 1 MHz / 128 = 7812.5 Hz
    // Timer 0 Period =  128 uSeconds <-- Always equals prescaler when clock is 4 MHz
    // Overflow every 128 uSecond * 2^16 ticks = 8.4 seconds

    // Interrupt setup
    INTCONbits.GIE = 1;  // Turn on Global interrupts
    INTCONbits.PEIE = 1; // Enable Peripheral interrupts (to use Timer 1 as interrupt)
    
    while (1) {
        // Read Timer0 and display on line 1
        result0 = ReadTimer0(); // read timer
        sprintf(line1, "Timer 0 -> %5u", result0);
        XLCDL1home();
        XLCDPutRamString(line1);

        // TODO: Read Timer1 and display on line 2 (very similar to above)

    }
}


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

void high_isr(void) {
    if (INTCONbits.TMR0IF) { // Check wether it was the timer 0 interrupt
        INTCONbits.TMR0IF = 0; // Clear interrupt flag for TIMER Zero
        PORTC++;
    }
	
	// You will need this later... PIR1bits.TMR1IF
}

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

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