Blink using interrupts, not delay

timer_driven
LEdoian 3 years ago
parent 1b84426f04
commit b0a5df0fcf

@ -13,6 +13,7 @@
#include <libopencm3/stm32/gpio.h> #include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/f1/exti.h> #include <libopencm3/stm32/f1/exti.h>
#include <libopencm3/stm32/f1/nvic.h> #include <libopencm3/stm32/f1/nvic.h>
#include <libopencm3/stm32/f1/timer.h>
#include <stdbool.h> #include <stdbool.h>
//#define DELAY 8000000 // Should be a second, or maybe not (second iff 8MHz clk, 1/3second if 24MHz clk) // Too long!! //#define DELAY 8000000 // Should be a second, or maybe not (second iff 8MHz clk, 1/3second if 24MHz clk) // Too long!!
@ -61,10 +62,30 @@ void setup_exti0_in (void) {
exti_enable_request(EXTI0); exti_enable_request(EXTI0);
} }
void setup_tim6_periodic_irq (void) {
// Enable clock
rcc_periph_clock_enable(RCC_TIM6);
// Config:
// Set prescaler
timer_set_prescaler(TIM6, 65535);
// Set counter value
timer_set_period(TIM6, 183);
// Enable interrupts in NVIC
nvic_enable_irq(NVIC_TIM6_IRQ);
// Enable sending interrupts from the timer
timer_enable_irq(TIM6, TIM_DIER_UIE);
// Finally, enable the counter
timer_enable_counter(TIM6);
}
void setup (void) { void setup (void) {
setup_system(); setup_system();
setup_gpio_out(); setup_gpio_out();
setup_exti0_in(); setup_exti0_in();
setup_tim6_periodic_irq();
} }
// libopencm3 has a pre-declared interrupt handlers, so it needs to have this name // libopencm3 has a pre-declared interrupt handlers, so it needs to have this name
@ -82,12 +103,23 @@ void exti0_isr (void) {
running = true; running = true;
} }
void tim6_isr (void) {
timer_clear_flag(TIM6, TIM_SR_UIF);
// Just change value of GPIO C9 (green LED)
uint16_t cur = gpio_get(GPIOC, GPIO9);
if (cur) {
gpio_clear(GPIOC, GPIO9);
} else {
gpio_set(GPIOC, GPIO9);
}
}
int main (void) { int main (void) {
setup(); setup();
// Array of what leds should be on // Array of what leds should be on
uint16_t states[4] = {0, GPIO9, GPIO8, GPIO9 | GPIO8}; uint16_t states[4] = {0, 0, GPIO8, GPIO8};
char state = 0; char state = 0;
int i; int i;
@ -102,7 +134,7 @@ int main (void) {
state = (direction == FORWARD ? (state + 1)%4 : (/*state+4-1*/ state+3)%4 ); // New state state = (direction == FORWARD ? (state + 1)%4 : (/*state+4-1*/ state+3)%4 ); // New state
gpio_set(GPIOC, states[state]); gpio_set(GPIOC, states[state]);
gpio_clear(GPIOC, (GPIO8 | GPIO9) ^ states[state]); gpio_clear(GPIOC, (GPIO8) ^ states[state]);
} }
return 0; return 0;
} }

Loading…
Cancel
Save