Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

PWM duty cycle problem

Status
Not open for further replies.

nadre25

Member level 3
Joined
May 15, 2009
Messages
56
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,780
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;
}

}

when i add:

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

Advanced Member level 4
Joined
May 4, 2011
Messages
115
Helped
32
Reputation
64
Reaction score
30
Trophy points
1,308
Location
Serbia
Activity points
1,980
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_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,187
Helped
1,141
Reputation
2,301
Reaction score
1,116
Trophy points
1,403
Location
Brazil
Activity points
53,394
Not too clear if desired output waveform is a triangle of sinusoid.

+++
 

nadre25

Member level 3
Joined
May 15, 2009
Messages
56
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,780
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.
 

nadre25

Member level 3
Joined
May 15, 2009
Messages
56
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,780
runtime error. the compilation is successful but using the simulator PROTEUS, it says that the error is within the CALL stack.
 

KerimF

Advanced Member level 4
Joined
May 17, 2011
Messages
1,461
Helped
374
Reputation
756
Reaction score
370
Trophy points
1,363
Location
Syria
Activity points
12,314
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.
 

nadre25

Member level 3
Joined
May 15, 2009
Messages
56
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,780
@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

Advanced Member level 4
Joined
May 17, 2011
Messages
1,461
Helped
374
Reputation
756
Reaction score
370
Trophy points
1,363
Location
Syria
Activity points
12,314
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?
 

nadre25

Member level 3
Joined
May 15, 2009
Messages
56
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,780
nope. it would stay the same. lets just say that the unsign_pwm = 150;
 

KerimF

Advanced Member level 4
Joined
May 17, 2011
Messages
1,461
Helped
374
Reputation
756
Reaction score
370
Trophy points
1,363
Location
Syria
Activity points
12,314
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?

Added:
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

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,020
Reputation
4,156
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,377
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.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top