Kriterien
- kleine Prozessorbelastung
- schnelle Reaktionszeit auf Signaländerungen
- wenig / optimierte Variablen
- sinnvolle Datentypen
- keine
delay
Funktionen - keine
Timer
verwenden
Lösungsansätze
- Interrupt
- beschränkt verfügbar / einsetzbar
- abhängig vom Board / Prozessor
- volatile
- in Kombination sinnvoll
- Register
- Bitoperation (komplex zum Programmieren)
Beispiel
/* Interrupt Port Mapping
https://www.arduino.cc/en/Reference/PortManipulation
https://arduino-projekte.webnode.at/registerprogrammierung/ein-ausgangsports/
Port registers allow for lower-level and faster manipulation of the i/o pins.
B (digital pin 8 to 13)
C (analog input pins)
D (digital pins 0 to 7)
*/
#define BAUDRATE 9600
#define DELAY 2000
void setup() {
Serial.begin(BAUDRATE);
Serial.println("Port Mapping");
// D2 is INPUT - capactitive touch sensor
// D2 is INTERRUPT (Arduino UNO)
DDRD &= ~(1 << DDD2);
// D8 is OUTPUT - led
DDRB |= (1 << DDB0);
// DDD2 (digital Port 2) interrupts loop by calling isr (interrupt service routine)
// interrupt by: LOW - CHANGE - RISING - FALLING signal
attachInterrupt(digitalPinToInterrupt(DDD2), isr, CHANGE);
}
// interrupt service routine toggles led
void isr() {
Serial.print("*");
// read pin D5 - touch sensor
byte touch = (PIND & (1 << PIND2)) >> PIND2;
Serial.print(touch);
// toggle pin D8 - led
if (touch) PORTB |= (1 << PORTB0); // on
else PORTB &= ~(1 << PORTB0); // off
}
void loop() {
Serial.print(".");
delay(DELAY);
}
Ausführungen
Das Arduino Projekt InterruptPortMapping ist im basis Projekt.