cancel
Showing results for 
Search instead for 
Did you mean: 

How accurate should the PWM input on a Nucleo32L031 be?

Richard Cooke
Associate II
Posted on September 04, 2017 at 22:25

Hi Folks,

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;

 HAL_TIM_SlaveConfigSynchronization(&htim2, &sSlaveConfig)

 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?

Thanks,

RC

1 ACCEPTED SOLUTION

Accepted Solutions
Posted on September 04, 2017 at 22:37

>>T

hese values are close but should they be closer? 

Prescaler and Period are programmed as N-1, ie dividing by 3000, set 2999, for 16 set 15

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

2 REPLIES 2
Posted on September 04, 2017 at 22:37

>>T

hese values are close but should they be closer? 

Prescaler and Period are programmed as N-1, ie dividing by 3000, set 2999, for 16 set 15

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Richard Cooke
Associate II
Posted on September 04, 2017 at 23:09

Thanks Clive,

I just wasjust typing in my admission that I goofed when I was doing the math in calculating the tick rate.  As soon as I subtracted 1 from the prescaler the math worked out within 0.6% which is close enough for my application.  I can't believe I wasted time trying to track this down when it was right in front of me.  Thanks again.

Richard