/********************************************************************
* FileName:        Problem 4.c
* Processor:       PIC18F4520
* Compiler:        MPLAB C18 v.3.06 
*
* Lab Exam Problem 4 - Winter 2008-2009
*                                                                     
*
*       Author               Date              Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// David Fisher			1/26/09

/**  Header Files **************************************************/     
#include <p18f4520.h> 
#include <timers.h>
#include <stdio.h>
#include "LCD module.h"


/** Configuration Bits *********************************************/     
#pragma config OSC = EC  // EC = External 4MHz Crystal for PICDEM board only
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config BOREN = 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 ***********************************************/
int sampleVariable = 0;
char line1[17];
int counter = 0;

/*******************************************************************
* Function:        void main(void)
********************************************************************/
#pragma code
void main (void)
{
	// Setup Output LEDs
	ADCON1 = 0x0F;
	// Initialize the LCD
	XLCDInit();
	XLCDClear();	


	//A configure timer0
	OpenTimer0( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_128 );
   	INTCONbits.GIE = 1;			// Enable Interrupts
   	
	WriteTimer0(65535-7812);
	while( 1 )
	{
		if(counter > 0)
		{
			sprintf(line1,"%d",counter);
			XLCDL1home();
			XLCDPutRamString(line1);			
		}
	}

}


/*****************************************************************
* Function:        void high_isr(void)
* Possible sources of interrupt - none
* Overview:
******************************************************************/
#pragma interrupt high_isr
void high_isr(void)
{
	// Add code here for the high priority Interrupt Service Routine (ISR)
	if(INTCONbits.TMR0IF)  // Check wether it was the timer 0 interrupt
	{						
		INTCONbits.TMR0IF = 0;	// Clear interrupt flag for TIMER Zero
		counter++;
		WriteTimer0(65535-7812);
	}

}

/******************************************************************
* Function:        void low_isr(void)
* Possible sources of interrupt - none
* Overview:
********************************************************************/
#pragma interruptlow low_isr
void low_isr(void)
{
	// Add code here for the low priority Interrupt Service Routine (ISR)
}