我想使用2-s级功能生成方形时钟。我试过这个。
function pll2( block)
%UNTITLED13 Summary of this function goes here
% Detailed explanation goes here
setup(block);
function setup(block)
% Register number of ports
block.NumInputPorts = 1;
block.NumOutputPorts = 1;
block.InputPort(1).Dimensions = 1;
block.InputPort(1).DatatypeID = 0; % boolean
block.InputPort(1).Complexity = 'Real';
block.InputPort(1).DirectFeedthrough = true;
% Override output port properties
block.OutputPort(1).Dimensions = 1;
block.OutputPort(1).DatatypeID = 0; %double
block.OutputPort(1).Complexity = 'Real';
block.NumDialogPrms = 1;
block.DialogPrmsTunable = {'Tunable'};
Ts = 1;
block.SampleTimes = [Ts 0];
% Specify the block simStateCompliance. The allowed values are:
% 'UnknownSimState', < The default setting; warn and assume DefaultSimState
% 'DefaultSimState', < Same sim state as a built-in block
% 'HasNoSimState', < No sim state
% 'CustomSimState', < Has GetSimState and SetSimState methods
% 'DisallowSimState' < Error out when saving or restoring the model sim state
block.SimStateCompliance = 'DefaultSimState';
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
%block.RegBlockMethod('InitializeConditions', @InitializeConditions);
%block.RegBlockMethod('Start', @Start);
block.RegBlockMethod('Outputs', @Outputs); % Required
block.RegBlockMethod('Update', @Update);
%block.RegBlockMethod('Derivatives', @Derivatives);
block.RegBlockMethod('Terminate', @Terminate); % Required
%block.RegBlockMethod('SetInputPortSamplingMode', @SetInpPortFrameData);
%end setup
function DoPostPropSetup(block)
block.NumDworks = 1;
block.Dwork(1).Name = 'c';
block.Dwork(1).Dimensions = 1;
block.Dwork(1).DatatypeID = 0;%double
block.Dwork(1).Complexity = 'Real'; % real
block.Dwork(1).UsedAsDiscState = true;
function Outputs(block)
%block.Dwork(1).Data = counter;
t = block.CurrentTime;
l = 0.8*exp(-9);
%c = 220*exp(-15) + block.InputPort(1).Data*3*exp(-15);
f = 1/(2*pi*sqrt(l*(block.Dwork(1).Data)));
block.OutputPort(1).data = square(2*pi*f*t);
function Update(block)
c = block.Dwork(1).Data;
c = 220*exp(-15) + block.InputPort(1).Data*3*exp(-15); %% block.InputPort(1).data is a value which is updated by another block, it is an integer value.
block.Dwork(1).Data = c;
%counter = block.Dwork(1).Data;
%%
%% Terminate:
%% Functionality : Called at the end of simulation for cleanup
%% Required : Yes
%% C-MEX counterpart: mdlTerminate
%%
function Terminate(block)
%end SetInpPortFrameData
%end Terminate
但问题是这个方形时钟不能正常工作, 那么有没有其他方法来生成一个时钟,你可以根据需要更新电容器C的值,如代码所示。由block.InputPort(1).Data。此块已在simulink中模拟。