#include <p18f2455.h>
#include "configs.h"

void main(void) {
	PORTA = 0b00000000;		// clear all PORTA pins 
	ADCON1 = 0b00001111;	// set up PORTA to be digital I/Os
	TRISA = 0b00000100;		// set up RA2 as a digital input and the rest of PORTA as digital outputs
	PORTAbits.RA1 = 1;		// set RA1 high initially
	
	/*
	* Initialize Timer0 to overflow every 700ms.
	* We can compute this speed because we have configured the CPU to
	* run at 24 MHz.  The timer's clock rate is derived from the system's
	* clock rate divided by 4, so the timer increments at a rate of 6 MHz.
	* T0CON = 0b10000101 sets a prescaler of 1:64, so the timer will run
	* 64 times slower, or at 0.09375 MHz = 93.75 kHz.  To compute how long
	* it will take for the timer to overflow, we use the fact that Timer0 is 
	* a 16-bit timer so it will overflow after it has incremented 2^16 = 65,536 times.
	* At 93.75 kHz that will happen 93,750 / 65536 = 1.43 times per second,
	* or once every 0.699 milliseconds.
	*/
	T0CON = 0b10000101;		// initialize Timer0 to go off every 700 ms (i.e., enable Timer0 with a prescaler of 1:64)

	while (1) {
		while (INTCONbits.T0IF==0) {					// until Timer0 interupt goes off...
   	    	PORTAbits.RA1 = (PORTAbits.RA2==1) ? 0:1;	// ...if RA2 is high, make RA1 low and vice versa
       	}
		INTCONbits.T0IF = 0;							// reset the Timer0 interupt flag
		PORTAbits.RA0 = !PORTAbits.RA0;					// toggle RA0
    }
}

