Bonsoir, j'ai une carte banale sur laquelle mon arduino Nano active des relais et quand je veux mettre une tempo en interruption avec timer 1, ça bug: ma led ne test ne s'éteint pas !
void setup() {
/*
* Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board.
* The chips used on the Arduino board (the ATmega8 and ATmega168) have three ports:
-B (digital pin 8 to 13)
-C (analog input pins)
-D (digital pins 0 to 7)
//All Arduino (Atmega) digital pins are inputs when you begin...
*/
PCICR |= (1 << PCIE0); //enable PCMSK0 scan
PCMSK0 |= (1 << PCINT0); //Set pin D8 trigger an interrupt on state change. Input from optocoupler
pinMode(3,OUTPUT); //Define D3 as output for the DIAC pulse
pinMode(test, OUTPUT);
pinMode(power1, OUTPUT);
pinMode(direction1, OUTPUT);
/// pour les tests de fonctionnement
digitalWrite (test,HIGH);
digitalWrite (power1,HIGH);
delay(1000); // wait for a second
digitalWrite (direction1,HIGH);
delay(1000); // wait for a second
digitalWrite (power1,LOW);
delay(1000); // wait for a second
digitalWrite (direction1,LOW);
delay(1000); // wait for a second
digitalWrite (test,LOW);
// alimentation moteur1
digitalWrite (power1,HIGH);
// activation moteur1 sens 1
digitalWrite (direction1,LOW);
cli();//stop interrupts
//////////////////////////////////////////////////////////////
//set timer1 interrupt at 0.28Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
//OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// maximum de registre compare, soit 0.28 Hz soit 4.19 secondes
OCR1A = 65535;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
/////////////////////////////////////////////////////////////
sei(); // Active l'interruption globale
valor = 10;
}
void loop() {
delayMicroseconds(valor); //This delay controls the power
digitalWrite(3,HIGH);
delayMicroseconds(100);
digitalWrite(3,LOW);
}
//timer1 interrupt en comparateur, voir réglages dans le setup
ISR(TIMER_COMPA_vect){
if (toggle1 == 1) {
digitalWrite (test,HIGH);
valor = 10;
toggle1 = 0;
} else {
digitalWrite (test,LOW);
valor = 150;
toggle1 = 1;
}
}
J'ai eu du mal à trouver l'erreur mais c'est seulement à l'activation de timer1 compare interrupt que ça plante; TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
Que se passe t-il ?
