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
Andrew Neil
Evangelist III
Posted on January 18, 2013 at 22:19

'' 

extern

definition !''

If it's extern, then it's a declaration

You can't have an extern definition!

michaelmccartyeng
Associate II
Posted on January 19, 2013 at 04:31

I read the whole article, and all the ones referenced. 

When I changed to using extern, the compiler did indeed find issues that it didnt before which I'm sure were causing me headaches. And my debug char works now, a simple extern declare.c define. (i actually had to edit this to change those :)
frankmeyer9
Associate II
Posted on January 19, 2013 at 13:44

You can't have an extern definition!

 

Depend on what one means.

Prepending the definition of a (global) variable with ''

extern

'' is redundant, but otherwise correct.

M0NKA
Senior
Posted on January 19, 2013 at 15:27

It seems you were right, some people take this 'software engineering' thing too seriously ;)