Main Content

Apply the c2000lib Blockset


This section uses an example to show how to create a Simulink® model that uses Embedded Coder® Support Package for Texas Instruments™ C2000™ Processors blocks to target your board. The example creates a model that performs PWM duty cycle control via pulse width change. It uses the C2812 ADC block to sample an analog voltage and the C2812 PWM block to generate a pulse waveform. The analog voltage controls the duty cycle of the PWM and you can observe the duty cycle change on the oscilloscope.

Hardware Setup

The following hardware is required for this example:

  • Spectrum Digital eZdsp F2812

  • Function generator

  • Oscilloscope and probes

To connect the target hardware:

  1. Connect the function generator output to the ADC input ADCINA0 on the eZdsp F2812.

  2. Connect the output of PWM1 on the eZdsp F2812 to the analog input of the oscilloscope.

  3. Connect VREFLO to AGND on the eZdsp F2812. See the section on the Analog Interface in Chapter 2 of the eZdsp™ F2812 Technical Reference.

Starting the c2000lib Library

At the MATLAB® prompt, type the following command:


This command opens the c2000lib library blockset, which contains libraries of blocks designed for targeting your board.

Setting Up the Model

Preliminary tasks for setting up a new model include configuring your model for the target hardware and setting the simulation parameters.

To configure your model with ert.tlc system target file:

  1. In the Simulink Editor of your model, select Simulation > Model Configuration Parameters > Hardware Implementation.

  2. Select a TI C2000 Hardware board. The Texas Instruments Code Composer Studio (c2000) option automatically gets selected for Toolchain.

    The following default settings in the Simulation > Model Configuration Parameters dialog box appear when you select your C2000 hardware board.









    discrete (no continuous states)


    Higher priority value indicates higher task priority

    Value is 'off' and the parameter is disabled

    Code Generation > Optimization

    Remove internal data zero initialization

    Value is 'off' and the parameter is disabled

    Code Generation > Optimization

    Maximum stack size (bytes)

    512 (MAU)

    Hardware Implementation

    Device vendor

    Texas Instruments

    Hardware Implementation

    Device type


    Hardware Implementation

    Support long long

    Selected by default

    Code Generation > Interface

    Code replacement library

    TI C28x

  3. Click Apply.


The generated code does not honor Simulink ‘stop time’ from the simulation. The ‘stop time’ is interpreted as inf. To implement a stop in the generated code, you must put a Stop Simulation (Simulink) block in your model.

Adding Blocks to the Model

  1. Open or double-click the C281x library, c281xlib.

  2. Drag the ADC block into your model. Double-click the ADC block in the model and set Sample time to 64/80000.

  3. Drag the PWM block into your model. Double-click the PWM block in the model and set the following parameters.

    Waveform period sourceSpecify via dialog
    Waveform period unitsClock cycles
    Waveform period64000
    Waveform typeAsymmetric
    OutputsEnable PWM1/PWM2Selected
    Duty cycle sourceInput port
    LogicPWM1 control logicActive high
    PWM2 control logicActive low
    DeadbandUse deadband for PWM1/PWM2Selected
    Deadband prescaler16
    Deadband period12
    ADC ControlADC start eventPeriod interrupt

  4. Enter simulink in the MATLAB Command Window to open the Simulink Library browser. Drag a Gain (Simulink) block from the Math Operations library into your model. Double-click the Gain block in the model and set the following parameters in the Function Block Parameters dialog box. Click OK.

    Sample time-1
    Signal AttributesOutput data type modeuint(16)
    Integer rounding modeFloor
    Parameter AttributesParameter data type modeInherit from input

  5. Connect the ADC block to the Gain block and the Gain block to the PWM block.

Generating Code from the Model

This section summarizes how to generate code from your real-time model.

There are three ways to start the automatic code generation process:

  • In the Simulink Editor, click Build Model or Deploy to Hardware.

  • On your model, press Ctrl+B.

  • Press the Build button on the Code Generation pane of the Configuration Parameters dialog box.