extern variables doubt

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

extern variables doubt

745 Views
vinkar
Contributor III

Hello Friends,

I am writing some code for a KL04 chip. I want to define and use a global variable which can eventually be placed in a header file.

The value of that variable will  be changed/modified in a few different functions. But, I am getting no result. The definition and usage are shown below

extern volatile uint8_t commState;

// - function 1 -- //

static void runMode(uint8_t commState){ 

          uint8_t nextState;  

          volatile uint8_t commState;          

          nextState = commState;  

          dutyCycle = 10; 

           while(1){

//            tempCountValSaved = tempCount;

                 stateChange(nextState,dutyCycle);

                     commTimerFunc(tempCount);

                     while(commutationFlag == 0){

  }                       

                     commutationFlag = 0;

                     nextState--;

             if(nextState >=6) nextState = 5;

//            zeroCrossTempFlag = 0;           

           }         

        }

// -- function 2 -- //

static uint8_t blindStartUp(uint8_t commState){   
                            
                

      uint8_t slotCounts = 0;

      uint8_t nextState = 0;

      uint8_t dutyCycle;

      uint8_t rpmStartup = 0;

      uint8_t rough = 0;

      uint8_t initIteration = 0;

      uint8_t runTime = 0;

      uint8_t flagger = 0; 

      volatile uint8_t commState;                 

     

     

      nextState = commState;

      tempCount = 12000; 

      zeroCrossFlagDet = 0;  

  while(zeroCrossFlagDet != 1){   
      while(tempCount >= 1500){
     
    stateChange(nextState,dutyCycle);
          commTimerFunc(tempCount);
    while(commutationFlag == 0);
    commutationFlag = 0;
    nextState--;
       if(nextState >=6) nextState = 5;                   
   }               
   // --------------------------------------- //
      if(zeroCrossFlagDet != 1){
      
       }                   
    commutationFlag = 0;                   
    nextState--;
    if(nextState >=6) nextState = 5;
    zeroCrossTempFlag = 0;
   }
  }
  asm("nop");
                
commState = nextState;
return(zeroCrossFlagDet);
}

    

Now , in both these functions commState is being modified but there is no change.

The value what I give in the first time stays in commState. So, what exactly is the syntax of an

extern in Kinetis or rather Code Warrior.

Regards,

Vinod.

0 Kudos
1 Reply

493 Views
atm
Contributor II

You will want to modify your code as follows:

volatile uint8_t commState;

// - function 1 -- //

static void runMode() {                       // uint8_t commState){

          uint8_t nextState; 

          //volatile uint8_t commState;   // remove this line as it declares a local com state       

          nextState = commState; 

          dutyCycle = 10;

           while(1){

//            tempCountValSaved = tempCount;

                 stateChange(nextState,dutyCycle);

                     commTimerFunc(tempCount);

                     while(commutationFlag == 0){

  }                     

                     commutationFlag = 0;

                     nextState--;

             if(nextState >=6) nextState = 5;

//            zeroCrossTempFlag = 0;         

           }       

        }

// -- function 2 -- //

static uint8_t blindStartUp(){    

      uint8_t slotCounts = 0;

      uint8_t nextState = 0;

      uint8_t dutyCycle;

      uint8_t rpmStartup = 0;

      uint8_t rough = 0;

      uint8_t initIteration = 0;

      uint8_t runTime = 0;

      uint8_t flagger = 0;

      //volatile uint8_t commState;  // again you do not want to create a local variable              

   

   

      nextState = commState;

      tempCount = 12000;

      zeroCrossFlagDet = 0;

  while(zeroCrossFlagDet != 1){ 
      while(tempCount >= 1500){
    stateChange(nextState,dutyCycle);
          commTimerFunc(tempCount);
    while(commutationFlag == 0);
    commutationFlag = 0;
    nextState--;
       if(nextState >=6) nextState = 5;
   }
   // --------------------------------------- //
      if(zeroCrossFlagDet != 1){
       }
    commutationFlag = 0;
    nextState--;
    if(nextState >=6) nextState = 5;
    zeroCrossTempFlag = 0;
   }
  }
  asm("nop");
commState = nextState;
return(zeroCrossFlagDet);
}

  

Now , in both these functions commState is being modified but there is no change.

The value what I give in the first time stays in commState. So, what exactly is the syntax of an

extern in Kinetis or rather Code Warrior.

Regards,

Vinod.

Then if you need to use it in two different files, use declare the variable as:

extern volatile uint8_t commState;

This can be done in either the header file or inside the code that uses it (though putting it in the header file of the module containing the definition probably makes your code more readable).


Hope it helps!

0 Kudos