Quantcast
Channel: Forums - Recent Threads
Viewing all articles
Browse latest Browse all 262198

CCS/TMS320F28335: SPWM generation through EPWM interrupt

$
0
0

Part Number:TMS320F28335

Tool/software: Code Composer Studio

I am trying to generate SPWM from EPWM interrupt. At every ePWM interrupt, I am trying to increment the theta by 1.8degrees and find the value of sine and multiply that value with TBPRD to update CMPA value. After exceeding 'theta' 2*PI again i am trying to reset it. But the problem is the theta is increasing only 51 times and onwards it is not changing any value and the CMPA value is not updating. 

Please, help me in this context. Please find the code attached. 

#include "DSP28x_Project.h"     // Device Header file and Examples Include File
#include<IQmathLib.h>

void InitEPwm1Example(void);
void Gpio_setup1(void);

float PI=2.0f*3.14159f;
_iq input, sin_out;
long GlobalQ=GLOBAL_Q;
float a,b;float theta=0.0f;//float deltheta=0.31415926f;
//float deltheta=(float)(1.8f*PI/180);
float deltheta=0.031415f;
Uint16 v1[200];Uint16 count=0;
//
// Function Prototypes
//
__interrupt void ePWM1A_compare_isr(void);

//
// Main
//
void main(void)
{
    //
    // Step 1. Initialize System Control:
    // PLL, WatchDog, enable Peripheral Clocks
    // This example function is found in the DSP2833x_SysCtrl.c file.
    //
    InitSysCtrl();

    //
    // Step 2. Initialize GPIO:
    // This example function is found in the DSP2833x_Gpio.c file and
    // illustrates how to set the GPIO to it's default state.
    //
    //InitGpio();  // Skipped for this example

    //
    // Step 3. Clear all interrupts and initialize PIE vector table:
    // Disable CPU interrupts
    //
    DINT;

    //
    // Initialize the PIE control registers to their default state.
    // The default state is all PIE interrupts disabled and flags
    // are cleared.
    // This function is found in the DSP2833x_PieCtrl.c file.
    //
    InitPieCtrl();

    //
    // Disable CPU interrupts and clear all CPU interrupt flags:
    //
    IER = 0x0000;
    IFR = 0x0000;

    //
    // Initialize the PIE vector table with pointers to the shell Interrupt
    // Service Routines (ISR).
    // This will populate the entire table, even if the interrupt
    // is not used in this example.  This is useful for debug purposes.
    // The shell ISR routines are found in DSP2833x_DefaultIsr.c.
    // This function is found in DSP2833x_PieVect.c.
    //
    InitPieVectTable();

    //
    // Interrupts that are used in this example are re-mapped to
    // ISR functions found within this file.
    //
    EALLOW;         // This is needed to write to EALLOW protected registers
    PieVectTable.EPWM1_INT = &ePWM1A_compare_isr;
    EDIS;    // This is needed to disable write to EALLOW protected registers

    //
    // Step 4. Initialize the Device Peripheral. This function can be
    //         found in DSP2833x_CpuTimers.c
    //
    //InitCpuTimers();   // For this example, only initialize the Cpu Timers
/*#if (CPU_FRQ_150MHZ)
    //
    // Configure CPU-Timer 0 to interrupt every 500 milliseconds:
    // 150MHz CPU Freq, 50 millisecond Period (in uSeconds)
    //
    ConfigCpuTimer(&CpuTimer0, 150, 50);
#endif
#if (CPU_FRQ_100MHZ)
    //
    // Configure CPU-Timer 0 to interrupt every 500 milliseconds:
    // 100MHz CPU Freq, 50 millisecond Period (in uSeconds)
    //
    ConfigCpuTimer(&CpuTimer0, 100, 500000);
#endif*/
    Gpio_setup1();
    InitEPwm1Example();
    //
    // To ensure precise timing, use write-only instructions to write to the 
    // entire register. Therefore, if any of the configuration bits are changed
    // in ConfigCpuTimer and InitCpuTimers (in DSP2833x_CpuTimers.h), the
    // below settings must also be updated.
    //
    
    //
    // Use write-only instruction to set TSS bit = 0
    //
    //CpuTimer0Regs.TCR.all = 0x4000;

    //
    // Step 5. User specific code, enable interrupts
    //

    //
    // Configure GPIO32 as a GPIO output pin


    //
    // Enable CPU INT1 which is connected to CPU-Timer 0:
    //


    //
    // Enable TINT0 in the PIE: Group 1 interrupt 7
    //
    PieCtrlRegs.PIEIER3.bit.INTx1 = 1;
    IER |= 4;
    //
    // Enable global Interrupts and higher priority real-time debug events
    //
    EINT;           // Enable Global interrupt INTM
    ERTM;           // Enable Global realtime interrupt DBGM

    //
    // Step 6. IDLE loop. Just sit and loop forever (optional):
    //
    for(;;);
}


