how to make a PWM with a variable duty cycle, one duty cycle fixed during a time and after the second one?

Dear community,
I’ve got a problem, even if I try many things; I have to admit that I don’t know how to solve it. It’s the first I face of timing problem.
The problem:
I want to generate a pwm with a variable duty cycle, the frequency is fixed. If the condition is true (‘1’): From t=0 to t=200ms the duty cycle is 50% from t=200ms to condition is not true the duty cycle is 10%. If the condition is false (‘0’): duty cycle is 0.
Why I do this? I want to control a motor with a variable duty cycle to save energy, I just need more current at the start to move and after less current just to keep the move.
So I try something like this but I really no sure that it is the good way I developed a Simulink diagram: http://img4.hostingpics.net/pics/883426image001.png
Pwm_gen1
In the FCN, I try several codes but they didn’t work, I don’t know if it’s better to use another block or way to do it… Code inside FCN
function y = fcn(u)
%#eml
if (u >= 1)
y = 0.5;
%I want to wait here 200ms before changing the duty cycle
y = 0.1;
else
y=0;
end
I try to put wait () instruction but it seems not compatible inside “embedded matlab function” block. I try another think, I counted the rising edge of the duty cycle (using ‘detect rise positive’ block)and when I reach a value equivalent to 200ms, I change the duty cycle but I didn’t manage it, I think I have a problem with initiate a value for a counter (first time I do it).
Does someone has some idea to help me to solve this problem? Does someone face of this kind of problem?
Thanks in advance community.
p.s I used Matlab-Simulink R2009a.

1 Comment

You could create your own Level 2 S-function that outputs a square wave, and then based on its input changes what the duty cycle of that square wave is.
If you were willing to attempt this approach, i have already built a custom S-function block that is somewhat near to what you are trying to do and could edit it into something workable.

Sign in to comment.

Answers (0)

Communities

More Answers in the  Power Electronics Control

Asked:

on 4 May 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!