/*****************************************************************************
Flashing LEDS
Flashes Derbot LEDs, driven by Timer 0 interrupt on overflow.
Demonstrates: Use of Timer 0 peripheral, Interrupts, and inline assembly.
TJW 30.10.05										Tested 2.11.06
 ****************************************************************************/

#include <p18F242.h>
#include <timers.h>

#pragma config OSC = HS, OSCS = OFF  //HS oscillator, oscillator switch off 
#pragma config PWRT = ON, BOR = OFF  //power-up timer on, brown-out detect off
#pragma config WDT = OFF		     //watchdog timer off
#pragma config STVR = ON, LVP = OFF  //Stack overflow reset enable on, 
										//low voltage programming off

//function prototype, repeated for info
void timer0_isr (void);

unsigned char counter = 0;

//Define the high interrupt vector to be at 0008h
#pragma code high_vector=0x08  
void interrupt (void)
{
  _asm GOTO timer0_isr _endasm  //jump to ISR
}

#pragma code	//Return to default code section

//Function timer0_isr specified as high-priority ISR
#pragma interrupt timer0_isr 

//timer0_isr function. No transfer of parameters, as required by ISRs
void timer0_isr (void)
{
  counter = counter + 1;
  PORTC = counter<<5; //Shift Counter left, and move to PORT C
  INTCONbits.TMR0IF = 0; //Clear TMR0 interrupt flag 
}
void main (void)
{
  //Initialise
	TRISC = 0b10000000;
	PORTC = 0;		//Switch outputs off

/*Initialise TMR0: interrupt enabled,16-bit operation, internal clock, 
prescaler divide by 4, hence (with 4MHz clock)period of 1usx64kx4 = 262ms*/

  OpenTimer0 (TIMER_INT_ON & T0_SOURCE_INT & T0_16BIT & T0_PS_1_4);
  INTCONbits.GIE = 1;//Enable global interrupt

  while (1)	//Await interrupts
    {
    }
}
