cancel
Showing results for 
Search instead for 
Did you mean: 

Setting up my STM32L031 for PWM input using TIM2

Richard Cooke
Associate II
Posted on August 30, 2017 at 03:38

Hi Folks,

I think I've set up my TIM2 correctly using the CUBEMX but I'm new to the STM32 world and I'd like someone to take a look at this.  I'm going to be reading the output from a optocoupler connected to the 120VAC line and then through a buffer so the frequency will be 60Hz (here in the US).  I need to measure the pulse width to an accuracy of +/- 500 uS (1/2000 sec). I can then calculate the peak voltage and take action if it falls below a set value. I haven't been able to find a clear description on how to set up the TIM2 in CubeMX but here's what I've started with:

0690X000006043DQAQ.jpg0690X000006043NQAQ.jpg

Am I close to getting it correct?  My pulse generator decided that today was a good day to quit so I won't be able to test the code until Thursday when the new one shows up.

Any help will be greatly appreciated.

RC

#pwm #pwm-input
3 REPLIES 3
Posted on August 30, 2017 at 03:45

Configure another TIM to generate the pulses and loop back the external wiring, scoping to confirm signal form.

For measuring PWM consider using 'PWM Input' mode, it pairs channels 1 and 2, and resets the counter, so you can read period and pulse width from CCR1/2

Want to keep prescaler low to improve granularity, but likely constrained here by 16-bit counter.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on August 30, 2017 at 04:49

Thanks Clive,

Thanks for the idea of using the PWM output to test the input.  That must be why you get the big money. 

They don't make it easy to find but I think I stumbled on it.  Is this correct?

0690X000006043IQAQ.jpg0690X000006043SQAQ.jpg

I'm not sure of the Trigger Output (TRGO) Parameters.  Do they look OK?

Thanks again.  You've saved me untold hours of frustration.

RC

Posted on August 30, 2017 at 05:42

Unfortunately not much of a CubeMX guy, looks reasonable enough, I'd look at the output code. There is likely an example under the Cube/HAL directories, here for the L4, likely a similar set under the L0

STM32Cube_FW_L4_V1.8.0\Projects\STM32L496ZG-Nucleo\Examples\TIM\TIM_PWMInput

In the SPL this code takes CH2 as an input, and CCR2=Period, CCR1=Duty

/* ---------------------------------------------------------------------------

TIM4 configuration: PWM Input mode

In this example TIM4 input clock (TIM4CLK) is set to 2 * APB1 clock (PCLK1),

since APB1 prescaler is different from 1.

TIM4CLK = 2 * PCLK1

PCLK1 = HCLK / 4

=> TIM4CLK = HCLK / 2 = SystemCoreClock /2

External Signal Frequency = TIM4 counter clock / TIM4_CCR2 in Hz.

External Signal DutyCycle = (TIM4_CCR1*100)/(TIM4_CCR2) in %.

--------------------------------------------------------------------------- */

TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;

TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

TIM_ICInitStructure.TIM_ICFilter = 0x0;

TIM_PWMIConfig(TIM4, &TIM_ICInitStructure);

/* Select the TIM4 Input Trigger: TI2FP2 */

TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);

/* Select the slave Mode: Reset Mode */

TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);

TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);

/* TIM enable counter */

TIM_Cmd(TIM4, ENABLE);

/* Enable the CC2 Interrupt Request */

TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..