
#include <p18cxxx.h>

// 14K50 CONFIGURATION **************************************************/
// These are the same settings used by the bootloader and must not be changed here.
//        #pragma config CPUDIV = NOCLKDIV
//        #pragma config USBDIV = OFF
//        #pragma config FOSC   = HS
//        #pragma config PLLEN  = ON
//        #pragma config FCMEN  = OFF
//        #pragma config IESO   = OFF
//        #pragma config PWRTEN = OFF
//        #pragma config BOREN  = OFF
//        #pragma config BORV   = 30
//        #pragma config WDTEN  = OFF
//        #pragma config WDTPS  = 32768
//        #pragma config MCLRE  = OFF
//        #pragma config HFOFST = OFF
//        #pragma config STVREN = ON
//        #pragma config LVP    = OFF
//        #pragma config XINST  = OFF
//        #pragma config BBSIZ  = OFF
//        #pragma config CP0    = OFF
//        #pragma config CP1    = OFF
//        #pragma config CPB    = OFF
//        #pragma config WRT0   = OFF
//        #pragma config WRT1   = OFF
//        #pragma config WRTB   = OFF
//        #pragma config WRTC   = OFF
//        #pragma config EBTR0  = OFF
//        #pragma config EBTR1  = OFF
//        #pragma config EBTRB  = OFF       


// prototypes
void dly1mS(void);
void delay_mS(unsigned int dly);
void timer (void);



/** VECTOR REMAPPING ***********************************************/
// On PIC18 devices, addresses 0x00, 0x08, and 0x18 are used for
// the reset, high priority interrupt, and low priority interrupt
// vectors.  However, the current Microchip USB bootloader occupies
// addresses 0x00-0xFFF. Therefore the bootloader code remaps these 
// vectors to new locations.
	
	#define REMAPPED_RESET_VECTOR_ADDRESS			0x1000
	#define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS	0x1008
	#define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS	0x1018

	void YourHighPriorityISRCode();
	void YourLowPriorityISRCode();
	
	extern void _startup (void);        // See c018i.c in your C18 compiler dir
	#pragma code REMAPPED_RESET_VECTOR = REMAPPED_RESET_VECTOR_ADDRESS
	void _reset (void)
	{
	    _asm goto _startup _endasm
	}

	#pragma code REMAPPED_HIGH_INTERRUPT_VECTOR = REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS
	void Remapped_High_ISR (void)
	{
	     _asm goto YourHighPriorityISRCode _endasm
	}
	#pragma code REMAPPED_LOW_INTERRUPT_VECTOR = REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS
	void Remapped_Low_ISR (void)
	{
	     _asm goto YourLowPriorityISRCode _endasm
	}
	#pragma code
	
	
	//These are your actual interrupt handling routines.
	#pragma interrupt YourHighPriorityISRCode
	void YourHighPriorityISRCode()
	{
		//Check which interrupt flag caused the interrupt.
		//Service the interrupt
		//Clear the interrupt flag
		//Etc.
	}	//This return will be a "retfie fast", since this is in a #pragma interrupt section 

	#pragma interruptlow YourLowPriorityISRCode
	void YourLowPriorityISRCode()
	{
		//Check which interrupt flag caused the interrupt.
		//Service the interrupt
		//Clear the interrupt flag
		//Etc.
	
	}	//This return will be a "retfie", since this is in a #pragma interruptlow section 

	#pragma code
	
#define LEDwhi	LATCbits.LATC4			// green led
#define LEDred	LATCbits.LATC6			// red led

//#define LEDgrn	LATCbits.LATC4			// green led
//#define LEDred	LATCbits.LATC5			// red led
#define DELAY	167						// delay in mS (167 = 2seconds for all 12 pins)

void main(void)
{   
	ANSEL = 0;						// all pins to digital mode
	ANSELH = 0;

	TRISB = 0;						// all pins to output;
	TRISC = 0;	
	
//	TRISA = 0XFF;
	TRISC = 4;
	
	T3CON = 0xb1;					// 16 bit, 8:1 prescale, running
	




	
	while(1) {
		

	
		
	LATC = 0;
	timer();
	LATC = 0XFF;
	timer();
		

       
			
	} 
}


void dly1mS(void)
{
	TMR3H = (0-1500)>>8;						// 1500 * (8/12) = 1000uS
	TMR3L = (0-1500)&0x0ff;					// must write TMR3 in this order
	PIR2bits.TMR3IF = 0;
	while(!PIR2bits.TMR3IF);				// wait 1mS
}
	
	
void delay_mS(unsigned int dly)
{
unsigned int x;
	for(x=0; x<dly; x++) dly1mS();
}


void timer (void){
	
	unsigned int t=0xFFFF;
	while(t!=0) t--;
}		 