AnsweredAssumed Answered

SVPWM and sinf() slow calculation.

Question asked by SProg on Jun 19, 2016
Latest reply on Jun 21, 2016 by SProg
Hello to everyone, i am building an old project with STM32F107 now.

I am trying to calculate the SVPWM values on the fly but seems like compiler's code is so long.I am running on 72MHz.

The fuction below needs around 60-65 usec,which is too much to fit on 20Khz timing.

I am using KEIL uvision 5 and 3lvl optimization.


float THETA;
int SECTOR=0;
float Ua=0;
float Ub=0;
float Uc=0;
float PI=3.14;
float T=0.0000625;  // 16kHz
float MAXMODINDEX = 2250;
volatile float n=0;
volatile float i=0;
float  FREF =50;
int   MAXn;


//--------------------------------//

void SVPWM(int count)
{
__disable_irq();     
     
     
THETA= 2*PI*FREF*count*T;
SECTOR = THETA/(PI/3);
     
Ua = (FREF/50)*sinf((PI/3)-(THETA-SECTOR*(PI/3)));
Ub = (FREF/50)*sinf(THETA-SECTOR*(PI/3));
Uc = 1.0-Ua-Ub;




switch(SECTOR)
     {
     case 0:       TIM3->CCR1 = MAXMODINDEX*(Ua + Ub + 0.5*Uc);
                   TIM3->CCR2 = MAXMODINDEX*(Ub + 0.5*Uc);
                   TIM3->CCR3 = MAXMODINDEX*(0.5*Uc);
          break;


     case 1:
                    TIM3->CCR1 = MAXMODINDEX*(Ua + 0.5*Uc);
                                   TIM3->CCR2 = MAXMODINDEX*(Ua + Ub + 0.5*Uc);
                                   TIM3->CCR3 = MAXMODINDEX*(0.5*Uc);
          break;


     case 2:       TIM3->CCR1 = MAXMODINDEX*(0.5*Uc);
                TIM3->CCR2 = MAXMODINDEX*(Ua + Ub + 0.5*Uc);
                 TIM3->CCR3 = MAXMODINDEX*(Ub + 0.5*Uc);


          break;


     case 3:       TIM3->CCR1 = MAXMODINDEX*(0.5*Uc);
                TIM3->CCR2 = MAXMODINDEX*(Ua + 0.5*Uc);
                TIM3->CCR3 = MAXMODINDEX*(Ua + Ub + 0.5*Uc);


          break;


     case 4:      TIM3->CCR1 = MAXMODINDEX*(Ub + 0.5*Uc);
               TIM3->CCR2 = MAXMODINDEX*(0.5*Uc);
               TIM3->CCR3 = MAXMODINDEX*(Ua + Ub + 0.5*Uc);


          break;


     case 5:       TIM3->CCR1 = MAXMODINDEX*(Ua + Ub + 0.5*Uc);
                TIM3->CCR2 = MAXMODINDEX*(0.5*Uc);
                TIM3->CCR3 = MAXMODINDEX*(Ua + 0.5*Uc);


          break;




     default: break;
        }
     
__enable_irq();     

}

Outcomes