extern variable doubt

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

extern variable doubt

Jump to solution
910 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.

Labels (1)
Tags (2)
0 Kudos
1 Solution
4 Replies
683 Views
BlackNight
NXP Employee
NXP Employee

Hi Vinod,

this is more of C programming question than a CW vs Kinetis one.

You only can mark *global* variables with extern, not local ones.

You need commState placed *outside* the function(s):

Erich

0 Kudos
683 Views
vinkar
Contributor III

Hello Eric,

Thank you very much indeed.

I tried that. But without success. If my program memory is full code warrior will warn me, right.

Also, when I compile, how can I find the sie of the hex file or rather how much memory space my code will occupy.

Vinod

0 Kudos
683 Views
scottm
Senior Contributor II

Hi Vinod,

Can you post your modified version?  If you declare volatile uint8_t commState global in the first file, and extern volatile uint8_t commState in the other or in a shared header (but NOT inside a function) then it should work fine.

Assuming your linker file is set up properly, then yes, the linker will warn you if you're out of space.  You can look through the map file to check.

Your functions seem to have commState declared both as a parameter and as a local.  It ought to be something like this:

header.h

-----------

extern volatile uint8_t commState;

module1.c

-----------

#include "header.h"

volatile uint8_t commState;

void func1(void)

{

     commState = 1;

}

module2.c

-----------

#include "header.h"

void func2(void)

{

     commState = 2;

}

So commState is a volatile uint8_t declared global in module1.c, and module2.c can access it because it has the header with the extern.  module1.c doesn't have to have the header, but I do it that way for consistency.

(And Erich, I'm happy to answer this one and a couple of others for you if you'll go take a look at my HCS08 linker deadstripping question!)

Scott

0 Kudos