#include <htc.h>

// Initialize user ID locations
__IDLOC(0000);

/* Program device configuration word
 * Oscillator = Internal RC No Clock
 * Watchdog Timer = Off
 * Power Up Timer = Off
 * Master Clear Enable = Internal
 * Code Protect = Off
 * Data EE Read Protect = Off
 * Brown Out Detect = BOD and SBOREN disabled
 * Internal External Switch Over Mode = Disabled
 * Monitor Clock Fail-safe = Disabled
 */
__CONFIG(INTIO & WDTDIS & PWRTDIS & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS & IESODIS & FCMDIS);

unsigned int i;

// Peripheral initialization function
void init(void) {
	/***** Common Code ****
	 *  Timer 0 interrupt enabled.
	 *  Peripheral interrupts not enabled
	 *  Global interrupt disabled during initialization
	 */
	INTCON	= 0b00100000;

	/***** 12F683 Code ****
	 *  Internal oscillator set to 8MHz
	 */
	OSCCON	= 0b01110000;
	
	/***** General purpose IO Code ****
	 *  Port directions: 1=input, 0=output
	 */
	TRISIO	= 0b00110000;
	
	/***** Timer 0 Code ****
	 *  Timer 0 is prescaled by 1:256
	 *  Timer 0 is clocked internally.
	 */
	TMR0 = 0;
	OPTION	= 0b00000111;
	
	ADCON0 = 0;
	PCON = 0; // SBOREN = OFF
	GIE = 1;
} // init

void main(void) {
	init();
	i = 0;
	GPIO2 = 0;
	while (1){
		;
	}
} // main

void interrupt my_isr(void) {
	if (T0IF) {
		i++;
		if (i == 32) {
			GPIO2 = !GPIO2;
			i = 0;
			TMR0 = 0;
		}
		T0IF = 0;	// clear event flag
	}
} // my_isr
