⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 heat_pwm.m

📁 Matlab学习课件
💻 M
字号:
function done = heat_pwm
% PWM generator task for heater control program
% 'Done' is 1 if the task is finished with its current work
% File: heat_pwm.m

heatglbl 	% 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!

done = 1;	% This is the 'safe' default. If the programmer forgets to set a
		% value for 'done' it will default to 1 and not hang.
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 every opportunity -- the test functions will return with
% done=1 if there is nothing to be done. This will allow maximum precision
% in scheduling the pulse transition points.

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)
		done = 0;	% This state is always transient
		% 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)
		% Measure actual pwm duty cycle
		tstep = get_time;
		if start_pwm >= 0
			kdc = kdc + 1;
			meas_duty(kdc,1) = tstep;
			meas_duty(kdc,2) = (start_off - start_pwm) / (tstep - start_pwm);
		end
		start_pwm = tstep;
		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
		% Exit section for this transition
		done = 0;	% Go on to the next scan
	elseif (tstep >= end_on_time) & (duty_cycle >= 1)
		Spwm_gen_next = 1; % Transition to Compute_Times
		% Exit section for this transition
		done = 0;	% Go on to the next scan
	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
		tstep = get_time;
		start_off = tstep;
	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
		% Exit section for this transition
		done = 0;	% Go on to the next scan
	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 + -