/*******************************************************************2
* FileName:        Problem 1.c
* Processor:       PIC18F4520
* Compiler:        MPLAB C18 v.3.06 
*                                                                     
*
*       Author               Date              Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// David Fisher

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

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

	
/*******************************************************************
* Function:        void main(void)
********************************************************************/
#pragma code
void main (void)
{
	ADCON1=0x0F;

	// -------------- RB0 Setup Start ------------------
	TRISBbits.TRISB0 = 0;
	// --------------- RB0 Setup End -------------------
	

	// ------- Timer + Interrupt Setup Start -----------
	OpenTimer0( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_16 );
	WriteTimer0(TIMER_START_VALUE);
	INTCONbits.GIE = 1;				// Turn on Global interrupts
	// -------- Timer + Interrupt Setup End ------------		


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

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

	
/*****************************************************************
* 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)  		// Timer 0 interrupt
	{						
		INTCONbits.TMR0IF = 0;			// Reset interrupt flag
		PORTBbits.RB0 ^= 1;				// Toggles RB0 in a cute way
		WriteTimer0(TIMER_START_VALUE);	// Restart timer (0.25 seconds)
		
	}
	
}

/******************************************************************
* 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)
}