This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

This error makes no sense to me. It must be trivial, but I am stumped.

Hello all, I am getting a compiler error saying " error:expected a declaration". It sounds like I have not declared a variable as a type. I have declared a variable as 'int usePI = 0;' and then later in the code I have if(usePI){ several lines of code }. The if(usePI) line is where the compiler complains about a declaration, but I have declared it. I am pasting the code up to where the error happens below. I am using CCS 3.3.59.4 and the TI IDE  v 5.98.0.339, BIOS 5.32   I appreciate any help. Thanks

 

#include "DSP28x_Project.h"     // Device Headerfile and Examples Include File
#include "SineTable.h"  // Contains look-up table used in Clarke-Park transforms
#include "ClarkePark.h"  // Include header for the CLARKEPARK object
#include "PosSpeed.h"  // Header for QEP and Speed
#include "PIController.h" // Header for PI controller
#include "CLF.h"            // Header for Control Lyapunov Function Controller
#include "InvParkClarke.h"  // Header for Inverse Park Transform
#include "SpaceVectorMod.h" // Header for Space Vector Modulation
#include "PWM.h"   // Header for PWM update
const float SYSTEM_FREQUENCY = 150;   // System clock (MHz)
const float ISR_FREQUENCY = 5;    // Sample frequency (kHz)

// Configure which ePWM timer interrupts are enabled at the PIE level:
#define PWM1_INT_ENABLE  1

// Configure the period for each timer
#define PWM1_TIMER_TBPRD   0x1FFF

// Configure ADC
#define ADC_usDELAY  5000L
#define ADC_CKPS   0x1   // ADC module clock = HSPCLK/2*ADC_CKPS   = 25.0MHz/(1*2) = 12.5MHz
#define ADC_SHCLK  0xf   // S/H width in ADC module periods                        = 16 ADC clocks
#define Volt_Scale 45.77637e-6 // Scale factor for volts
#define Volt_Offset -1.5
#define Current_Scale 0.0443

// Prototype statements for functions found within this file.
interrupt void FOC_isr(void);
void InitEPwmTimer(void);

// Global variables used in this example
float CurrentPhase1;
float CurrentPhase2;
float rotorangle;
float speedRef = 1;
float IdRef = 0;
long  loopcnt = 0;
int   usePI = 0; //set to one for PI control

// Instance objects
CLARKEPARK clarkpark = CLARKEPARK_DEFAULTS;
POSSPEED qep_posspeed = POSSPEED_DEFAULTS;
if(usePI){
 PICONTROLLER pi_speed = PICONTROLLER_DEFAULTS;
 PICONTROLLER pi_Id = PICONTROLLER_DEFAULTS;
 PICONTROLLER pi_Iq = PICONTROLLER_DEFAULTS;
}
else{

 CLF clf = CLF_DEFAULTS;

}

//More code here ...

 

 

 

  • Is the if/else code within the context of a function? It should work if it is within a function.

    You can use #if/#else/#endif preprocessor conditionals in place of the if/else if you want these evaluated during preprocessing.

  • Try the option -pdv (verbose diagnostics).  That usually helps situations like this.  See slide 27 of this presentation http://tiexpressdsp.com/index.php/CGT_Tips_%26_Tricks_for_Beginners .

    Thanks and regards,

    -George

     

  • "expected a declaration" does not mean that the variable is undefined, it means that the compiler expected a declaration for a function or variable instead of an if-statement.  Most likely your code is not enclosed within a function; you can't put statements at the global scope.  Even if you were to enclose it in a function, the contents of the if would declare some variables that would never be usable elsewhere.  You probably meant to use an #ifdef preprocessor directive instead of an if-statement.  You can't refer to a declared variable in such an if statement, so you'll also have to make usePI a preprocessor macro, too.

    #define usePI 0
    #if usePI
    PICONTROLLER pi_speed = PICONTROLLER_DEFAULTS;
    PICONTROLLER pi_Id = PICONTROLLER_DEFAULTS;
    PICONTROLLER pi_Iq = PICONTROLLER_DEFAULTS;
    #else
    CLF clf = CLF_DEFAULTS;
    #endif