/*******************************************************************************
 *	TestTimer.c		Greenpower CarComputer Test Program
 *				T.Barnaby,	BEAM Ltd,	2008-02-28
 *
 *	Copyright (c) Beam Ltd and Chipping Sodbury School
 *******************************************************************************
 *
 * Function:	Tests the interrupt driven timer
 * Version:	1.0
 */
#include <string.h>
#include <pic18f2520.h>
#include <picLib3.c>

#define	CLOCK		4000000		// Clock frequency

void isr(void) interrupt 1 {
	static UInt8 c;
	// Check timer 3 interrupt
	if(PIR2bits.TMR3IF){
		timerTick();

		if(c++ == 10){
			PORTBbits.RB7 = !PORTBbits.RB7;
			c = 0;
		}
		PIR2bits.TMR3IF = 0;			// Clear timer interrupt flag
	}
}

void main(void) {
	// Set up Oscillator
	OSCCON = 0x62;				// Internal 4MHZ
	TRISB = 0x0F;           		// PORTB top 4 bits Output

	PORTB = 0;				// LED's off

	timerInit();
		
	// Enable interrupts	
	INTCON = 0;				// Clear interrupt flag bits
	INTCONbits.PEIE = 1;
	PIE2bits.TMR3IE = 1;			// TMR3 overflow interrupt enable
	INTCONbits.GIE = 1;			// Global interrupt enable

	// Loop forever
	while(1){
	}
}
