Proper way to set up the PWM input using Tim2 ch1 & ch3?
I'm using the STM32L031K6 chip. I have a 100HZ signal connected to PA0 (Timer2 ch1) and PB0 (Timer2 ch 3). I think I've setup everything correctly using the CubeMX.
The problem I'm finding is the interrupt only fires for the positive going edge on TIM_CHANNEL_1. The TIM_CHANNEL_3 never fires.
Here's my setup in TIM.C:
void MX_TIM2_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_IC_InitTypeDef sConfigIC;
htim2.Instance = TIM2;
htim2.Init.Prescaler = 3;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 0xffff;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
if (HAL_TIM_IC_Init(&htim2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
sConfigIC.ICFilter = 0;
if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_3) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}And here's my PWM interrupt handler:
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) // AC input pulses will trigger this interrupt
{
static uint16_t index = 0;
static uint16_t index2 = 0;
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
timerCount1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
pulseCount[index] = timerCount1;
index++;
if(index > 9)
index = 0;
TIM2->CNT = 0;
} // end of htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)
{
timerCount2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3);
pulseCount2[index2] = timerCount2;
index2++;
if(index2 > 9)
index2 = 0;
} // end of htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3Using my oscope I can see the pulses on PA0 and PB0 so I know the signal is there. Using the debugger I can see that TIM_Channel_1 is triggering but not channel_3. The pulse ON time is around 3ms and the off time is 7ms. The clock is at 8MHz with a prescaler of 3 so my timer clock should be 0.5Mhz.
Can anyone point me to where I went wrong?
Thanks in advance,
Ricahrd
