Skip to content

Instantly share code, notes, and snippets.

@cho0h5
Last active March 18, 2024 12:54
Show Gist options
  • Save cho0h5/044e403544b5977cddd4674de63ad7dd to your computer and use it in GitHub Desktop.
Save cho0h5/044e403544b5977cddd4674de63ad7dd to your computer and use it in GitHub Desktop.
#include "pico/stdlib.h"
#include "hardware/gpio.h"
const uint LED_PIN = 16;
const uint BUTTON_PIN = 15;
int init_pin() {
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
gpio_init(BUTTON_PIN);
gpio_set_dir(BUTTON_PIN, GPIO_IN);
gpio_pull_down(BUTTON_PIN);
}
int main() {
init_pin();
while (true) {
int state = gpio_get(BUTTON_PIN);
gpio_put(LED_PIN, state);
}
}
#include "pico/stdlib.h"
#include "hardware/gpio.h"
const uint LED_PIN = 16;
const uint BUTTON_PIN = 15;
void interrupt_handler(uint gpio, uint32_t events) {
if (events & 0b0100) // when the button is released
gpio_put(LED_PIN, 0);
else if (events & 0b1000) // when the button is pressed
gpio_put(LED_PIN, 1);
}
int init_pin() {
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
gpio_init(BUTTON_PIN);
gpio_set_dir(BUTTON_PIN, GPIO_IN);
gpio_pull_down(BUTTON_PIN);
}
int main() {
init_pin();
gpio_set_irq_enabled_with_callback(BUTTON_PIN, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, interrupt_handler);
while (true) {
// this core can do other job now
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment