Microcontroller Unit Lab 5
GPIO Interrupt Requests
Intro
Interrupt Requests (IRQs) are a way to trigger code execution on hardware events. An IRQ pauses current excecution of the stack & goes to a specific subroutine/function called an IRQ Callback which can be defined by your code. The CPU returns to paused stack location after IRQ Callbacak finishes.
Summary
Trigger code execution using an Interrupt with a variety of devices.
Pt. 1 A button press enables/disables hello-world LED blink.
Example Code For Pt. 1
#include "pico/stdlib.h"
const uint button_gp_pin = 18;
const uint led_onb_gp_pin = 25;
bool enable_blink = false;
void button_press_callback(uint gpio, uint32_t event_mask);
int main() {
// setup & initialize
gpio_init(button_gp_pin);
gpio_set_dir(button_gp_pin, GPIO_IN);
gpio_init(led_onb_gp_pin);
gpio_set_dir(led_onb_gp_pin, GPIO_OUT);
//setting up interrupt on the button gp pin
gpio_set_irq_enabled_with_callback(button_gp_pin, GPIO_IRQ_EDGE_FALL, true, button_press_callback);
// primary loop
while (true) {
if(enable_blink){
gpio_put(led_onb_gp_pin, 1);
sleep_ms(250);
gpio_put(led_onb_gp_pin, 0);
sleep_ms(250);
}else {
sleep_ms(1);
}
}
}
//note that global variables like this are typically placed at the top of a source file
uint64_t last_button_press_time = 0;
uint64_t debounce_delay_us = 500000; //half second delay for debounce
void button_press_callback(uint gpio, uint32_t event_mask){
uint64_t current_time = time_us_64();
uint64_t time_since_last_press = current_time - last_button_press_time;
if(time_since_last_press > debounce_delay_us){
enable_blink = !enable_blink; //toggle this variable t/f
last_button_press_time = current_time;
}
}