Main Content

Use Simulink Strings inside C Function Block to Model Scrolling Display

This example shows how to use Simulink® strings inside the C Function block to model a scrolling display for a user-defined Simulink® string.

Open the model. In this model, DisplayController subsystem receives a custom string and outputs a fixed-size Simulink® string with a scrolling effect.

mdl = "CFunctionStringExample.slx";
open_system(mdl)

Scrolling display model.

The DisplayController subsystem contains two C Function blocks, Scrolling Display and Send to Hardware.

Contents of DisplayController masked subsystem.

The mask parameter Display Size indicates the size of the scrolling display. The C Function block named Scrolling Display uses this mask parameter in conjunction with the input string length to achieve the desired string output. This model is configured to output 20 characters at each time step.

The DisplayController block Display Size parameter is set to 20.

Scrolling Display

The C Function block named Scrolling Display receives a dynamic Simulink® string named inputStr at its input port and outputs a fixed-size Simulink® string named outputStr. The output code, initial values of the parameters, and ports and parameter names are defined in the Output section, Start section, and Ports and Parameters table, respectively.

This block interprets a Simulink® string of length n as a C character array of length of n+1. The Buffer size of dynamically-sized string (bytes) parameter under Advanced parameters section of Simulation Target pane of Configuration Parameters dialog box determines number of character buffer generated for dynamic string signals. This allows the C Function block to use string library functions and zero-based indexing to output a fixed-size Simulink® string with a scrolling effect.

Output code, port and parameter settings for the C Function block that outputs scrolling display.

Send to Hardware

The C Function block named Send to Hardware propagates the fixed-size string outputStr to an external hardware display board. This block uses a hardware function call set_display that uses outputStr as an input argument to generate the code for the external hardware display board.

Block parameters for the C Function block named Send to Hardware uses a hardware function call set_display() to generate code.

Simulate Model and Visualize Results

Simulate the model and visualize the display in the Display block named Simulation Display. To change the display size, double-click the DisplayController subsystem and change the value of the Display Size parameter. To change the input string, double-click the String Constant block and change the string.

out = sim (mdl);

The Display blocks shows the scrolling effect of the output string.

See Also

|

Topics