I started writing firmware for my product and I'm a rookie here. I went through many articles about not using global variables or functions. Is there any limit for using global variables in an 8 bit system or is it a complete 'No-No'. How should I use global variables in my system or should I completely avoid them?
I would like to take valuable advice from you guys on this topic to make my firmware more compact.
Best Answer
You can use global variables successfully, as long as you keep in mind @Phil's guidelines. However, here are some nice ways to avoid their issues without making the compiled code less compact.
Use local static variables for persistent state that you only want to access inside one function.
Use a struct to keep related variables together, to make it clearer where they should be used and where not.
Use global static variables to make the variables visible only within the current C file. This prevents accidental access by code in other files due to naming conflicts.
As a final note, if you are modifying a global variable within an interrupt routine and reading it elsewhere:
volatile
.OR