cancel
Showing results for 
Search instead for 
Did you mean: 

my u8 becomes 0 after leaving the interrupt ?

michaelmccartyeng
Associate II
Posted on January 18, 2013 at 04:07

I automatically feel stupid asking this question, I feel as if I should know this already. I'm not sure the relation volatile, static and other keywords have with variables that you may access all over the place. 

So if I have a variable that I want to be able to read in one function and in another at what point would I want it to be volatile ? 

This is what I'm doing I have a var that is a simple char rx from the usart, then in my main i read that char and call the function. I debug and see that the char is in fact 'g' in the interrupt but when i get back around to my main its always 0x00

-- defined in some .h file global to everything

volatile static u8 debugChar; // debug char 

-- my int handler, works great ! 

void USARTx_IRQHANDLER(void)

{

  if(USART_GetITStatus(Debug_USART, USART_IT_RXNE) != RESET)

  {

  debugChar = USART_ReceiveData(Debug_USART);

printf(''\n\rDebug input command: %c\n\r'',debugChar);

  USART_ClearITPendingBit(USART2,USART_IT_RXNE);

  }

}

-- get back to main after leaving int handler and debugChar is 0

switch(debugChar)

{

case 'g':

testGPIO();

break;

}

I have tried just static, i added volatile to it in an attempt to get it to work thinking volatile was a keyword for ''things that need to change quickly'' but i'm pretty sure thats wrong. 

Thanks for reading this far

#declaration-vs-definition
13 REPLIES 13
frankmeyer9
Associate II
Posted on January 18, 2013 at 08:12

I have tried just static, i added volatile to it in an attempt to get it to work thinking volatile was a keyword for ''things that need to change quickly'' but i'm pretty sure thats wrong. 

 

Not exactly.

''volatile'' tells the compiler to leave the hands off this variable, because it may change somewhere else, being it another module, or simply hardware.

The static keyword confuses many C coders, and obviously you too.

static is overloaded, it has two meanings. Inside a function, it make auto variables persistent, keeping it's value across calls to this function. Outside of functions, it reduces the visibility of the variable to the current module, i.e. *.c file. Without static, such a variable would be visible and accessible from everywhere.

This is an advanced C textbook issue.

I guess your interrupt handler and your main are not in the same module, and thus you access two different variables of the same name, with the obvious result.

crt2
Associate II
Posted on January 18, 2013 at 08:41

why dont you use extern and point to it via library?

M0NKA
Senior
Posted on January 18, 2013 at 11:23

Hi,

You should not declare vars in .h file. Do it in a C file, then if other files need to access

as global, just re-declare in each C file as extern, but make sure if it is initialized var, to

put initialization only in original C file, not in the external declarations, otherwise the complier will create copies of it.

The best practice with any C coding - never trust the compiler, use good decompiler(IdaPro) and drag your resulting elf file to check the mess created by the

compiler - saves many hours of hair pulling.

BR, Chris

frankmeyer9
Associate II
Posted on January 18, 2013 at 12:34

You are of course correct.

If the OP has not yet fully understood the whole build process, especially concerning linkage, I suggest to consult a good C textbook. They always have a chapter on how to reference global symbols

Andrew Neil
Evangelist III
Posted on January 18, 2013 at 12:58

''You should not declare vars in .h file''

Oh yes you should!

 

You most certainly

should

  put extern declarations  for shared variables in so-called ''header'' files - what you must

not

  do is to put definitions in your headers.

http://c-faq.com/decl/decldef.html

''just re-declare in each C file as extern''

No! do not  do that! That is a maintenance nightmare! Use a header!

See also:

http://www.keil.com/forum/22134/

And:

http://bit.ly/Sg3oRM

Some resources for getting started with 'C' - including free online textbook:

http://bit.ly/X5Atml

crt2
Associate II
Posted on January 18, 2013 at 14:40

I think my vocabulary might be a bit missed. What I've meant was put extern vartype varname into .h file and put vartype varname in corresponding .c file. That way you then just include a .h file anywhere into your project and linker will automatically link the variables to same spot.

Andrew Neil
Evangelist III
Posted on January 18, 2013 at 14:47

''put extern vartype varname into .h file and put vartype varname in corresponding .c file. That way you then just include a .h file anywhere into your project and linker will automatically link the variables to same spot''

 

That's right.

In addition, be sure to include the header into the .c file which defines the variable; that way (and only that way), the compiler gets to ''see'' both the extern declaration and the definition - and can warn on any mismatch.

This is illustrated in the previously-mentioned Keil thread:

http://www.keil.com/forum/22134/

michaelmccartyeng
Associate II
Posted on January 18, 2013 at 21:00

Thanks, its clear there is a wealth of knowledge here and people are readily willing to share it. I dont know why I declared that static var, just rushing through things I guess. I now have a good understanding of what static is at the file and function scope. 

Thanks also for the bit about extern. With all of my other files I was declaring them in the file scope of the .c file and then extern in the other .c files i would use the var in. Declaring it as extern in the included header sounds like the way to go. I'll go back over my existing code and be sure its that way. 

I have one .h file called ''main.h'' which includes all of my separate module headers for each ''thing'' I have created. So whatever was in the .h , for example extern var, would be accessible to each module that might need it. I guess then you can control your private by the things that are not ''extern''. 

anyway thanks again for the help !

frankmeyer9
Associate II
Posted on January 18, 2013 at 21:22

And, as Andy pointed out, note the difference between an

extern

declaration and an

extern

definition !