I'm relatively new to the STM32 universe so please the giggling to a minimum.
I set up the TIM21 to output a 60Hz signal and with an ON time of 3000 ticks x 32 x 1/8MHz = 12.0ms
htim21.Instance = TIM21;
htim21.Init.Prescaler = 32;
htim21.Init.CounterMode = TIM_COUNTERMODE_UP;
htim21.Init.Period = 4000;
htim21.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
Hooking up my scope I measure the output waveform as period = 16.5ms and ON time = 12.33ms which gives a frequency of 1/0.0165 = 60.6Hz. This is pretty close to the 60Hz it should be. The question I have concerns reading this signal using the PWM input method.
I'm using TIM2 and channel 1 to read the PWM signal.
Here's my code for setting up the TIM2:
htim2.Instance = TIM2;
htim2.Init.Prescaler = 16;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 0xFFFF;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;
sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;
sSlaveConfig.TriggerPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sSlaveConfig.TriggerPrescaler = TIM_ICPSC_DIV1;
sSlaveConfig.TriggerFilter = 2;
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
sConfigIC.ICFilter = 2;
HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1)
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
sConfigIC.ICSelection = TIM_ICSELECTION_INDIRECTTI;
HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2)
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
The issue I'm running into is that the TIM2->CCR1 and TIM2->CCR2 values are only close to what they should be. The CCR1 count value should be the period and the CCR2 should be the ON time count value. With the 60Hz and 12.33ms ON time input the debugger is showing CCR1 = 0x1e52 = 7762d. At the 8Mhz clock and 16 prescaler the tick should be 0.125us x 16 = 2us/tick. CCR1 = 7762 x 2us = 15.5us. CCR2 = 5820 = 11.64us. These values are close but should they be closer? Is the 32L031 chip capable of better?
My scope shows a period of 16.5ms and an ON time of 12.4ms but TIM2->CCR1 shows 15.5ms and TIM->CCR2 shows 11.64ms.
If I setup TIM21 to output a 600hz signal the TIM2 PWM input values are much closer to what they should be. My scope shows the ON time to be 1238.5us and the TIM2->CCR2 = 9886 x 0.125us/tick = 1236us. So it's within 2 us which is where it should be.
Does anybody know of a way to get TIM2 to accurately measure slow PWM inputs?