DSP

DSP28335 epwm使用

2019-07-13 10:58发布

PWM1,2,3的A,B,C 6个通道初始化 #include "pwm.h"
//PRD:自动重装值
//CMPA:通道A比较值
//CMPB:通道B比较值
void InitEPwm123(int16 PRD)
{
   InitEPwmGpio();          //初始化EPWM1,2,3的GPIO
   Epwm1_Init(PRD);
   Epwm2_Init(PRD);
   Epwm3_Init(PRD);
}


//设置EPWM1模块
void Epwm1_Init(int16 PRD)
{
EPwm1Regs.TBPRD = PRD ; //设置周期
    EPwm1Regs.CMPA.half.CMPA = 0;  //设置占空比
EPwm1Regs.CMPB = 0;


// Setup TBCLK
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP;  //递增模式
//EPwm1Regs.TBCTL.bit.CTRMODE =TB_COUNT_UPDOWN;  //增减模式
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;    // Disable phase loading
EPwm1Regs.TBCTL.bit.PRDLD = TB_SHADOW;    // Disable phase loading
EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_DISABLE;    //
EPwm1Regs.TBPHS.half.TBPHS = 0x0000;       // Phase is 0
EPwm1Regs.TBCTR = 0x0000;                  // Clear counter
EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1;   // Clock ratio to SYSCLKOUT
EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1;      // TBCLK = SYSCLKOUT/(HSPCLKDIV*CLKDIV)


// Setup shadow register load on ZERO
EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
    EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;
EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;


// 递增模式的动作
EPwm1Regs.AQCTLA.bit.ZRO = AQ_SET;            // Set PWM1A on Zero
EPwm1Regs.AQCTLA.bit.CAU = AQ_CLEAR;          // Clear PWM1A on event A, up count
EPwm1Regs.AQCTLB.bit.ZRO = AQ_SET;            // Set PWM1B on Zero
EPwm1Regs.AQCTLB.bit.CBU = AQ_CLEAR;          // Clear PWM1B on event B, up count
}


void Epwm2_Init(int16 PRD)
{
EPwm2Regs.TBPRD = PRD ; //设置周期
EPwm2Regs.CMPA.half.CMPA = 0;  //设置占空比
EPwm2Regs.CMPB = 0;
// Setup TBCLK
EPwm2Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP;//; // Count up
EPwm2Regs.TBCTL.bit.PHSEN = TB_DISABLE;    // Disable phase loading
EPwm2Regs.TBCTL.bit.PRDLD = TB_SHADOW;    // Disable phase loading
EPwm2Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_DISABLE;    //
EPwm2Regs.TBPHS.half.TBPHS = 0x0000;       // Phase is 0
EPwm2Regs.TBCTR = 0x0000;                  // Clear counter
EPwm2Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1;   // Clock ratio to SYSCLKOUT
EPwm2Regs.TBCTL.bit.CLKDIV = TB_DIV1;      // TBCLK = SYSCLKOUT/(HSPCLKDIV*CLKDIV)


// Setup shadow register load on ZERO
EPwm2Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
EPwm2Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
EPwm2Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;
EPwm2Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;


// Set actions
EPwm2Regs.AQCTLA.bit.ZRO = AQ_SET;            // Set PWM1A on Zero
EPwm2Regs.AQCTLA.bit.CAU = AQ_CLEAR;          // Clear PWM1A on event A, up count
EPwm2Regs.AQCTLB.bit.ZRO = AQ_SET;            // Set PWM1B on Zero
EPwm2Regs.AQCTLB.bit.CBU = AQ_CLEAR;          // Clear PWM1B on event B, up count
}




