Hi,
I used TMS320F28377S Launchpad (Development Kit Launchxl-F28377s).
I want to generate very simple SPWM but I do not. And I dont undersand where I do mistake. I used EPWM interrupt (5kHz)and 50Hz sin wave. this sin wave is generated in main blok only one time and saved into an array and used for compare value. I analyze pwm signals using an oscilloscope I see fluctuation in the signal. here my all code.
please help me.
thank you so much
#include "F28x_Project.h"
#include "math.h"
#define EPWM2_TIMER_TBPRD 5000 /for kHz PWM
#define PI 3.14159265358979323846
void InitEPwm2Example(void);
__interrupt void epwm2_isr(void);
unsigned int i=0,m=0,k=0;
unsigned int b[101]={0};
void main(void)
{
InitSysCtrl();
/*Here I generate sin look up table for sin values, I have 100 sample for one period 50Hz sin wave.Also I multiple buy -1 of negative values of sin wave.
Sin(w*t)=sin(2*pi*f*t)=sin(2*pi*50*(20ms/100)*sample_number)
Sample number=0,1,2,3,4,……………,100
f=50Hz
m=sample number*/
for(m=0;m<100;m++){
a=sin(PI*0.02*m);
if(sin(PI*0.02*m)<0){
a=-a;
}
b[m]=5000*a;
}
b[100]=0;
GpioCtrlRegs.GPAPUD.bit.GPIO2 = 1; // Disable pull-up on GPIO2 (EPWM2A)
GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1; // Configure GPIO2 as EPWM2A
DINT;
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.EPWM2_INT = &epwm2_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
EALLOW;
CpuSysRegs.PCLKCR0.bit.TBCLKSYNC = 0;
EDIS;
InitEPwm2Example();
EALLOW;
CpuSysRegs.PCLKCR0.bit.TBCLKSYNC = 1;
EDIS;
IER |= M_INT3;
PieCtrlRegs.PIEIER3.bit.INTx2 = 1;
CpuSysRegs.PCLKCR2.bit.EPWM2=1;
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
for(;;)
{
asm (" NOP");
}
}
// Here my interrup ISR
__interrupt void epwm2_isr(void)
{
EPwm2Regs.CMPA.bit.CMPA=b[i];
i++;
if (i==101){
i=0; }
EPwm2Regs.ETCLR.bit.INT = 1;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
}
//here epwm init codes
void InitEPwm2Example()
{
EPwm2Regs.TBPRD = EPWM2_TIMER_TBPRD; // Set timer period 801 TBCLKs
EPwm2Regs.TBPHS.bit.TBPHS = 0x0000; // Phase is 0
EPwm2Regs.TBCTR = 0x0000; // Clear counter
EPwm2Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Count up and down
EPwm2Regs.TBCTL.bit.PHSEN = TB_DISABLE; // Disable phase loading
EPwm2Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1; // Clock ratio to SYSCLKOUT
EPwm2Regs.TBCTL.bit.CLKDIV = TB_DIV2;
EPwm2Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO;
EPwm2Regs.ETSEL.bit.INTEN = 1; // Enable INT
EPwm2Regs.ETPS.bit.INTPRD = ET_1ST; // Generate INT on 1st event
}