This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Problem variating the duty cycle

Other Parts Discussed in Thread: HALCOGEN

Hi

Im using TMS570LS1224 MCU for the control of 12 DC motors

I did a test to see the changes on the Duty cycle using this code: 

/* Include Files */

#include "sys_common.h"

/* USER CODE BEGIN (1) */
#include "stdio.h"
#include "system.h"
#include "etpwm.h"
unsigned int c;

void main(void)
{
c=0;


/* Initialise EPWM and ECAP with GUI configuration */
etpwmInit();
//ecapInit();
etpwmStartTBCLK();
for(;;){
do{

etpwmSetCmpA(etpwmREG1, 15);
etpwmSetCmpB(etpwmREG1, 15);
etpwmSetCmpA(etpwmREG2, 15);
etpwmSetCmpB(etpwmREG2, c);
etpwmSetCmpA(etpwmREG3, c);
etpwmSetCmpB(etpwmREG3, c);
etpwmSetCmpA(etpwmREG4, c);
etpwmSetCmpB(etpwmREG4, c);
etpwmSetCmpA(etpwmREG6, c);
etpwmSetCmpB(etpwmREG6, c);
etpwmSetCmpA(etpwmREG7, c);
etpwmSetCmpA(etpwmREG7, c);
c+=5;
__delay_cycles(160000000);
}while(c<100);
c=0;
}

/* Start counter in CountUp mode */
//etpwmSetCount(etpwmREG1, 0);
//etpwmSetCounterMode(etpwmREG1, CounterMode_Up);

// while(1);

/* USER CODE END */
}

but when runing, the code did not work as expected.. Am I missing something???

The configuration was done using Halcogen with a Period of 10kHz. The PWM works fine. But the problem is the duty cycle does not change. 

I don't know if the 0-100 is ok or if I should be using a different scale to reach the 0-100% 

I have also configured the VCLK4 at 80MHz

Best regards

  • Is your project very large, or can you export it to a .zip file and attach it to this thread? Please include the HALCoGen .hcg and .dil files.
  • Hi Bob

    I was wondering if you were able to open the files..

    best regards

  • I've checked your program.

    Something is happening, but you are setting a very very low duty cycle, and then wait many cycles before continuing.

    Your clock divider is 3199U

    /** - Sets time period or frequency for ETPWM block both PWMA and PWMB*/
    etpwmREG1->TBPRD = 3199U;

    for 50% duty cycle, you'd need to pass 13CCU to the etpwmSetCmpA() functions. (6348 decimal)

    edit: the register doesn't hold a percentage. If you put the same value as etpwmREG1->TBPRD (+1), you get  100%
    If you put (etpwmREG1->TBPRD)/2, you get 50%

    The maximum you pass is  100, that's very little duty cycle

    This example continuously runs from 0 to 100% (just replace your code in main.c with it)

    	etpwmInit();
    	//ecapInit();
    	etpwmStartTBCLK();
    	c = 0U;
    	while(1){
    
    			etpwmSetCmpA(etpwmREG1, c);
    			etpwmSetCmpB(etpwmREG1, c);
    			etpwmSetCmpA(etpwmREG2, c);
    			etpwmSetCmpB(etpwmREG2, c);
    			etpwmSetCmpA(etpwmREG3, c);
    			etpwmSetCmpB(etpwmREG3, c);
    			etpwmSetCmpA(etpwmREG4, c);
    			etpwmSetCmpB(etpwmREG4, c);
    			etpwmSetCmpA(etpwmREG6, c);
    			etpwmSetCmpB(etpwmREG6, c);
    			etpwmSetCmpA(etpwmREG7, c);
    			etpwmSetCmpA(etpwmREG7, c);
    			c+=1;
    			if(c>3199U) {c = 0;}
    			 __delay_cycles(160000);
    	}
    

  • Or in steps from 0 -> 99%:

    	while(1){
    
    			etpwmSetCmpA(etpwmREG1, (etpwmREG1->TBPRD/100)*c);
    			etpwmSetCmpB(etpwmREG1, (etpwmREG1->TBPRD/100)*c);
    			etpwmSetCmpA(etpwmREG2, (etpwmREG2->TBPRD/100)*c);
    			etpwmSetCmpB(etpwmREG2, (etpwmREG2->TBPRD/100)*c);
    			etpwmSetCmpA(etpwmREG3, (etpwmREG3->TBPRD/100)*c);
    			etpwmSetCmpB(etpwmREG3, (etpwmREG3->TBPRD/100)*c);
    			etpwmSetCmpA(etpwmREG4, (etpwmREG4->TBPRD/100)*c);
    			etpwmSetCmpB(etpwmREG4, (etpwmREG4->TBPRD/100)*c);
    			etpwmSetCmpA(etpwmREG6, (etpwmREG6->TBPRD/100)*c);
    			etpwmSetCmpB(etpwmREG6, (etpwmREG6->TBPRD/100)*c);
    			etpwmSetCmpA(etpwmREG7, (etpwmREG7->TBPRD/100)*c);
    			etpwmSetCmpA(etpwmREG7, (etpwmREG7->TBPRD/100)*c);
    			c+=1;
    			if(c>100) {c = 0;}
    			 __delay_cycles(1600000);
    	}
    

  • Heey! yeah!! I will try it this week and let you know. I need to change the period but I think with this I will do fine =)

    Thanks a lot!!

    Just one question... where did you get the 3199U and the 13CCU?

    What U means??

    Best regards!!

  • > 3199U
    You will find, if you step trough the etpwmInit() code, that that value is assigned to register TBPRD.
    It's a result of the clock setting of your dev board (I think yours is clocking at 80 MHz?),
    and the period you selected for your signal in HALCoGen. (It's by how much that 80 MHz is divided to get your desired signal period)

    In your case, a full period will be 3199 counts.

    Your signal A duty cycle ends up in register CMPA
    0 is a duty cycle of 0% (flat ground level signal)
    TBPRD + 1 = a duty cycle of 100% (flat 3.3V signal)
    TBPRD / 2 = 50% duty cycle

    > 13CCU
    That's a goof-up of me, because I mixed decimal and hexadecimal.
    You get a duty cycle of 50% by passing the half of what's in the TBPRD register.



    > What U means
    That's an indicator in the C language that a numeric constant is unsigned.