HIDE NAV

Microcontroller Unit Lab 7

GPIO Interrupt Requests



Introduction

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.
 Do not use gpio_get() anywhere, instead using only the interrupt callback to manage input

Pt. 2  Add two more buttons to adjust blink speed - all managed by a single irq callback

Pt. 3  Create a 'proximity sensor alarm' with an infrared proximity sensor using GPIO Interrupts.



Example Code For Pt. 1


#include "pico/stdlib.h"

#define LED_ONB 25
#define BUTTON_BLINK 16

void gpio_callback(uint gpio, uint32_t event_mask);

bool do_blink = false;

void app_pin_setup();

int main() {
	// setup &  initialize
	app_pin_setup();

	irq_set_enabled(IO_IRQ_BANK0, true);
	gpio_set_irq_enabled(BUTTON_BLINK,GPIO_IRQ_EDGE_FALL,true);
	gpio_set_irq_enabled(BUTTON_BLINK,GPIO_IRQ_EDGE_RISE,true);
	gpio_set_irq_callback(gpio_callback);

	// primary loop
		while (true) {
			if(do_blink) {
				gpio_put(LED_ONB, 1);
				sleep_ms(150);
				gpio_put(LED_ONB, 0);
				sleep_ms(150);
			} else {
				sleep_ms(150);
		}
	}
}

void app_pin_setup(){
	gpio_init(BUTTON_BLINK);
	gpio_set_dir(BUTTON_BLINK, GPIO_IN);
	gpio_pull_up(BUTTON_BLINK);

	gpio_init(LED_ONB);
	gpio_set_dir(LED_ONB, GPIO_OUT);
}

void gpio_callback(uint gpio, uint32_t event_mask){
	if(gpio == BUTTON_BLINK){

		if(event_mask == GPIO_IRQ_EDGE_FALL){
			do_blink = true;
		} else if(event_mask == GPIO_IRQ_EDGE_RISE){
			do_blink = false;
		}

	}
}