From 2c547f642f098a4198d2c20fed9fb32a0b116190 Mon Sep 17 00:00:00 2001 From: Buhanka Date: Wed, 8 Apr 2026 17:57:25 +0300 Subject: [PATCH] button work, but dont understand how read input from pin --- main.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/main.c b/main.c index 9e7017f..d30c267 100644 --- a/main.c +++ b/main.c @@ -1,3 +1,4 @@ +#include #include #include "pico/stdlib.h" #include "hardware/gpio.h" @@ -7,31 +8,36 @@ #define BUTTON_PIN_COLUMN_1 18 #define LED_PIN 25 -void button_callback(uint pin); +void button_callback(uint gpio, uint32_t events); int main() { stdio_init_all(); + printf("Starting\n"); gpio_init(BUTTON_PIN_ROW_1); - gpio_init(BUTTON_PIN_COLUMN_1); + //gpio_init(BUTTON_PIN_COLUMN_1); gpio_set_dir(BUTTON_PIN_COLUMN_1, GPIO_OUT); gpio_set_dir(BUTTON_PIN_ROW_1, GPIO_IN); - gpio_pull_up(BUTTON_PIN_ROW_1); - gpio_pull_up(BUTTON_PIN_ROW_1); + gpio_pull_up(BUTTON_PIN_COLUMN_1); + gpio_set_irq_enabled(BUTTON_PIN_ROW_1, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true); + gpio_set_irq_callback(&button_callback); + irq_set_enabled(IO_IRQ_BANK0, true); + //gpio_set_irq_enabled_with_callback(BUTTON_PIN_ROW_1, GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true, &button_callback); gpio_init(LED_PIN); gpio_set_dir(LED_PIN, GPIO_OUT); while (true) { - if (!gpio_get(BUTTON_PIN_ROW_1)) { - gpio_put(LED_PIN, 0); - // printf("ROW: %i\n", gpio_get(BUTTON_PIN_ROW_1)); - // printf("COLUMN: %i\n", gpio_get(BUTTON_PIN_COLUMN_1)); - } else { - gpio_put(LED_PIN, 1); - // printf("ROW: %i\n", gpio_get(BUTTON_PIN_ROW_1)); - // printf("COLUMN: %i\n", gpio_get(BUTTON_PIN_COLUMN_1)); - } + sleep_ms(1000); } } -void button_callback(uint pin){ - +void button_callback(uint gpio, uint32_t events){ + gpio_acknowledge_irq(gpio, events); + if(gpio == BUTTON_PIN_ROW_1){ + if (events == GPIO_IRQ_EDGE_RISE){ + gpio_put(LED_PIN, true); + printf("irq is happend \n"); + } + else{ + gpio_put(LED_PIN, false); + } + } }