Add timer.{h, c}

This commit is contained in:
Alexander Heldt
2024-08-03 11:49:46 +02:00
parent 9b131a3c24
commit 062a014c7c
2 changed files with 60 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
#include "rcc.h"
#include "timer.h"
void tim4_init(void) {
// Enable timer
RCC->APB1ENR |= RCC_APB1ENR_TIM4_ENABLE;
// Reset timer
TIM4->CR1 = 0x0000;
TIM4->CR2 = 0x0000;
// Set prescaler
// f_clk = 48MHz -> /48000 = 1KHz counting frequency = 1ms
TIM4->PSC = (uint16_t) 48000 - 1;
// Set ARR to maximum value to get 1ms between updates
TIM4->ARR = (uint16_t) 0xFFFF;
}
void tim4_start(void) {
TIM4->CR1 |= TIM4_ENABLE;
}