Hi all,
I am generating the Famous SPWM with the TMS320F2802x as shown by the picture.
My code is based on a simple way.....
I have a lookup table in the code that contain 200 values from 0 to 200 and my TBPRD value for frequency is 200 (that gives 300kHz)
I use the interrupt routine as seen by the code and I a
const int sineLookup[] = {
0,
0,
0,
0,
0,
1,
1,
2,
2,
3,
4,
5,
6,
7,
8,
10,
11,
12, blabla
200,
10,
8,
7,
6,
5,
4,
3,
2,
2,
1,
1,
0,
0,
};
frequency= 200; // define the TBPRD value for frequency 300kHz
interrupt void epwm1_isr(void)
{
if(i==200)
{
i=0;
}
if(k==400)
{
k=0;
}
if(k<=199)
{
EPwm1Regs.CMPA.half.CMPA=sineLookup[i];
EPwm1Regs.CMPB=frequency;
}
else
{
EPwm1Regs.CMPA.half.CMPA=0;
EPwm1Regs.CMPB=0;
}
i++;
k++;
// Clear INT flag for this timer
EPwm1Regs.ETCLR.bit.INT = 1;
// Acknowledge this interrupt to receive more interrupts from group 3
PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
}
void InitEPwm1Example()
{
CLK_enablePwmClock(myClk, PWM_Number_1);
EPwm1Regs.TBPRD=frequency; // Timer set for frequency (1/Tsw1....kHz)
EPwm1Regs.TBPHS.half.TBPHS = 0; // Phase is 0
// Setup TBCLK
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Count up down
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE; // Master module
EPwm1Regs.TBCTL.bit.PRDLD = TB_SHADOW;
EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_IN; // Sync flow through
EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; // Load on CTR=Zero
EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; // Load on CTR=Zero
EPwm1Regs.TBCTL.bit.HSPCLKDIV=TB_DIV1;
EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1; // Slow just to observe on the scope
EPwm1Regs.AQCTLA.bit.CAU = AQ_CLEAR; // Set PWM2A on Zero
EPwm1Regs.AQCTLA.bit.CAD = AQ_SET;
EPwm1Regs.AQCTLB.bit.CBU = AQ_CLEAR; // Set PWM2B on Zero
EPwm1Regs.AQCTLB.bit.CBD = AQ_SET;
// Interrupt where the sine wave will be updated
EPwm1Regs.ETSEL.bit.INTSEL = ET_CTR_PRD; // Select INT on Zero event
EPwm1Regs.ETSEL.bit.INTEN = 1; // Enable INT
EPwm1Regs.ETPS.bit.INTPRD = ET_1ST; // Generate INT on 1st event
}
Now the question....
For some reason the iteration number i and k (here 200 and 400) are directly related to the period Tsw2.
I try now to go from 1kHz to 10kHz and keeping the 200values per half period. But I can't. I HAVE TO use 20 and 40 iterations instead and
the TBPRD can go up to 6.
How can I keep the 400 values and go up to Tsw2=10kHz.
THere must be a way to make a better code to overcome that issue since the Microcontroller can do it.