Apply the c2000lib Blockset
Introduction
This section uses an example to show how to create a Simulink® model that uses C2000™ Microcontroller Blockset 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:
Connect the function generator output to the ADC input ADCINA0 on the eZdsp F2812.
Connect the output of PWM1 on the eZdsp F2812 to the analog input of the oscilloscope.
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:
c2000lib
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:
In the Simulink Editor of your model, select Simulation > Model Configuration Parameters > Hardware Implementation.
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.
Pane
Field
Setting
Solver
Type
Fixed-stepSolver
Solver
discrete (no continuous states)Solver
Higher priority value indicates higher task priority
Value is 'off' and the parameter is disabledCode Generation > Optimization
Remove internal data zero initialization
Value is 'off' and the parameter is disabledCode Generation > Optimization
Maximum stack size (bytes)
512 (MAU)Hardware Implementation
Device vendor
Texas InstrumentsHardware Implementation
Device type
C2000Hardware Implementation
Support long long
Selected by defaultCode Generation > Interface
Code replacement library
TI C28xClick Apply.
Note
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 block in your model.
Adding Blocks to the Model
Open or double-click the C281x library,
c281xlib.Drag the ADC block into your model. Double-click the ADC block in the model and set Sample time to
64/80000.Drag the PWM block into your model. Double-click the PWM block in the model and set the following parameters.
Pane Field Parameter Timer Module AWaveform period source Specify via dialogWaveform period units Clock cyclesWaveform period 64000Waveform type AsymmetricOutputs Enable PWM1/PWM2 Selected Duty cycle source Input portLogic PWM1 control logic Active highPWM2 control logic Active lowDeadband Use deadband for PWM1/PWM2 Selected Deadband prescaler 16Deadband period 12ADC Control ADC start event Period interruptEnter
simulinkin the MATLAB Command Window to open the Simulink Library browser. Drag a Gain 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.Pane Field Parameter Main Gain 30Multiplication Element-wise(K.*u)Sample time -1Signal Attributes Output data type mode uint(16)Integer rounding mode FloorParameter Attributes Parameter data type mode Inherit from inputConnect 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.