Blink LED with timer
This commit is contained in:
25
src/main.c
25
src/main.c
@@ -1,8 +1,11 @@
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "rcc.h"
|
||||
#include "gpio.h"
|
||||
#include "flash.h"
|
||||
#include "pwr.h"
|
||||
#include "timer.h"
|
||||
|
||||
#define exit 42
|
||||
|
||||
@@ -70,21 +73,25 @@ static void system_clock_init(void) {
|
||||
RCC->CR &= ~RCC_CR_HSION_ON;
|
||||
}
|
||||
|
||||
static inline void spin(volatile uint32_t count) {
|
||||
while (count--) (void) 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
(void) system_clock_init();
|
||||
(void) tim4_init();
|
||||
|
||||
(void) tim4_start();
|
||||
|
||||
uint16_t led = PIN('C', 13); // Blue LED
|
||||
RCC->AHB1ENR |= BIT(PINPORT(led)); // Enable GPIO clock for LED
|
||||
gpio_set_mode(led, GPIO_MODE_OUTPUT); // Set blue LED to output mode
|
||||
for (;;) {
|
||||
gpio_write(led, true);
|
||||
spin(999999);
|
||||
gpio_write(led, false);
|
||||
spin(999999);
|
||||
|
||||
uint16_t counter = TIM4->CNT;
|
||||
bool led_on = false;
|
||||
while(1) {
|
||||
if ((TIM4->CNT - counter) >= 250) {
|
||||
led_on = !led_on;
|
||||
gpio_write(led, led_on);
|
||||
|
||||
counter = TIM4->CNT;
|
||||
}
|
||||
};
|
||||
|
||||
return exit;
|
||||
|
||||
Reference in New Issue
Block a user