AnsweredAssumed Answered

STM8 Timer 1 and constant current control using ETR

Question asked by Martin Davey on Mar 7, 2013
Latest reply on Mar 7, 2013 by Martin Davey
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 19.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.



 

Outcomes