Monitor and Tune GPIOs, ADCs, DACs, and ePWMs on C2000 MCUs | Getting Started with C2000 Microcontroller Blockset, Part 15 - MATLAB & Simulink
Video Player is loading.
Current Time 0:00
Duration 13:10
Loaded: 1.25%
Stream Type LIVE
Remaining Time 13:10
 
1x
  • Chapters
  • descriptions off, selected
  • en (Main), selected
    Video length is 13:10

    Monitor and Tune GPIOs, ADCs, DACs, and ePWMs on C2000 MCUs | Getting Started with C2000 Microcontroller Blockset, Part 15

    From the series: Getting Started with C2000 Microcontroller Blockset

    Learn how to use Model-Based Design to work with GPIOs, ADCs, DACs, and ePWMs on TI C2000™ MCUs using C2000 Microcontroller Blockset in Simulink®. Learn how you can use the monitoring and tuning capabilities offered by C2000 Microcontroller Blockset to tune the signals of an embedded algorithm running on a C2000 microcontroller from the Simulink enviroment and also log the signals in a host computer.

    Published: 13 Jan 2025

    Hello, everyone, this is Jay Karthik and Prabhakar here from MathWorks. In this video, I am going to show you how you can work with hardware peripheral blocks like GPIO, DAC, ADC, and ePWM available in the C2000 Microcontroller Blockset, to develop a embedded application in Simulink.

    By the end of this video, we will be creating a model that runs on C2000 MCU to toggle the LEDs available on the C2000 Launchpad, generate voltage on a DAC port, read voltage using an ADC channel, generate a PWM signal, and vary the duty cycle of the PWM signal.

    For any hardware project you want to work on Simulink, the first thing you need to do is make the model ready for the particular hardware you have. To do this, let's open Simulink and create a blank model. Then go to Model Settings and click on Hardware Implementation tab. Then you select the hardware board of your choice.

    Today, I will be using the F28379D Launchpad from TI, so I am going to select that. Then you need to make sure that the correct serial COM port of the launchpad is configured in the external mode option under Target hardware resources. This is useful to debug the project as we develop the model.

    You can find the COM port of your launchpad in the Device Manager in Windows. In my case, it is the COM23. Make sure you set the same value. Click on Apply and OK. Once you do this, you will see that there is a new tab named Hardware in the Simulink toolstrip. You can also see that the hardware we selected is also mentioned here.

    Now, open the Simulink Library Browser and under C2000 Microcontroller Blockset, open the F2837xD library. Here you can find the hardware peripheral blocks that we can use to build our embedded application in Simulink, for the F28379D Launchpad that we have.

    To get started, let me drag and drop the digital output block, which we can use to toggle the GPIOs on the hardware. Then let's add a constant block to the model and connect the output signal of the constant block to the input port of the digital output block.

    Now, if we run this model on hardware, we can toggle the GPIO on the hardware. To test this, let's try to toggle the onboard LEDs available on the launchpad. There are two LEDs on the launchpad, which is connected to the GPIO34 and GPIO31, as per the circuit diagram that is available from T's reference documents.

    To work with two GPIOs, we need another set of digital output and constant block. So let's copy paste these blocks and configure the digital output blocks for GPIO34 and 31. Double click on the digital output block to open the block parameters and configure the block for GPIO34 and 31, respectively.

    Then go to the Hardware tab and click on Monitor and Tune to generate code and run the model on the hardware. Make sure the stop time is set to infinite or the model will stop at the stop time you have set. Once the model starts running on the hardware, you can change the value on the constant block to see how that changes the LED status. Whenever you change the constant block value to 0, it makes the GPIO pins output to low and the LED turns on.

    When you set the constant block value to 1, that makes the GPIO pins output to high, and the LED turns off. The LEDs output is inverted because there is a open drain buffer circuit that is present which drives the LED.

    Let me stop the model now to add a digital to analog converter block to show how you can generate an analog signal on the hardware. Drag and drop the DAC block into the model and select the DAC channel in the block parameters.

    In my case, I am planning to use the A channel, and I have connected a multimeter to measure the voltage on the DAC A pin. The F28379D has a 12-bit DAC, so a range of input value from 0 to 4,095 will translate to a analog DC voltage of 0 to 3 volts on the DAC pin.

    Let's add a constant block and some basic logic to multiply the signal by 4,095 and divide it by 3 to give that output signal to the DAC input port and run the model. Once the model starts running, you can change the constant value and see how the voltage changes on the DAC pin, by looking at the multimeter measuring the voltage on the DAC pin.

    Now, say I set the value of the constant block to 3, the voltage measured is 3 on the DAC pin, which you can see on the multimeter. I set it to 2.5. The DAC outputs a 2.5 volt. I set it to 1, and the output is 1 volt. And I set it to 0, the output is 0 volts.

    This way of using model-based design to develop your embedded application takes a lot lesser time than manual hand coding by going through multiple pages of technical reference manuals of an MCU to do what we did just in few minutes. As you are iterating on the hardware itself with hardware peripherals during the development, you can debug your application much faster using Simulink and the C2000 Microcontroller Blockset.

    Now, let me add an ePWM block into the model to generate a PWM signal in one of the ePWM pins on the hardware. In the block parameters of the ePWM block, select the ePWM module that you want to use. In my case, it is going to be the ePWM. So I'm just leaving it as it is.

    Then set the time period units either in clock cycles or seconds. I am going to choose Seconds. If you are a pro user of C2000 MCUs, you can define it in clock cycles as well, for the particular MCU of your choice.

    The default value of time period in seconds is set to 100 microseconds, which means the ePWM module on the C2000 is expected to generate a 10 kilohertz PWM signal. You can also select the counter type in the counting mode parameter.

    I am going to configure the module with an up counter. Each ePWM module can generate two ePWM signals, ePWMA and ePWMB and are tied to two ePWM pins, respectively, on a C2000 device. For this demo, I am planning to use only one output, which is the ePWM1A.

    So make sure it is enabled in the ePWMA tab and set what action to take when the counter compare happens. In my case, I want to set the signal high when the counter equals 0 and clear the signal when the counter equals the counter compare A value on the up counter.

    Then in the Counter Compare tab, you should specify the counter compare A value either in clock cycles or in percentages. I will select percentages now. The default value of 50, when specified by a dialog on the block parameter, means the ePWM module is expected to generate the PWM signal at 50% duty cycle.

    Using the dropdown menu, you can change "specify compare A value via input" port to generate the input port on the ePWM block. This allows you to give input signal to the compare A value, which you can use to vary the duty cycle of the ePWM signal from the application that you are developing.

    Let's add a constant block and connect it to the input port of the ePWM block and run the model on the hardware. I have connected the output signal of the ePWM1A to a scope. So once the model starts running, you can notice that the frequency of the signal is 10 kilohertz, and the duty cycle of the PWM signal generated is 1%.

    As you vary the input on the constant block, you can see that the duty cycle of the generated PWM signal also varies accordingly. So like this, you can use these ePWM blocks in your model to build power conversion and motor control applications that require PWM signals at the end to control the switching devices.

    So far, all these blocks that I showed were actually generating only output signals. Next, let's read some values from the external world using the ADC block as an input. Just like the other blocks, we need to configure the ADC block with the ADC channel we want to use and provide an input voltage to the ADC pin that we can read from the hardware.

    I will be using the ADCA1 pin for this demo. And for the external input to this pin, I will short it with the DACA, which we use to generate the analog signal. So this way, we can check if we are able to read the same voltage on the ADC input, just as we see on the multimeter connected to the DAC pin.

    By default, the ADC block is configured for ADC resolution of 12-bit single ended input. And most of the DAC 2000 launchpads have an internal voltage reference set to 3 volts or 2.5 volt. The board that I have is set to 3-volt voltage reference high for the ADC input, which means that the output value of the ADC varies from 0 to 4,095 for a DC input from 0 to 3 volts.

    Since both DAC and ADC have the same range and resolution, we can apply the inverse logic of what we applied for DAC to measure the input voltage on the ADC pin using the ADC counts, which is to multiply the ADC counts by 3 and divide it by 4,095. I will add a display block to display the resulting value. It should be same as the analog voltage generated by the DAC pin that we see on the multimeter.

    Now, when you run the model, you can see that, as you vary the voltage of the DAC, the ADC and the multimeter reads the same voltage that the DAC pin is generating. This is happening because, as I mentioned earlier, we have shorted the DAC and the ADC pin. With the DAC, voltage is what we are reading on the ADC pin.

    Like this, you can use the ADC ports available on the C2000 MCU to read the analog voltage or connect any analog sensor to the ADC input port to read real world data using the ADC.

    Let's stop the model now. With that, we come to the end of this video, where you learned how to use GPIO, ADC, DAC, and ePWM.