刚接触STM32L0系列的芯片,现在的SDK都是通过st的工具cube mx生成的,但定时器没有跑进设定的中断去。
请大神帮忙看看代码哪里出了问题,如果能提供份能正常跑起来的定时器代码也感激万分
void SysBaseTimerInit(void)
{
/* USER CODE BEGIN TIM21_Init 0 */
/* USER CODE END TIM21_Init 0 */
TIM_SlaveConfigTypeDef sSlaveConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
/* USER CODE BEGIN TIM21_Init 1 */
/* USER CODE END TIM21_Init 1 */
htim21.Instance = TIM21;
htim21.Init.Prescaler = 1600;
htim21.Init.CounterMode = TIM_COUNTERMODE_UP;
htim21.Init.Period = 10000;
htim21.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim21.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim21) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
if (HAL_TIM_OC_Init(&htim21) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;
sSlaveConfig.InputTrigger = TIM_TS_ITR0;
if (HAL_TIM_SlaveConfigSynchronization(&htim21, &sSlaveConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim21, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sConfigOC.OCMode = TIM_OCMODE_TIMING;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_OC_ConfigChannel(&htim21, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)
{
//if(htim->Instance == TimHandle.Instance)
{
if(a == 1)
{
LED1_WRITE(0);
a = 0;
}
else
{
a = 1;
LED1_WRITE(1);
}
}
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM21)
{
/* USER CODE BEGIN TIM21_MspInit 0 */
/* USER CODE END TIM21_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_TIM21_CLK_ENABLE();
/* TIM21 interrupt Init */
HAL_NVIC_SetPriority(TIM21_IRQn, 0, 2);
HAL_NVIC_EnableIRQ(TIM21_IRQn);
/* USER CODE BEGIN TIM21_MspInit 1 */
/* USER CODE END TIM21_MspInit 1 */
}
}
|