How to Work with ePWMs on C2000 MCUs | Getting Started with C2000 Microcontroller Blockset, Part 14
From the series: Getting Started with C2000 Microcontroller Blockset
Follow this step-by-step guide on how to design a model in Simulink® using ePWM blocks to generate ePWM signals on TI C2000™ MCUs using C2000 Microcontroller Blockset. Follow a detailed walkthrough of the steps required to modify duty cycles from Simulink on a PWM signal generated by an F28379D LaunchPad™.
Published: 13 Jan 2025
Welcome to yet another video on PWM. In this video, let's see how a PWM signal is constructed. Let me illustrate this by generating a PWM signal of 5 kilohertz with 50% duty cycle.
I have the Simulink model configured for F28379D Launchpad. And I have added ePWM block. Let me open the block parameters. To configure the frequency of the PWM signal, I have two options here in the timer period units. One is seconds and clock cycles.
If I choose seconds, I can directly provide the time period of the PWM signal, which is 1 divided by 5 kilohertz, which is equal to 200 microsecond, here in the timer period parameter. By entering this, I have configured the PWM frequency. Now, to set the duty cycle, let me navigate to the Counter Compare tab, where CMPA, that is, Compare a Register, is used to configure the duty cycle.
Again, I have two ways. One is by choosing percentages or clock cycle. By choosing Percentage, I can enter 50 to set the duty cycle of the PWM signal. This is straightforward. But configuring the PWM frequency and duty cycle with the help of a clock cycle is always recommended.
Though you enter the PWM frequency and duty cycle in terms of seconds and percentages, internally, the controller operates in terms of clock cycle. So let us see how to do that. Before that, let me introduce some variables used here in this calculation. The PWM frequency is 5 kilohertz.
And hence, the time period comes to 1 divided by 5 kilohertz, which is 200 microseconds, which I have represented it by Tctr. We know that each peripheral runs at its own clock, and the ePWM clock frequency is megahertz. That is, the time period represented by TBCLK is 10 nanoseconds.
You can refer to my previous video on how to set the ePWM clock frequency. Now, navigating to the documentation, you can calculate the TBPRD value based on the count mode whether it is up, down, or up down. Let me configure it for up counter mode. So let me use this formula to calculate the TBPRD value. And hence, I get a TBPRD equal to 19,999.
Going back to the block parameter, let me choose the count mode to up and enter the TBPRD value to 19,999. Make sure you have chosen clock cycle as the timer period units. Let's see what happens when I enter TBPRD value here. Now, the up counter starts counting from 0 to TBPRD.
The count value increments by one for every TPCLK, which is 10 nanoseconds. As the count value reaches the TBPRD, the counter resets to 0 and starts incrementing again by 1, and this cycle keeps repeating.
Now, let's configure the duty cycle, which is based on the CMPA. You can set the CMPA value in the range of 0 to TBPRD. For a 50% duty cycle, let me set it to half of TBPRD, which is 19,999 divided by 2, which is 10,000.
Having set these values here in the configurations, let's now come to the final construction part. This construction is configured here in the ePWM a tab, where you can set the action. You can set the action when counter is 0, Counter reaches CMPA, counter reaches period. And you have the options to set, clear, toggle, or do nothing. And depending upon the action you choose, you can generate different PWM signals.
Say, for example, I have cleared the signal when the counter is 0, and I have set the signal when the counter reaches CMPA value, and I have cleared the signal when the counter reaches period. By this way, I would be able to generate the PWM signal that is shown here.
If I set the signal when counter reaches 0 and clear the signal when counter reaches CMPA and again set the signal when counter reaches period, I'd be able to come up with this kind of PWM signal. So by varying the CMPA value, you'd be able to change the duty cycle of the PWM signal.
For simplicity, I have explained it with the CMPA. But for the construction of a ePWMA signal, there are two comparators, one is CMPA and one is CMPB.
As there are multiple types of PWM, like leading-edge modulated signal, trailing-edge modulated signal, and pulse center to edge modulated signal, depending upon the type of modulation, you can set the values of CMPA and CMPB and select the action you want to perform. By this way, you would be able to generate different PWM signals for your application.
And again, I have explained this for up counter. You can either choose down counter or up down counter. In down counter, the counter initially loads with a value of TBPRD and counts down to 0. And when it reaches to 0, the counter again resets to TBPRD value and decrements by 1. And this cycle keeps repeating.
When the count mode is set to up down, the counter is initially loaded with 0, and the count value keeps incrementing by 1 until it reaches to TBPRD and then starts decrementing by 1 until it reaches to 0. And in between, it checks for the CMPA and CMPB value.
And depending upon the action that you have configured, the PWM signal will be generated. So this is how a PWM signal is constructed. Thanks for watching this video.