Table of Contents |
---|
Interrupts
Interrupt Code Sample
Sample Code to to toggle an LED when a button triggers an interrupt.
Code Block | ||
---|---|---|
| ||
//Define LED and switch connections
const byte ledPin = 13;
const byte buttonPin = 2;
// Boolean to represent toggle state
volatile bool toggleState = false;
void checkSwitch@) {
//Check status of switch
// Toggle LED if button pressed
if (digitalRead(buttonPin) == LOW) {
// Switch was pressed
// Change state of toggle
toggleState = !toggleState;
// Indicate state on LED
digitalWrite(ledPin, toggleState);
}
}
void setup() {
//Set LED pin as output
pinMode(ledPin, OUTPUT) ;
// Set switch pin as INPUT with pullup
pinMode (buttonPin, INPUT_PULLUP) ;
//Setup Serial Monitor
Serial. begin (9600);
// Attach Interrupt to Interrupt Service Routine
attachInterrupt(digitalPinToInterrupt(buttonPin),checkSwitch, FALLING);
}
void loop() {
// 5-second time delay
Serial.println("Delay Started");
delay (5000);
Serial.println("Delay Finished");
Serial.println(".......
}
|
Pin Change Interrupts
Arduino microcontrollers have a limited number of pins that are hardware interrupt compatible.
To get around this limitation, we can use a pin change interrupts on almost all pins.
Enable Port B
Each port has a mask to enable particular pins.
Activate Pin D12 on Port B
Interrupt Service Routines are pre-defined for each port
Define the ISR
We must rest the interrupt by writing a "1" to the corresponding bit of the PCIFR register. This is not required when using an ISR.
Note about Pin Change Interrupts
Analyzing the Following Code
ATmega328P Interrupt Vector Table
- The ATmega328P provides support for 25 different interrupt sources. These interrupts and the separate Reset Vector each have a separate program vector located at the lowest addresses in the Flash program memory space.
...
Code Block |
---|
static uint8_t saveTCCR1A, saveTCCR1B; static inline void capture_init(void) { saveTCCR1A = TCCR1A; saveTCCR1B = TCCR1B; //reset TimerCounter 1 TCCR1B = 0; TCCR1A = 0; TCNT1 = 0; TIFR1 = (1<<ICF1) | (1<<TOV1); TIMSK1 = (1<<ICIE1) | (1<<TOIE1); } static inline void capture_start(void) { TCCR1B = (1<<ICNC1) | (1<<ICES1) | (1<<CS10); } static inline uint16_t capture_read(void) { return ICR1; } static inline uint8_t capture_overflow(void) { return TIFR1 & (1<<TOV1); } static inline void capture_overflow_reset(void) { TIFR1 = (1<<TOV1); } static inline void capture_shutdown(void) { TCCR1B = 0; TIMSK1 = 0; TCCR1A = saveTCCR1A; TCCR1B = saveTCCR1B; } #define TIMER_OVERFLOW_VECTOR TIMER1_OVF_vect #define TIMER_CAPTURE_VECTOR TIMER1_CAPT_vect |
References
Reference | URL |
---|---|
Understanding Arduino Interrupts | Hardware, Pin Change & Timer Interrupts | https://www.youtube.com/watch?v=wIcC8-g9Lnw |
Electronic Basics #30: Microcontroller (Arduino) Timers | https://www.youtube.com/watch?v=IdL0_ZJ7V2s |
Configuring & Handling ESP32 GPIO Interrupts In Arduino IDE |