#include <p33FJ64MC802.h>
#include <stdio.h>
#include <timer.h>

int cpt1ms;
int cpt=0;


_FOSCSEL(FNOSC_PRI);								// Primary oscillator (XT, HS, EC)
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF  & POSCMD_XT);	// Clock Switching Both disabled and Fail Safe Clock Monitor is disabled
													// OSC2 Pin Function: OSC2 is Clock Output
_FWDT(FWDTEN_OFF); 


void InitPORT_AND_PIN() 
{
//	AD1PCFGLbits.PCFG8=1; //AD8 (RC2) EN DIGITAL

}


/* Example code for Timer1 ISR */
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
{
	PORTBbits.RB0=!PORTBbits.RB0;
/* Interrupt Service Routine code goes here */
IFS0bits.T1IF = 0; // Clear Timer1 Interrupt Flag
}


void InitTimer1(){
/* Enable Timer1 Interrupt and Priority to "1" */
 /*  ConfigIntTimer1(T1_INT_PRIOR_1 & T1_INT_ON);
    WriteTimer1(0);
	cpt1ms=0;
    OpenTimer1(T1_ON & T1_GATE_OFF & T1_IDLE_STOP &
               T1_PS_1_1 & T1_SYNC_EXT_OFF &
               T1_SOURCE_EXT, 39960);  //TIMER1 à 1ms
*/
/* Initialisation Timer par registre */

T1CONbits.TON = 0;                   // Disable Timer
T1CONbits.TCS = 0;                  // Select internal instruction cycle clock
T1CONbits.TGATE = 0;                // Disable Gated Timer mode
T1CONbits.TCKPS = 0b00;             // Select 1:1 Prescaler
TMR1 = 0x00;                        // Clear timer register
PR1 = 9;                            // Load the period value       PR1=((FCY/8)/fréquence du timer)-1;
IPC0bits.T1IP = 0x01;               // Set Timer1 Interrupt Priority Level
IFS0bits.T1IF = 0;                  // Clear Timer1 Interrupt Flag
IEC0bits.T1IE = 1;                  // Enable Timer1 interrupt
T1CONbits.TON = 1;                  // Start Timer

}

int main(void) 
{
	

//	InitPORT_AND_PIN();
 	InitTimer1();

	TRISBbits.TRISB0=0;
	AD1PCFGLbits.PCFG2=1;

	for (;;)
{
    TMR1 = 255; // Clear timer register

	PORTBbits.RB0=!PORTBbits.RB0;


}

	return 0;
}
