2013-03-07 04:01 AM
Hi All,
I am using the STM8L152 to generate a PWM, with it's duty cycle depending on current flowing through a relay coil. I have achieved this using the ETR input, with the following code:/* Time base configuration */
TIM1_TimeBaseInit(TIM1_PRESCALER, TIM1_CounterMode_Up, TIM1_PERIOD, TIM1_REPTETION_COUNTER);
/* Toggle Mode configuration: Channel1 */
TIM1_OC1Init(TIM1_OCMode_PWM2, TIM1_OutputState_Enable, TIM1_OutputNState_Disable,
CCR1_Val, TIM1_OCPolarity_Low, TIM1_OCNPolarity_Low, TIM1_OCIdleState_Set, TIM1_OCNIdleState_Set);
//TIM1->CCMR1 |= 0b10000000; // Turn on ETR clear
TIM1_ETRClockMode1Config(TIM1_ExtTRGPSC_OFF, TIM1_ExtTRGPolarity_NonInverted, 0x00);
//TIM1->SMCR |= (1 << 3);
//TIM1_SelectInputTrigger(TIM1_TRGSelection_ETRF);
TIM1_SelectSlaveMode(TIM1_SlaveMode_Reset);
TIM1_OC1PreloadConfig(DISABLE);
/* Enable TIM1 outputs */
TIM1_CtrlPWMOutputs(ENABLE);
/* TIM1 enable counter */
TIM1_Cmd(ENABLE);
However, this method resets the counter on ETR input causing a variation in frequency.
I read on page 329 of the reference manual (section 5.9), that the OCiREF signal can be cleared in response to an ETR.
Should should mean the frequency stays the same, but the duty cycle adjusts.
Have been unsuccessful so far in achieving this. Does anybody have a working example of this method?
Thanks,
Martin.
#pwm-etr
2013-03-07 01:21 PM
Hi,
Ok I solved it with this code:/* Toggle Mode configuration: Channel1 */
TIM1_OC1Init(TIM1_OCMode_PWM1, TIM1_OutputState_Enable, TIM1_OutputNState_Disable,
CCR1_Val, TIM1_OCPolarity_High, TIM1_OCNPolarity_Low, TIM1_OCIdleState_Reset, TIM1_OCNIdleState_Set);
TIM1_ETRConfig(TIM1_ExtTRGPSC_OFF, TIM1_ExtTRGPolarity_NonInverted, 0x0A);
/* Turn on ETR clears OCiREF */
TIM1_ClearOC1Ref(ENABLE);
TIM1_SelectOCREFClear(TIM1_OCReferenceClear_ETRF);
TIM1_OC1PreloadConfig(DISABLE);
/* Enable TIM1 outputs */
TIM1_CtrlPWMOutputs(ENABLE);
/* TIM1 enable counter */
TIM1_Cmd(ENABLE);
Thanks,
Martin.