my board:STM32 H743 after configure the advanced timer to capture pwm input,i can get the pwm frequency. but if i use hal_init() in the
main函数,不能得到,为什么?我在中断中添加了led,现在led不打开,意味着,可以进入回调函数。显示代码:
主:
HAL_Init();
SystemClock_Config();
TIMx_Configuration();
TIM8_Configuration();
configuration part:
在回调函数
void TIMx_Configuration(void)
{
//TIMx_GPIO_Config();
HAL_NVIC_SetPriority(GENERAL_TIM_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(GENERAL_TIM_IRQn);
GENERAL_TIM_CLK_ENABLE();
TIM_TimeBaseStructure.Instance = GENERAL_TIM;
TIM_TimeBaseStructure.Init.Period = 10000 - 1;
TIM_TimeBaseStructure.Init.Prescaler = 240000 - 1;
HAL_TIM_Base_Init(&TIM_TimeBaseStructure);
HAL_TIM_Base_Start_IT(&TIM_TimeBaseStructure);
}
void TIM8_Configuration()
{
//set priority
HAL_NVIC_SetPriority(MOTORIC_TIM_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(MOTORIC_TIM_IRQn);
TIM_IC_InitTypeDef TIM_ICInitStructure;
TIM_SlaveConfigTypeDef TIM_SlaveConfigStructure;
MOTORIC_TIM_CLK_ENABLE();
motoric_htimx_bldcm.Instance = MOTORIC_TIM;
motoric_htimx_bldcm.Init.Period = 100;
// TIMxCLK = HCLK=240MHz // timer frequency=TIMxCLK/(TIM_Prescaler+1)=1MHz
motoric_htimx_bldcm.Init.Prescaler = MOTOR_ICPWM_PRESCALER_COUNT - 1;
motoric_htimx_bldcm.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;
motoric_htimx_bldcm.Init.CounterMode=TIM_COUNTERMODE_UP;
//motoric_htimx_bldcm.Init.RepetitionCounter=0;
HAL_TIM_IC_Init(&motoric_htimx_bldcm);
- configure timer mode*
TIM_ICInitStructure.ICPolarity = TIM_ICPOLARITY_RISING;
TIM_ICInitStructure.ICSelection = TIM_ICSELECTION_DIRECTTI;
TIM_ICInitStructure.ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.ICFilter = 0x0;
HAL_TIM_IC_ConfigChannel(&motoric_htimx_bldcm,&TIM_ICInitStructure,TIM_CHANNEL_1);
HAL_TIM_IC_ConfigChannel(&motoric_htimx_bldcm,&TIM_ICInitStructure,TIM_CHANNEL_2);
HAL_TIM_IC_ConfigChannel(&motoric_htimx_bldcm,&TIM_ICInitStructure,TIM_CHANNEL_3);
HAL_TIM_IC_ConfigChannel(&motoric_htimx_bldcm,&TIM_ICInitStructure,TIM_CHANNEL_4);
TIM_SlaveConfigStructure.SlaveMode = TIM_SLAVEMODE_RESET;
TIM_SlaveConfigStructure.InputTrigger = TIM_TS_TI1FP1;
HAL_TIM_SlaveConfigSynchronization(&motoric_htimx_bldcm,&TIM_SlaveConfigStructure);
//四通道捕获每个通道采用IC it模式,输入频率约为1.5khz
HAL_TIM_IC_Start_IT(&motoric_htimx_bldcm,TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&motoric_htimx_bldcm,TIM_CHANNEL_2);
HAL_TIM_IC_Start_IT(&motoric_htimx_bldcm,TIM_CHANNEL_3);
HAL_TIM_IC_Start_IT(&motoric_htimx_bldcm,TIM_CHANNEL_4);
}
刚刚引导,显示中断是ok的
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == GENERAL_TIM)
{
LED1_TOGGLE;
}
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
//获取捕获值
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
LED1_TOGGLE;
IC1Value = HAL_TIM_ReadCapturedValue(&motoric_htimx_bldcm,TIM_CHANNEL_1);
IC2Value = HAL_TIM_ReadCapturedValue(&motoric_htimx_bldcm,TIM_CHANNEL_2);
if (IC1Value != 0)
{
//计算频率
DutyCycle = (float)((IC2Value+1) * 100) / (IC1Value+1);
Frequency = 240000000/24000/(float)(IC1Value+1);
}
else
{
if not get, 0
DutyCycle = 0;
Frequency = 0;
}
}
}
最后,这个问题解决了。这不是关于hal_init(),原因是:我的PWM捕获配置配置为从模式,需要更改为主模式。像这样:
TIM_MasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
TIM_MasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&motoric_htimx_bldcm, &TIM_MasterConfig);