I’ve set an ISR to calculate the sine value for every index of the table up to 200 (the length of the array), then the calculated value is offset by multiplying half the period register then adding another half. After that, the final value is assigned to the duty cycle register (multiplied by 2 because the period register is 15-bit and the duty cycle register is 16-bit).
How can I calculate the sine wave and change it’s frequency without the “index/resolution” part? Basically, I want to get rid of that part and use the classic sinewave formula: Ampsin(wt), where w = 2pif.
Here’s the code I have so far:
int sineWave[SPWM_RES]; // Sine wave lookup table
int sineWave180[SPWM_RES]; // Sine wave lookup table
volatile int sineIndex = 0; // Sine wave index
float Amp_coef = 0;
void __attribute__((__interrupt__, no_auto_psv)) _T3Interrupt(void) //This is the Timer3 interrupt function
{
IFS0bits.T3IF = 0; // Clear Timer3 interrupt flag
float sine = Amp_coef * sin(2 * M_PI * sineIndex / SPWM_RES);
sineWave[sineIndex] = (int)((P1TPER/2) * sine + P1TPER/2);
sineWave180[sineIndex] = (int)((P1TPER/2) * (-sine) + P1TPER/2);
P1DC1 = sineWave[sineIndex] * 2; // Set duty cycle from lookup table
P1DC2 = sineWave180[sineIndex] * 2;
if(SPWM_RES == sineIndex)
{
sineIndex = 0;
}
else
{
++sineIndex;
}
//sineIndex = (++sineIndex) % SPWM_RES;
}
I want to get rid of array index part and change the frequency, but don’t quite get it.
Nobody Else is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.