void Gpio_setup1(void){
    EALLOW;
    GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0;   // Enable pullup on GPIO0
    GpioCtrlRegs.GPAPUD.bit.GPIO1 = 0;   // Enable pullup on GPIO1
    GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;  // GPIO0 = PWM1A
    GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1;  // GPIO1 = PWM1B
    GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 0;
    GpioCtrlRegs.GPBDIR.bit.GPIO32 = 1;
    EDIS;

}
void
InitEPwm1Example()
{
    //
    // Setup TBCLK
    //
    //EPWM1 A and B are GPIO 0 and 1
    EPwm1Regs.TBCTL.bit.CTRMODE = 2; // Count up down
    EPwm1Regs.TBPRD = 3750;       // Set timer period
    EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;    // Disable phase loading
    EPwm1Regs.TBPHS.half.TBPHS = 0x0000;       // Phase is 0
    EPwm1Regs.TBCTR = 0x0000;                  // Clear counter
    EPwm1Regs.TBCTL.bit.HSPCLKDIV = 001;   // Clock ratio to SYSCLKOUT
    EPwm1Regs.TBCTL.bit.CLKDIV = 000;
    EPwm1Regs.TBCTL.bit.SYNCOSEL=1;
    EPwm1Regs.CMPA.half.CMPA =1875;    // Set compare A value
    EPwm1Regs.CMPB = 1875;              // Set Compare B value
    EPwm1Regs.DBCTL.bit.OUT_MODE=3;
    EPwm1Regs.DBCTL.bit.POLSEL=2;
    EPwm1Regs.DBCTL.bit.IN_MODE=0;
    EPwm1Regs.DBRED=10;
    EPwm1Regs.DBFED=10;
    //EPwm1Regs.PCCTL.bit.CHPEN=1;
    //EPwm1Regs.PCCTL.bit.CHPFREQ=0x111;
    //EPwm1Regs.PCCTL.bit.OSHTWTH=0x1111;
    //EPwm1Regs.PCCTL.bit.CHPDUTY=0x011;
    //
    // Set actions
    //
    EPwm1Regs.AQCTLA.bit.CAU=2;    // Set PWM1A on Zero
    EPwm1Regs.AQCTLA.bit.CAD=1;
    EPwm1Regs.AQCTLB.bit.CBU=2;
    EPwm1Regs.AQCTLB.bit.CBD=1;
    EPwm1Regs.ETSEL.bit.INTEN=1;
    EPwm1Regs.ETSEL.bit.INTSEL=4;
    EPwm1Regs.ETPS.bit.INTPRD=1;
}
__interrupt void
ePWM1A_compare_isr(void)
{
    //CpuTimer0.InterruptCount++;

    //
    // Toggle GPIO32 once per 500 milliseconds
    //
    input=_IQ24(theta);
    sin_out =_IQ24sin(input);
    a=_IQ24toF(input);
    b=_IQ24toF(sin_out);
    v1[count]=(Uint16)((b+1.0f)*0.5f*EPwm1Regs.TBPRD);
    EPwm1Regs.CMPA.half.CMPA =v1[count];    // Set compare A value
    EPwm1Regs.CMPB = v1[count];              // Set Compare B value
        // v1[count]=b;*/
    //EPwm1Regs.CMPA.half.CMPA=EPwm1Regs.TBPRD-_IQsat(_IQ28mpy(b,EPwm1Regs.TBPRD),EPwm1Regs.TBPRD,0);
   // v1[count]=b;
    count=count+1;
    theta=theta+deltheta;
    if (theta>PI){
        count=0;
        theta=0.0f;
           }
    GpioDataRegs.GPBTOGGLE.bit.GPIO32 = 1;
    EPwm1Regs.ETCLR.bit.INT=1;
    PieCtrlRegs.PIEACK.all=4;
    //
    // Acknowledge this interrupt to receive more interrupts from group 1
    //
    //PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}


Viewing all articles
Browse latest Browse all 262198

Trending Articles