Main Content

Generate Code Using Simulink Coder

This example shows how to select a system target file for a Simulink® model, generate C code for real-time simulation, and view generated files.

The model represents an 8-bit counter that feeds a triggered subsystem that is parameterized by constant blocks INC, LIMIT, and RESET. Input and Output represent I/O for the model. The Amplifier subsystem amplifies the input signal by gain factor K, which updates when signal equal_to_count is true.

1. Open model CounterModel and configure the system target file to use grt.tlc.

model='CounterModel';
open_system(model)

2. Open the Simulink Coder app.

3. Open the Model Configuration Parameters dialog box. In the C Code tab, click Settings. Alternatively, from the command line, enter:

cs = getActiveConfigSet(model);
openDialog(cs);

4. Select the Code Generation tab.

5. In the Target Selection pane, click Browse to select a system target file.

You can generate code for a specific target computer environment or purpose. Some options are provided by built-in system target files, which control the code generation process for a target computer environment.

6. Select the Generic Real-Time (GRT) system target file and click Apply.

Optionally, in the Code Generation Advisor pane set the parameter Select objective to Execution efficiency or Debugging. Then, to identify and systematically change parameters to meet your objectives, click Check model...

7. Generate code for the model by using one of these options:

  • On the C Code tab, click Build.

  • Press Ctrl+B.

8. View the code generation report.

The report includes links to model files such as CounterModel.c and associated utility and header files.

This figure contains a portion of CounterModel.c.

9. Close the model.

bdclose(model)