3 Codestücke aus dem Arduino Forum, die verschiedene Möglichkeiten zeigen, Interrupts zu verwenden. Mit einer Zählschleife kann die Geschwindigkeit des Codes gmessen werden.
// 3 Beispiele aus dem Arduino Forum zur Ansteuerung von Interrupts
// ===================================================================
// // Beispiel 1 langsam
// #define LED_PIN 5 // digital pin #13 (portb)
// #define LED_ON() PORTB |= _BV(LED_PIN)
// #define LED_OFF() PORTB &= ~_BV(LED_PIN)
// void myfunc() {
// LED_OFF();
// }
// void setup() {
// pinMode(13, OUTPUT);
// attachInterrupt(0, myfunc, RISING);
// }
// void loop() {
// LED_ON();
// delayMicroseconds(20);
// }
// ===================================================================
// // Beispiel 2 schnell
// #define LED_PIN 5 // digital pin #13 (portb)
// #define LED_ON() PORTB |= _BV(LED_PIN)
// #define LED_OFF() PORTB &= ~_BV(LED_PIN)
// ISR(INT0_vect) {
// LED_OFF();
// }
// void setup() {
// pinMode(13, OUTPUT);
// digitalWrite(2, HIGH);
// LED_ON();
// // enable INT0 interrupt on change
// EICRA = 0x03; // INT0 rising edge on SCL, falling is 0x02
// EIMSK = 0x01; // enable only int0
// }
// void loop() {
// //LED_ON();
// delayMicroseconds(2000);
// }
// ===================================================================
// Beispiel 3 // LED connected to digital pin 13 #include <TimedAction.h> int ledPin = 13; // This is the INT0 Pin of the ATMega8 int sensePin = 2; // We need to declare the data exchange // variable to be volatile - the value is // read from memory. volatile int value = 0;
// Install the interrupt routine.
ISR(INT0_vect) {
//check the value again - since it takes some time to
//activate the interrupt routine, we get a clear signal.
value = digitalRead(sensePin);
}
ISR(PCINT0_vect) {
//check the value again - since it takes some time to
//activate the interrupt routine, we get a clear signal.
value = digitalRead(8);
delayMicroseconds(500);
}
int perfCounter = 0; // Zaehler für Messung der Durchlaufzeiten des Loops
TimedAction timedAction1 = TimedAction(100,timedperfCounter);
void timedperfCounter(void) // Zaehler für Messung der Durchlaufzeiten des Loops
{
Serial.print("Anzahl Loops in 100 ms ");
Serial.println(perfCounter, DEC);
perfCounter = 0;
}
void setup() {
Serial.begin(9600);
Serial.println("Initializing ihandler");
// sets the digital pin as output
pinMode(ledPin, OUTPUT);
// read from the sense pin
pinMode(sensePin, INPUT);
digitalWrite(sensePin, HIGH);
pinMode(8, INPUT);
digitalWrite(8, HIGH);
Serial.println("Processing initialization");
// Global Enable INT0 interrupt
//EIMSK |= ( 1 << INT0);
// Signal change triggers interrupt
//EICRA |= ( 1 << ISC00);
//EICRA |= ( 0 << ISC01);
PCICR |= (1 << PCIE0); // Pin Change Interrupt Enable 0
PCMSK0 |= (1 << PCINT0); // PCMSK0 Pin Change Mask Register 0, Arduino D8 = PCINT0 Pin
Serial.println("Finished initialization");
}
void loop() {
if (value) {
//Serial.println("Value high!");
digitalWrite(ledPin, HIGH);
} else {
//Serial.println("Value low!");
digitalWrite(ledPin, LOW);
}
//delay(100);
// Messung der Geschwindigkeit des Loops
timedAction1.check();
perfCounter++;
}