📄 pwm_samp.m
字号:
% Simulation of PWM generation, based on Matlab template
% File: pwm_samp.m
% Created 7/26/95 by DM Auslander
% changed 8/1/95 to conform to new template format, DMA
pwm_glbl % Read in global definitions -- because each function
%in Matlab requires a new file, this is a more compact
%way to share variables
% System setup and initialization
% Put initial values for all variables here
% Simulation parameter values
tfinal = 10;
del_t = 0.01;
ept = del_t / 100; % Used to check for round-off errors
% Use this to control the number of output values
t_outint = del_t * 4; % Time interval for outputs
% This is particularly important for the student edition!
nouts = ceil(tfinal / t_outint);
yout = zeros(nouts + 1,2); % Set this up for proper output dimension
tout = zeros(nouts + 1,1);
t_outnext = 0; % Time at which next output will be copied
iout = 1; % Index for outputs
tstep = 0; % Running time
% Set up the options for the differential equation solver
% (if one is being used). The lines given here use a modified
% version of "Odesuite"
% No ODE used in this case
% ode_opts = odeset('refine',0,'rtol',1.e-3); % Differential solver options
% Make up a task list using the string-matrix function, str2mat()
% Note that this can only take 11 strings in each call, so it must be called
% multiple times for more than 11 tasks
% Each name represents a function (file) for that task
t1 = str2mat('pwm_cmd','pwm_gen'); % Two tasks - one sets the commanded duty
% cycle and the other generates the PWM signal
tasks = str2mat(t1);
nn = size(tasks);
ntasks = nn(1); % Number of rows in 'tasks'
% Initial variable values
duty_cycle = 0;
pwm_out = 0;
Tcmd_delt = 1; % How often the PWM command generator should run
Tgen_delt = 0.1; % How often the PWM generator itself should run
Tpwm_cmd = 0; % Next run time for command
Tpwm_gen = 0; % Next run time for PWM generator
cmd_period = 10;
pwm_period = 1;
pwm_out = 0;
% Set initial states
Spwm_cmd = 0; % A state of 0 is used as a a flag to the
Spwm_gen = 0; % state to do whatever initialization it needs
% Initialize all the tasks
for j = 1:ntasks
tsk = tasks(j,:); % name of j-th task
feval(tsk); % No arguments - task takes care of state
% and when it runs internally.
end
i_audit = 1; % Index for transition audit trail
tbegin = get_time; % Time at the beginning of simulation step
% Step out the solution
while tstep <= tfinal
% Copy output values to the output array
if (tstep + ept) >= t_outnext
t_outnext = t_outnext + t_outint; % Time for next output
yout(iout,1) = duty_cycle;
yout(iout,2) = pwm_out;
tout(iout) = tstep; % Record current time
iout = iout + 1;
end
% Run the control tasks
for j = 1:ntasks
tsk = tasks(j,:); % name of j-th task
feval(tsk); % No arguments - task takes care of state
% and when it runs internally.
inc_time; % Increment time
% Simulation stuff would go here if there were one
end
tstep = get_time; % Update time
end
% Record the final output values
yout(iout,1) = duty_cycle;
yout(iout,2) = pwm_out;
tout(iout) = tstep;
% Plot results
subplot(2,1,1); % Top subplot
plot(tout,yout(:,1),'r');
axis([0 tfinal 0 1]);
ylabel('Duty Cycle');
subplot(2,1,2); % Bottom subplot
plot(tout,yout(:,2),'g');
axis([0 tfinal 0 1.2]);
ylabel('PWM_Out');
xlabel('Time');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -