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

extern int cpt1ms;

void __attribute__((__interrupt__)) _T1Interrupt(void)
{
	PORTBbits.RB0=!PORTBbits.RB0;
	cpt1ms++;
	flagIT=1; // Pour traitement différé
    WriteTimer1(0);
    IFS0bits.T1IF = 0;    /* Clear Timer interrupt flag */
}  


void InitTimer1()
{
	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 = 39960;                            // Load the period value       PR1=((FCY/Pre)/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  //TIMER1 à 1ms
	// prinicpe timer: comptage au rythme de Tcy*PreDiv
	// Si comptage=PER= 39960 alors TM1RIF ON
	//=> 39960 *Tcy*PreDiv
	// ici Fcy=80M/2=40Mhz =>25ns , PreDiv=T1_PS_1_1 =1
	// 39960*25ns*1=1ms

}