Continue to Site

# PWM duty cycle problem

Status
Not open for further replies.

##### Member level 3
HI, i have a problem creating this kind of waveform in PIC16F877A

my code only got the waveform until the vertical line.

here's my code:
while(1)
{
pwmplus=10;
for(lvl=0;lvl<=8;lvl++)
{
Pwm1_Change_Duty(unsign_pwm+pwmplus);
Delay_ms(1);
pwmplus=pwmplus+10;
}

}

pwmplus=80;
for(lvl=0;lvl<=8;lvl++)
{
Pwm1_Change_Duty(unsign_pwm-pwmplus);
Delay_ms(1);
pwmplus=pwmplus-10;
if(lvl==8)
{
Pwm1_Change_Duty(0);
}
}

it got an error.

can you help me with the code so that i could create the waveform above? thanks!

#### bjuric

In the first part, You increase PWM by 10, than 20, than 30.... with every for iteration.

But in the second part You subtract 80 in the first iteration, 70 in the second, 60 in the third, etc....

Try something like this for second part:
Code:
pwmplus=10;                               //start from 10
for(lvl=0; lvl<=8; lvl++)
{
Pwm1_Change_Duty(unsign_pwm-pwmplus);
Delay_ms(1);
pwmplus=pwmplus+10;                     //increase by 10
if(lvl==8)
{
Pwm1_Change_Duty(0);
}
}

#### andre_luis

##### Super Moderator
Staff member
Not too clear if desired output waveform is a triangle of sinusoid.

+++

##### Member level 3
thanks for the replies

@bjuric:

i've already done than that but still it gave me errors in using the simulation proteus. is there another way to create such wave form?

@andre_teprom:

the desired output is a pwm duty cycle that is increasing up to a point then returning back to the orignal duty cycle. i've already done the increasing part, i'm having problems in decreasing.

I've tried isolating functions (increasing/decreasing). When i run the program with the increasing part without the decreasing part, it works, also with the decreasing part without the increasing part. but when i tried running it together, the program crashes.

#### andre_luis

##### Super Moderator
Staff member
...but when i tried running it together, the program crashes...

Are you referring to runtime error, or to compilation error ?

+++

##### Member level 3
runtime error. the compilation is successful but using the simulator PROTEUS, it says that the error is within the CALL stack.

#### KerimF

Pwm1_Change_Duty(unsign_pwm-pwmplus);

I always wrote in assembly so I have no sure idea what "unsign_pwm" is. I guess it is a constant. In that case the above line should be:
Pwm1_Change_Duty(unsign_pwm+pwmplus);
I mean to keep the + sign.

##### Member level 3
@KerimF:

im sorry i didn't put the entire code. the unsign_pwm is a variable output of the ADC. can you tell me how can i return back to the original duty cycly if im going to keep the + sign? thanks.

#### KerimF

Pwm1_Change_Duty(unsign_pwm+pwmplus);

Do you mean that during the execution of the loop
for(lvl=0;lvl<=8;lvl++) {....}
the variable unsign_pwm is also changing?

##### Member level 3
nope. it would stay the same. lets just say that the unsign_pwm = 150;

#### KerimF

ok... so we need only to let the variable pwmplus... increase then decrease... and nothing else.

For instance, did you change - to + as I suggested and you couldn't get the result?

I think you already know that your loop iterates 9 times and not 8... right?

May I ask how the sum "unsign_pwm+pwmplus" is related to the duty cycle?
I mean which variable determines the PWM period (interval if you like)
To be honest, your number 150 as a minimum limit and 150+80=230 cannot give the general shape of the signal on your picture of the 1st post.

Last edited:

#### alexan_e

Is your PWM 8bit, with values 0-255?
Why do you use increase by 10 and why do you decrease by 80 and then 70..60 .. etc

a value of 150 will become 160,170,180,190,200,210,220,230 and then 230-80, 150-70, 80-60..

Is this what you want to do?

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pwmplus=80;
for(lvl=0; lvl<=16; lvl++)
{
if (lvl<8)
{
Pwm1_Change_Duty(pwmplus); // 80,90,100,110,120,130,140,150
pwmplus+=10;
}
else
{
pwmplus-=10;
Pwm1_Change_Duty(pwmplus); // 140,130,120,110,100,90,80,70,60

}
Delay_ms(1);
}

Alex

Status
Not open for further replies.