我的目标是中断38khz并切换LED,但无论我设置为预分频器和周期
static TIM_HandleTypeDef s_TimerInstance = {
.Instance = TIM2,
};
void Hal::initTim()
{
__TIM2_CLK_ENABLE();
s_TimerInstance.Init.Prescaler = 96; // This does nothing
s_TimerInstance.Init.CounterMode = TIM_COUNTERMODE_UP;
s_TimerInstance.Init.Period = 26; // This does nothing
s_TimerInstance.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&s_TimerInstance);
HAL_TIM_Base_Start_IT(&s_TimerInstance);
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
}
extern "C" void TIM2_IRQHandler()
{
HAL_TIM_IRQHandler(&s_TimerInstance);
}
extern "C" void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
HAL_GPIO_TogglePin(GPIOI, GPIO_PIN_1); // Happens every 43 seconds for some reason
}
extern "C" void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim){}
extern "C" void HAL_TIMEx_CommutationCallback(TIM_HandleTypeDef *htim){}
当我将其闪烁到目标时,无论预分频器和周期设置为何,LED都会每43秒切换一次。
我一定错过了什么,但我无法弄清楚。有中断经验的人可以提供帮助吗?
答案 0 :(得分:2)
好。定时器非常简单。你刚刚使用立方体做了很多。
__TIM2_CLK_ENABLE();
TIM2->PSC = 96; // prescaler (note that 0 is /1)
TIM2->ARR = 0xFFFF-26; // reload on overflow
TIM2->DIER = TIM_DIER_UIE; // overflow isr
TIM2->CR1 |= TIM_CR1_CEN; // enable timer
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
在isr中:
if(TIM2->SR & TIM_SR_UIF){
TIM2->SR &= ~TIM_SR_UIF;
}
暂且不说。我想在init上放心重置条件。
__HAL_RCC_TIM2_FORCE_RESET();
__NOP();
__HAL_RCC_TIM2_RELEASE_RESET();
答案 1 :(得分:0)
STM32F469I-DISCOVERY的不同项目,但我遵循了Jeroen3的回答。我不知道你会在哪里获得__HAL_RCC
...和__NOP();
代码,但它没有它。我计算了预分频器和周期是一些因素。例如,为了获得1Hz的闪烁LED,我会预分频器'* period'= timer_clock,我学到的是core_clock / 2.
因此,180MHz的核心时钟和1Hz的所需中断将给出
(预分频器+ 1)*(周期+ 1)= 180MHz / 2 / 1Hz = 90M
可分为9000和10000.我不确定预分频器,但16位定时器的周期不能超过2¹⁶-1。这意味着因子不能是90000和1000因为90000超过2'-1。它也不是900和100000。
extern "C"
{
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_tim.h"
#include "stm32469i_discovery.h" //Needed for LED
/* User can use this section to tailor TIMx instance used and associated
resources */
/* Definition for TIMx clock resources */
#define TIMx TIM3
#define TIMx_CLK_ENABLE() __HAL_RCC_TIM3_CLK_ENABLE()
/* Definition for TIMx's NVIC */
#define TIMx_IRQn TIM3_IRQn
#define TIMx_IRQHandler TIM3_IRQHandler
void TIM3_IRQHandler()
{
if(TIMx->SR & TIM_SR_UIF){
TIMx->SR &= ~TIM_SR_UIF;
BSP_LED_Toggle(LED2);
}
}
void initTimer()
{
TIMx_CLK_ENABLE();
//SystemCoreClock = 180MHz
//PSC = 180/2/10000-1 = 9000-1
TIMx->PSC = (uint32_t)((SystemCoreClock / 2) / 10000) - 1;// prescaler (note that 0 is /1)
TIMx->ARR = 10000-1; // reload on overflow
TIMx->DIER = TIM_DIER_UIE; // overflow isr
TIMx->CR1 |= TIM_CR1_CEN; // enable timer
HAL_NVIC_SetPriority(TIMx_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIMx_IRQn);
}
}
#include "gui/model/Timer.hpp"
void Timer::init()
{
initTimer();
}