/* created by Bertrand VANDEPORTAELE */ #include #include #define XOR ^ const int ledPin = 13; //broche 13=PB5 //valeur étalonnée de l'intervale de temps entre chaque interruption Timer, mesurée à l'oscillo en moyennant //valeur correcte pour la carte copie arduino avec oscillateur à quartz #define INTERVALE_IT_NS 260020 //Valeur à régler de l'intervale de temps en ns entre chaque commutation de la broche //commutation chaque 1/15 de seconde // #define INTERVALE_COMMUT_NS 66666667 //commutation chaque 1/120 de seconde //#define INTERVALE_COMMUT_NS 8333333 //commutation chaque 1/10 de seconde #define INTERVALE_COMMUT_NS 100000000 //commutation chaque 4 secondes, valeur proche du max // #define INTERVALE_COMMUT_NS 4000000000 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// unsigned long int cpt = 0; //peut compter jusquà 4*10^ 9, soit 4 secondes entre chaque commutation #define PRESCALER_INDEX 3 #define TIMER_PERIOD 8000 // TIMER ISR VECTOR and configuration unsigned char isr_modulo_count, global_isr_modulo = 0 ; ISR(TIMER2_COMPA_vect) { //transmit interrupt cpt = cpt + INTERVALE_IT_NS; if (cpt >= INTERVALE_COMMUT_NS) { cpt -= INTERVALE_COMMUT_NS; PORTB ^= 0x01 << 5 ; //bit 5 du port B } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void setupTimer2(unsigned char prescaler, unsigned int period) { TCCR2A = 0;// set entire TCCR2A register to 0 TCCR2B = 0;// same for TCCR2B TCNT2 = 0;//initialize counter value to 0 OCR2A = period;// = (16*10^6) / (8000*8) - 1 (must be <256) // turn on CTC mode TCCR2A |= (1 << WGM21); switch (prescaler) { case 0 : // Set CS20 bit for no prescaler TCCR2B |= (1 << CS20); case 1 : // Set CS21 bit for 8 prescaler TCCR2B |= (1 << CS21); case 2 : // Set CS21 bit for 64 prescaler TCCR2B |= (1 << CS21) | (1 << CS20); break ; case 3 : // Set CS21 bit for 128 prescaler TCCR2B |= (1 << CS22) ; break ; case 4 : // Set CS21 bit for 256 prescaler TCCR2B |= (1 << CS22) | (1 << CS21); break ; case 5 : // Set CS21 bit for 1024 prescaler TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20); break ; default : TCCR2B |= (1 << CS20); break ; } // enable timer compare interrupt TIMSK2 |= (1 << OCIE2A); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void setup() { // start serial port at 9600 bps: Serial.begin(115200); pinMode(ledPin, OUTPUT); cli();//stop interrupts setupTimer2(PRESCALER_INDEX, TIMER_PERIOD); // setup interrupt with prescaler (see datasheet for prescaler value, and reload value) sei();//allow interrupts } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void loop() { }