Topic : working on Ride 7 with STM8S105, regarding PWM

Forum : ST7/STM8

Original Post
Post Information Post
October 25, 2010 - 3:30am
Guest

hi, need some guidance about PWM codes.

I had manage to get the chip and LED up and running, making it glow from off to on. however as i change to higher frequency from 100hz, 1khz still fine, but from 10khz onwards, the glowing effect just went off, it's just like blinking on and off.

Below is my generalized code;

while(1)
{
Timer_Enable;
EnableInterrupts; //Global Interrupt Reset
if(Interrupt_flag== 1)
{
count++;
if(count == Pulse_Duration) //2 sec duration
{
Reset_Values;
}
else if(count == Pulse_Period) //1 sec off
{
Off Led;
}
else if((count <= Pulse_Period) && (count % Steps == 0)) // 1 sec glow
{
LED = Max_Brightness*Change_Rate //ON brightness of LED changes every time as the Change_Rate increases by 1%
Change_rate+= change;
}
Interrupt_flag = 0; //Reset Interrupt
}
}

So it's regarding the performance of the STM8 chip, please help enlighten me on how to change the counter freqeuency and can still produce the same pwm effect.

Replies
Post Information Post
+1
0
-1
October 25, 2010 - 9:22am
Raisonance Support Team

Hi,

It looks like you are facing an overflow.
You did not post your full code, in particular we do not have the type for your variables.
Set these variables as "unsigned long": LED, Max_Brightness, Change_Rate, change.
This should fix your problem.

However be aware that using long variable is expensive in terms of code size and speed, so in a real application you may want to rework your algorithm in order to use only "unsigned int" variables, which are MUCH more efficient.

Regards,
Bruno