[资料分享] TMS320F28335生成SPWM

Aguilera   2019-7-14 08:37 楼主

/*
 * main.c
 */
#include "DSP2833x_Project.h"

void InitEPwm1Example(void);
void Gpio_Setup(void);
interrupt void epwm1_isr(void);

int N=60;
float M=0.8;
int i;

float sina[30]={0.0000, 0.1081, 0.2150, 0.3193, 0.4199, 0.5156,
                0.6052, 0.6877, 0.7622, 0.8277, 0.8835, 0.9290,
                0.9635, 0.9868, 0.9985, 0.9985, 0.9868, 0.9635,
                0.9290, 0.8835, 0.8277, 0.7622, 0.6877, 0.6052,
                0.5156, 0.4199, 0.3193, 0.2150, 0.1081, 0.0000};

float sinb[30]={0.8660, 0.8069, 0.7383, 0.6610, 0.5760, 0.4843,
                0.3868, 0.2849, 0.1796, 0.0722, -0.0361, -0.1439,
                -0.2501, -0.3533, -0.4524, -0.5462, -0.6335, -0.7135,
                -0.7850, -0.8474, -0.8998, -0.9417, -0.9726, -0.9920,
                -0.9998, -0.9959, -0.9803, -0.9533, -0.9150, -0.8660};

int main(void) {
        InitSysCtrl();  //初始化系统控制
        InitEPwm1Gpio();
        Gpio_Setup();   //子函数,gpio口设置
        DINT;           //关闭中断
        InitPieCtrl();  //初始化中断控制
        IER = 0x0000;
        IFR = 0x0000;
        InitPieVectTable();//初始化中断矢量表

        EALLOW;  // This is needed to write to EALLOW protected registers
        PieVectTable.EPWM1_INT = &epwm1_isr;
        EDIS;

        EALLOW;
        SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;  //先置0,进行ePWM配置,再置1
        EDIS;

        InitEPwm1Example();

        EALLOW;
        SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;  //看上一个注释
        EDIS;

        i=1;

        IER |= M_INT3;

        // Enable EPWM INTn in the PIE: Group 3 interrupt 1-3
        PieCtrlRegs.PIEIER3.bit.INTx1 = 1;

        // Enable global Interrupts and higher priority real-time debug events:
        EINT;   // Enable Global interrupt INTM
        ERTM;   // Enable Global realtime interrupt DBGM

        for(;;){
            __asm("          NOP");
        }

}

void Gpio_Setup(void){
    EALLOW;
    GpioCtrlRegs.GPAMUX1.bit.GPIO0=1;   //GPIO0配置为ePWM1A功能
    GpioCtrlRegs.GPAMUX1.bit.GPIO1=1;   //GPIO1配置为ePWM1B功能
    EDIS;
}

void InitEPwm1Example(void){
       // Setup TBCLK
       EPwm1Regs.TBPRD = 0x61A8;                      // 3KHz
       EPwm1Regs.TBPHS.half.TBPHS = 0x0000;           // Phase is 0
       EPwm1Regs.TBCTR = 0;                           // 时基计数器清零
       EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Count up
       EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;        // Disable phase loading
       EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1;       // Clock ratio to SYSCLKOUT
       EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1;

       // Setup shadowing
       EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
       EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
       EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;  // Load on Zero
       EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;

       // Set actions
       EPwm1Regs.AQCTLA.bit.CAU = AQ_SET;             // Set PWM1A on event A, up count
       EPwm1Regs.AQCTLA.bit.CAD = AQ_CLEAR;           // Clear PWM1A on event A, down count

       EPwm1Regs.AQCTLB.bit.CBU = AQ_SET;             // Set PWM1B on event B, up count
       EPwm1Regs.AQCTLB.bit.CBD = AQ_CLEAR;           // Clear PWM1B on event B, down count

       // Interrupt where we will change the Compare Values
       EPwm1Regs.ETSEL.bit.INTSEL = ET_CTR_PRD;      // Select INT on PRD event
       EPwm1Regs.ETSEL.bit.INTEN = 1;                 // Enable INT
       EPwm1Regs.ETPS.bit.INTPRD = ET_1ST;            // Generate INT on 1ST event

       EPwm1Regs.CMPA.half.CMPA=0x0C35;
}

interrupt void epwm1_isr(void){

    if ((i>=0)&&(i<N/2)){
        EPwm1Regs.CMPA.half.CMPA=EPwm1Regs.TBPRD*((1.0+M*sina[i])/2.0);
        EPwm1Regs.CMPB=EPwm1Regs.TBPRD*((1.0+M*sinb[i])/2.0);
    }

    if ((i>=N/2)&&(i<N)){
        EPwm1Regs.CMPA.half.CMPA=EPwm1Regs.TBPRD*((1.0-M*sina[i-30])/2.0);
        EPwm1Regs.CMPB=EPwm1Regs.TBPRD*((1.0-M*sinb[i-30])/2.0);
    }

    i++;

    if (i>=N){
        i=0;
    }

    EPwm1Regs.ETCLR.bit.INT = 1;        //写1对ETFLG[INT]状态标志位清零,从而能进行下一次中断
    PieCtrlRegs.PIEACK.all=PIEACK_GROUP3;
}

 

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复