Solved
STM32f030f4p6 not isr on timer
I am a beginner. Please help me figure out why the interrupt is not happening. I want to measure the duty cycle, but the interrupt is not enabled. I am losing hope that this is possible.
#include <stdint.h>
#include <stm32f0xx.h>
#include <stdbool.h>
#include "uart.h"
int cnt = 0;
#define ledPin GPIO_ODR_4
#define baudRate 115200
char receivedString[100];
volatile uint32_t capture_value = 0;
volatile uint32_t frequency = 0;
volatile uint32_t duty_cycle = 0;
volatile bool state = false;
void SystemInit(void);
void TIM14_Config(void);
void TIM14_IRQHandler(void);
void init_PA4_LED(void);
void TIM3_Init(void);
void init_PA6_PWM_In(void);
void TIM3_IRQHandler(void);
int main(void) {
SystemInit();
UART_Init(baudRate);
init_PA4_LED();
TIM14_Config();
TIM3_Init();
init_PA6_PWM_In();
while (1) {
if (state) {
UART_SendString("Counter: ");
UART_SendNumber(cnt);
UART_SendString("\n");
state = false;
}
}
}
void SystemInit(void) {
if ((RCC->CR & RCC_CR_HSIRDY) == 0) {
RCC->CR |= RCC_CR_HSION;
while ((RCC->CR & RCC_CR_HSIRDY) == 0) {
}
}
RCC->CFGR &= ~RCC_CFGR_PLLSRC;
RCC->CFGR &= ~RCC_CFGR_PLLMUL;
RCC->CFGR |= RCC_CFGR_PLLMUL12;
RCC->CR |= RCC_CR_PLLON;
while ((RCC->CR & RCC_CR_PLLRDY) == 0) {
}
RCC->CFGR |= RCC_CFGR_SW_PLL;
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL) {
}
}
void init_PA4_LED(void) {
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
GPIOA->MODER |= GPIO_MODER_MODER4_0;
GPIOA->MODER &= ~GPIO_MODER_MODER4_1;
}
void TIM14_Config(void) {
RCC->APB1ENR |= RCC_APB1ENR_TIM14EN;
TIM14->PSC = 4799;
TIM14->ARR = 999;
TIM14->DIER |= TIM_DIER_UIE;
TIM14->CR1 |= TIM_CR1_CEN;
NVIC_EnableIRQ(TIM14_IRQn);
NVIC_SetPriority(TIM14_IRQn, 0);
}
void TIM14_IRQHandler(void) {
if (TIM14->SR & TIM_SR_UIF) {
TIM14->SR &= ~TIM_SR_UIF;
GPIOA->ODR ^= GPIO_ODR_4;
state = true;
}
}
void TIM3_IRQHandler(void) {
if (TIM3->SR & TIM_SR_CC1IF) {
capture_value = TIM3->CCR1;
cnt++;
if (capture_value != 0) {
frequency = 48000000 / capture_value;
duty_cycle = (TIM3->CCR1 * 100) / TIM3->ARR;
}
TIM3->SR &= ~TIM_SR_CC1IF;
}
}
void init_PA6_PWM_In(void) {
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
GPIOA->MODER &= ~(GPIO_MODER_MODER6);
GPIOA->MODER |= (GPIO_MODER_MODER6_1);
GPIOA->AFR[0] &= ~(GPIO_AFRL_AFSEL6);
GPIOA->AFR[0] |= (0x2 << GPIO_AFRL_AFSEL6_Pos);
}
void TIM3_Init(void) {
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
TIM3->CR1 = 0;
TIM3->PSC = 800 - 1;
TIM3->ARR = 60000 - 1;
TIM3->CR1 |= TIM_CR1_DIR;
TIM3->SMCR &= ~TIM_SMCR_SMS;
TIM3->CCMR1 &= ~TIM_CCMR1_CC1S;
TIM3->CCMR1 |= TIM_CCMR1_CC1S_0;
TIM3->CCMR1 &= ~TIM_CCMR1_IC1PSC;
TIM3->CCMR1 &= ~TIM_CCMR1_IC1F;
TIM3->CCER &= ~TIM_CCER_CC1P;
TIM3->CCER |= TIM_CCER_CC1E;
TIM3->CR2 &= ~TIM_CR2_MMS;
TIM3->DIER |= TIM_DIER_CC1IE;
TIM3->CR1 |= TIM_CR1_CEN;
NVIC_EnableIRQ(TIM3_IRQn);
NVIC_SetPriority(TIM3_IRQn, 0);
}
