I like to detect a button press to switch through 4 LEDs on the board, for example
Click-1 --> LED 1 on Click-2 --> LED 2 on Click-3 --> LED 3 on Click-4 --> LED 4 on Click-5 --> LED 1 off Click-6 --> LED 2 off Click-7 --> LED 3 off Click-8 --> LED 4 off Click-9 --> LED 1 on …
as much as possible, but button click detection does not work as expected:
#include <libopencm3/stm32/f4/rcc.h> #include <libopencm3/stm32/f4/gpio.h> uint16_t exti_line_state; /* Set STM32 to 168 MHz. */ static void clock_setup(void) { rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]); } static void gpio_setup(void) { /* Enable GPIOD clock. */ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); /* Set GPIO12 (in GPIO port D) to 'output push-pull'. */ gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12 | GPIO13 | GPIO14 | GPIO15); } static void button_setup(void) { /* Enable GPIOA clock. */ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); /* Set GPIO0 (in GPIO port A) to 'input open-drain'. */ gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO0); } int main(void) { int i; i = 1; clock_setup(); button_setup(); gpio_setup(); while (1) { exti_line_state = GPIOA_IDR; if ((exti_line_state & (1 << 0)) != 0) { if(i == 1){ i = 2; gpio_toggle(GPIOD, GPIO12); } if(i == 2){ i = 3; gpio_toggle(GPIOD, GPIO13); } if(i == 3){ i = 4; gpio_toggle(GPIOD, GPIO14); } if(i == 4){ i = 5; gpio_toggle(GPIOD, GPIO15); } if(i == 5){ i = 1; //Shut off all LEDs } } } return 0; }
source share