void Epwm3_Init(int16 PRD)
{
EPwm3Regs.TBPRD = PRD ; //设置周期
EPwm3Regs.CMPA.half.CMPA = 0;  //设置占空比
EPwm3Regs.CMPB = 0;
    // Setup TBCLK
EPwm3Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP;//; // Count up
EPwm3Regs.TBCTL.bit.PHSEN = TB_DISABLE;    // Disable phase loading
EPwm3Regs.TBCTL.bit.PRDLD = TB_SHADOW;    // Disable phase loading
    EPwm3Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_DISABLE;    //
EPwm3Regs.TBPHS.half.TBPHS = 0x0000;       // Phase is 0
EPwm3Regs.TBCTR = 0x0000;                  // Clear counter
EPwm3Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1;   // Clock ratio to SYSCLKOUT
EPwm3Regs.TBCTL.bit.CLKDIV = TB_DIV1;      // TBCLK = SYSCLKOUT/(HSPCLKDIV*CLKDIV)


// Setup shadow register load on ZERO
EPwm3Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
EPwm3Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
EPwm3Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;
    EPwm3Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;


    // Set actions
EPwm3Regs.AQCTLA.bit.ZRO = AQ_SET;            // Set PWM1A on Zero
EPwm3Regs.AQCTLA.bit.CAU = AQ_CLEAR;          // Clear PWM1A on event A, up count
    EPwm3Regs.AQCTLB.bit.ZRO = AQ_SET;            // Set PWM1B on Zero
EPwm3Regs.AQCTLB.bit.CBU = AQ_CLEAR;          // Clear PWM1B on event B, up count
}
头文件 #ifndef PWM_H_
#define PWM_H_
#include "main.h"
#if (CPU_FRQ_150MHZ)
  #define CPU_CLK   150e6
#endif
#if (CPU_FRQ_100MHZ)
  #define CPU_CLK   100e6
#endif
#define PWM_FREQ   10e3                // 10KHZ   If diff freq. desired, change freq here.
//#define SP        CPU_CLK/(2*PWM_CLK)   //上下计数模式
#define SP        ( (CPU_CLK/PWM_FREQ)-1 )     //向上计数模式的自动重装值   10Khz的PWM波

#define SP_UPDOWN         (CPU_CLK/(2*PWM_FREQ))    //向上计数模式的自动重装值

// Configure the period for each timer
#define EPWM1_TIMER_TBPRD  SP  // Period register
#define EPWM1_MAX_CMPA     SP-10
#define EPWM1_MIN_CMPA       10
#define EPWM1_MAX_CMPB     SP-10
#define EPWM1_MIN_CMPB       10

#define EPWM2_TIMER_TBPRD   SP // Period register
#define EPWM2_MAX_CMPA     SP-10
#define EPWM2_MIN_CMPA       10
#define EPWM2_MAX_CMPB     SP-10
#define EPWM2_MIN_CMPB       10

#define EPWM3_TIMER_TBPRD  SP  // Period register
#define EPWM3_MAX_CMPA     SP-10
#define EPWM3_MIN_CMPA       10
#define EPWM3_MAX_CMPB     SP-10
#define EPWM3_MIN_CMPB     10

#define Set_EPWM1A(Val)   EPwm1Regs.CMPA.half.CMPA = Val  //设置占空比
#define Set_EPWM1B(Val)   EPwm1Regs.CMPB = Val  //设置占空比
#define Set_EPWM2A(Val)   EPwm2Regs.CMPA.half.CMPA = Val  //设置占空比
#define Set_EPWM2B(Val)   EPwm2Regs.CMPB = Val  //设置占空比
#define Set_EPWM3A(Val)   EPwm3Regs.CMPA.half.CMPA = Val  //设置占空比
#define Set_EPWM3B(Val)   EPwm3Regs.CMPB = Val  //设置占空比

void InitEPwm123(int16 PRD);
void Epwm1_Init(int16 PRD);
void Epwm2_Init(int16 PRD);
void Epwm3_Init(int16 PRD);

#endif /* PWM_H_ */


main函数中要到的函数 InitEPwm123(SP);  //SP宏定义为自动重装值,根据不同的PWM波频率进行调整
    Set_EPWM1A(SP/4);      //设置为不同的占空比
    Set_EPWM1B(SP*7/8);
    Set_EPWM2A(SP/5);
    Set_EPWM2B(SP/4);
    Set_EPWM3A(SP/3);
    Set_EPWM3B(SP/2);