📄 pwm_gen.m
字号:
function pwm_gen
% PWM generator task
pwm_glbl % Global definitions
% The 'next-state'is maintained as a separate variable so that the
% initialization canbe handled correctly and so a test can be
% constructed to tell whether the entry function of a state
% should be run. The next-state variable must be static so it
% remembered across function invocations. The 'global' designation
% is the only way Matlab has of making a variable static.
% The default value of the next-state variable is -1, indicating
% that no state change has taken place.
global Spwm_gen_next end_on_time end_off_time
% Next state for this task (other local statics
% can be listed here.
% These aren't really global, but do have to be 'static'
% Matlab doesn't have a 'static' declaration so this will have to do!
if Spwm_gen == 0
% Initialization section - occurs once only
Spwm_gen = 1; % Make sure this section is not executed again!
Spwm_gen_next = 1; % First state.
%The default is -1 which indicates stay in same state
duty_cycle = 0; % This takes the place of the Initialize_PWM state
on_time = 0;
off_time = pwm_period;
pwm_out = 0;
return;
end
% This task runs at specific times. If current time has not reached
% the scheduled run time, just return.
if tstep < Tpwm_gen
return;
end
Tpwm_gen = Tpwm_gen + Tgen_delt; % Time for next run -- in this case
% the task runs at regular intervals, but that needn't
% be true all the time.
if Spwm_gen_next ~= -1
% There has been a state change (including self-transition)
[i_audit,trans_trail] = audit(2,Spwm_gen,Spwm_gen_next,tstep,...
trans_trail,i_audit);
% Record this transition
Spwm_gen = Spwm_gen_next;
run_entry = 1;
Spwm_gen_next = -1; % Default - stay in same state
else
run_entry = 0;
end
% Run the code associated with the current state
if Spwm_gen == 1 % **Compute_Times**
% Entry section
if run_entry % Only run this on entry to the state
% code for entry section goes here (if any)
duty_cycle = limitval(duty_cycle,0,1);
end_on_time = duty_cycle * pwm_period + tstep;
end_off_time = pwm_period + tstep;
end
% Action section
% code for action section goes here (if any)
% No action function for this task
%Test/exit section
if duty_cycle > 0
Spwm_gen_next = 2; % Transition to PWM_On
% Exit section
% code for exit section (if any)
% No exit section for this transition
elseif duty_cycle <= 0
Spwm_gen_next = 3; % Transition to PWM_Off
% No exit section for this transition
end
elseif Spwm_gen == 2 % **PWM_On**
% Entry section
if run_entry % Only run this on entry to the state
% code for entry section goes here (if any)
pwm_out = 1; % Turn on the PWM output
end
% Action section
% code for action section goes here (if any)
% No action function for this task
%Test/exit section
if (tstep >= end_on_time) & (duty_cycle < 1)
Spwm_gen_next = 3; % Transition to PWM_Off
% No exit section for this transition
elseif (tstep >= end_on_time) & (duty_cycle >= 1)
Spwm_gen_next = 1; % Transition to Compute_Times
% No exit section for this transition
end
elseif Spwm_gen == 3 % **PWM_Off**
% Entry section
if run_entry % Only run this on entry to the state
% code for entry section goes here (if any)
pwm_out = 0; % Turn on the PWM output
end
% Action section
% code for action section goes here (if any)
% No action function for this task
%Test/exit section
if tstep >= end_off_time
Spwm_gen_next = 1; % Transition to Compute_Times
% No exit section for this transition
end
else % Check for a state variable value that doesn't
% correspond to a defined state.
fprintf('Uknown state: %f\n',Spwm_gen);
error('Task: pwm_gen -- unknown state encountered');
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -