Static variable init inside ISR - split from LDS thread

Go To Last Post
19 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
 //Here every 50ms
  static uint8_t u8_100msCounter = 0;
  
  //100ms tick
  if (u8_100msCounter)
    u8_100msCounter--;

I may be wrong here, but when will this ever run?  You reset u8_100msCounter to 0 every time the ISR fires.

 

The code is ok, "u8_100msCounter" is initialized to zero only once (during declaration).

Last Edited: Sat. Apr 15, 2017 - 07:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Visovian wrote:

 //Here every 50ms
  static uint8_t u8_100msCounter = 0;
  
  //100ms tick
  if (u8_100msCounter)
    u8_100msCounter--;

I may be wrong here, but when will this ever run?  You reset u8_100msCounter to 0 every time the ISR fires.

 

The code is ok, "u8_100msCounter" is initialized to zero only once (during declaration).


Please explain the point you are making as I am missing it. I read the code I highlighted as the variable is set to zero when the ISR is entered and then the variable is tested for ???......

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Visovian has explained it. The static variable is only initialised once, i.e. in the Data or Bss section before you get to main().

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David,
The snippet I highlighted is inside the ISR...it's the first line. If it is initialized elsewhere then why say it equals zero?

Jim

Edit: I split the thread as my question has no bearing to the original thread

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

Last Edited: Sat. Apr 15, 2017 - 07:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jim is your question really "as statics are in BSS that are guaranteed to be Initialized to 0 then why bother with =0 ?". If so then good question, I guess it's not really necessary but for the benefit of the maintainer I guess it verifies the author's understanding. 

 

EDIT: I see David made the same point here:

 

https://www.avrfreaks.net/comment...

 

(but that post is now orphaned on the end of the original thread as it missed the split) 

Last Edited: Sat. Apr 15, 2017 - 07:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David.Prentice Wrote:

You are correct. There is no need to say 0. Any static storage defaults to zero. i.e. it can go into the BSS.
.
I mentioned the initialised DATA section because strictly speaking, it is initialised.
.
Look in your K & R. I am sure that Mr Kernighan explains it clearly.
.
David.

 

But my point is if the variable, defined as it is in the ISR gets reset back to zero EVERY time the ISR fires so wouldn't it make the IF statement after useless?

JIm

 

EDIT:  I missed a point and restructured my question

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

Last Edited: Sat. Apr 15, 2017 - 08:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
But my point is if the variable, defined as it is in the ISR gets reset back to zero EVERY time the ISR fires
But your assumption is simply false. The variable is NOT reset back to zero every time.

What do you think the "static" means in that context?

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sternst wrote:
What do you think the "static" means in that context?

After reading some of Stack Overflow I see what it means, but I will say that it is a very confusing, and as one poster noted dangerous thing.

 

Ok, I sort of get it.  Will have to see if I can use it somewhere.  Thanks all

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Not confusing. Not dangerous.

The only dangerous thing here is the number of programmers who don't fully understand "static" and who don't understand the importance of when it should be used.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
the importance of when it should be used.

Go on.... WHEN?

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The general 'rule' is you want to limit the scope of variables to only where they are used. The example given is a reasonable use of static. You want the variable to 'live' between calls like a global but only visible where it is used.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So the Static variable does not behave per se, like a conventional variable if the line I am questioning is only invoked once?

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So the Static variable does not behave per se, like a conventional variable

 That depends on how you look at it.   An initialized local variable is assigned a value when it is created, which is each time the function is entered.

An initialized static local variable is also assigned a value when it is created, but because it is "static", it is created at the start of program execution.  Only the scope (name recognition) is "local" to the function.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

westfw wrote:
An initialized local variable is assigned a value when it is created, which is each time the function is entered.

To be more precise:

 

A local variable can be either static or automatic

  • An automatic variable is created each time the function is entered;
    therefore, if it has an initialiser, that is applied each time the function is entered.
     
  • A static local variable is created only once - at the start of program execution;
    therefore, if it has an initialiser, that is applied at the start of program execution.

 

Note that local variables are not restricted to just the outer block of a function - they can be defined in any blobk.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

As Russell says it's about name scope/data hiding/modularization/localization or whatever term you want to give it.

The alternative to a static in that ISR would have been a global but often with variables named things like "count" you want to have a number. Using "static" keeps this one local to use just inside the ISR. Also for the benefit of the reader she can see that the variable is only created and used inside the ISR so there's no chance its being used elsewhere.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Avoid using normal global variables in a ISR if they only are used there, declare them static in the ISR.

 

Go on.... WHEN?

If you use a global variable in a ISR you have the tendency to make them volatile (even if not needed), and that make some very slow ISR code.

If variables are declared static the compiler can do a better job optimizing the code.   

  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I think it's time to read 'Code Complete' and 'Writing solid code'

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sparrow2 wrote:
Avoid using normal global variables in a ISR if they only are used there, declare them static in the ISR.

Actually, none of this has anything specifically to do with ISRs!

 

This whole usage of locals - static or automatic - is general to any function; not just ISRs.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For reference, the original discussion started here: https://www.avrfreaks.net/comment...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sun. Apr 16, 2017 - 01:21 PM