www.gusucode.com > simulinkcoder 案例源码程序 matlab代码 > simulinkcoder/TimersCountersAbsAndElapsedTimeExample.m

    %% Optimize Memory Usage for Time Counters 

%%
% This example shows how to optimize the amount of memory that the 
% code generator allocates for time counters. The example optimizes the memory 
% that stores elapsed time, the interval of time between two events. 
% 
% The code generator represents time counters as unsigned integers. The word 
% size of time counters is based on the setting of the model configuration 
% parameter *Application lifespan (days)*, which specifies the expected maximum  
% duration of time the application runs. You can use this parameter to prevent 
% time counter overflows. The default size is 64 bits.
% 
% The number of bits that a time counter uses depends on the setting of the 
% *Application lifespan (days)* parameter. For example, if a time counter 
% increments at a rate of 1 kHz, to avoid an overflow, the counter has the 
% following number of bits:
%
% * Lifespan < 0.25 sec:   8 bits
% * Lifespan < 1 min:      16 bits
% * Lifespan < 49 days:    32 bits
% * Lifespan > 50 days:    64 bits
%
% A 64-bit time counter does not overflow for 590 million years.
%
%% Open Example Model
% Open the example model |rtwdemo_abstime|.
%
model='rtwdemo_abstime';
open_system(model); 
%%
% The model consists of three subsystems |SS1|, |SS2|, and |SS3|. On the 
% *Optimization* tab, the *Application lifespan (days)* parameter is set to the 
% default, which is |auto|.
%
% The three subsystems contain a discrete-time integrator that requires elapsed 
% time as input to compute its output value. The subsystems vary as 
% follows:
% 
% * SS1 - Clocked at 1 kHz. Does not require a time counter. *Sample time
% type* parameter for trigger port is set to |periodic|. Elapsed time is 
% inlined as 0.001. 
% * SS2 - Clocked at 100 Hz. Requires a time counter. Based on a lifespan of
% 1 day, a 32-bit counter stores the elapsed time.
% * SS3 - Clocked at 0.5 Hz. Requires a time counter. Based on a lifespan of
% 1 day, a 16-bit counter stores the elapsed time. 
%
%% Simulate the Model
%
% Simulate the model. By default, the model is configured to show sample times 
% in different colors.  Discrete sample times for the three subsystems appear 
% red, green, and blue. Triggered subsystems are blue-green.
%
%% Generate Code and Report
% 
% 1. Create a temporary folder for the build and inspection process.
currentDir = pwd;
[~,cgDir] = rtwdemodir();
%%
% 2. Configure the model for the code generator to use the GRT system
% target file and a lifespan of |inf| days.
%
% 3. Build the model.
rtwbuild(model)
%% Review Generated Code
%
% Open the generated source file |rtwdemo_abstime.c|. 
%
cfile = fullfile(cgDir, 'rtwdemo_abstime_grt_rtw', 'rtwdemo_abstime.h');
rtwdemodbtype(cfile,'/* Real-time Model Data Structure */', '/* Block states (auto storage) */', 0, 1);
%%
% Four 32-bit unsigned integers, |clockTick1| , |clockTickH1| , |clockTick2| , 
% and |clockTickH2| are counters for storing the elapsed time of subsystems 
% |SS2| and |SS3|.
%% Enable Optimization and Regenerate Code 
% 
% 1. Reconfigure the model to set the lifespan to 1 day. 
%
set_param(model, 'LifeSpan', '1');
%%
% 2. Build the model.
rtwbuild(model)
%% Review the Regenerated Code
cfile = fullfile(cgDir, 'rtwdemo_abstime_grt_rtw', 'rtwdemo_abstime.h');
rtwdemodbtype(cfile,'/* Real-time Model Data Structure */', '/* Block states (auto storage) */', 0, 1);
%%
% The new setting for the *Application lifespan (days)* parameter instructs the
% code generator to set aside less memory for the time counters.  The 
% regenerated code includes:
%
% * 32-bit unsigned integer, |clockTick1|, for storing the elapsed time of 
% the task for |SS2|
% * 16-bit unsigned integer, |clockTick2|, for storing the elapsed time of
% the task for |SS3|
%
%% Related Information
% * <docid:simulink_gui.bq7cqu2-1>
% * <docid:rtw_ug.f29370>
% * <docid:rtw_ug.f997321>
%%
bdclose(model)
rtwdemoclean;
cd(currentDir)