STM8s - PWM value update problem

Thread Starter

Prajeet Anand

Joined Aug 26, 2014
21
Hi,
I'm using stm8s903f3 board to generate a PWM of a certain frequency. In the main function I have implemented a If statement to check for a condition and then update the CCR2 value and pass it on to the PWM function that I'm calling in the If loop. There seems to a problem, the CCR2 value is not getting updated. What am I doing wrong? Here is a snippet of the code :

if( res >=1.65 && res < 1.675){
pwm = pwm_temp-25;
PWM_Combine_Start(duty_cycle_led, pwm);
} else if ( resistance >= 1.675 && resistance < 1.70){
pwm = pwm_temp-22.5;
PWM_Combine_Start(duty_cycle_led, pwm);
}
 

Thread Starter

Prajeet Anand

Joined Aug 26, 2014
21
Without attempting to update CCR2, is the PWM function working on its own?
PWM_Combine_Start(duty_cycle_led, pwm) function is being called.Inside that function I'm doing a calculation to update CCR2 value. CCR2 = 166.65 * pwm which is then passed in to the Tim1OC4Init.
 

MrChips

Joined Oct 2, 2009
23,086
PWM_Combine_Start(duty_cycle_led, pwm) function is being called.Inside that function I'm doing a calculation to update CCR2 value. CCR2 = 166.65 * pwm which is then passed in to the Tim1OC4Init.
I can see that. And that is the problem.

Answer the question. Does the PWM work if you give it one value and never change it?
 

MrChips

Joined Oct 2, 2009
23,086
Yes it works if I give it one value
Thanks.
You cannot update CCR2 on the fly randomly. It has to be in sync with CNT.
If CNT is already higher than the new CCR2 you will have a problem.
The proper place to update CCR2 is in an interrupt routine when CNT wraps around to zero.
Or you can reset CNT after setting CCR2.
 

Thread Starter

Prajeet Anand

Joined Aug 26, 2014
21
Thanks.
You cannot update CCR2 on the fly randomly. It has to be in sync with CNT.
If CNT is already higher than the new CCR2 you will have a problem.
The proper place to update CCR2 is in an interrupt routine when CNT wraps around to zero.
Or you can reset CNT after setting CCR2.
Thank you for the clarification.
 
Top