Macro Definitions (#define)
C Construct
#define myParam 9.8;
Export Generated Macro Definition
1. Open example modelex_param_macro
.
2. In the model, select the Gain block. In the Property Inspector, set the value of theGainparameter tomyParam
.
3. Next to the parameter value, click the action button (the button with three vertical dots) and selectCreate.
4.In the Create New Data dialog box, setValuetoSimulink.Parameter(9.8)
. ClickCreate.A Simulink.Parameter
object,myParam
, appears in the base workspace. The Gain block uses the object to set the value of the Gain parameter, in this case,9.8
.
5. On theCode Generationtab, click theConfigure in Coder Appbutton. In the Code Mappings editor, setStorage Classto定义
. ClickOK.
6. To build the model and generate code, pressCtrl+B.
The generated header fileex_param_macro.h
定义了myParam
as a macro.
/* Definition for custom storage class: Define */ #define myParam 9.8 /* Referenced by: '/Gain' */
Reuse Macro from Handwritten Code
1. In the Code Mappings editor, on theParameterstab, click theUpdate Code Mappingsbutton.
2. ChangeStorage ClassofmyParam
from定义
toImportedDefine
.
3. In the Property Inspector, underCodesection, setHeader Filetoexternal_params.h
. The generated code imports the macro definition from a custom header file namedexternal_params.h
.
4.In your current folder, create the C header fileexternal_params.h
, which contains the#define
statement.
#ifndef _EXTERNAL_PARAMS #define _EXTERNAL_PARAMS #define myParam 9.8 #endif /* EOF */
5. To build the model and generate code, pressCtrl+B.
The generated header fileex_param_macro.h
does not define the macro. Instead, the file includes(#include)
the custom header fileexternal_params.h
.
/* Includes for objects with custom storage classes */ #include "external_params.h"
The source fileex_param_macro.c
contains a guard to check that a definition formyParam
exists.
#include "rtwtypes.h" #include "external_params.h" /* * Check that imported macros with storage class "ImportedDefine" are defined */ #ifndef myParam #error The variable for the parameter "myParam" is not defined #endif