1
\$\begingroup\$

I have an ISR that increments a counter every 5ms if the switch is pressed.

If the counter becomes 200 then do Reset. I plan to add more functions to be called from the counter value.

After a few encounters with ISRs, I have got the expert advice from this site to keep ISR short.

Then how should I keep the ISR properly or near to perfection.

Is it about keeping flags?

I trust calling ISR that do long routines is a bad architecture. But then how can I call long routines from the value of flags in ISR if it is kept in another source files other than main.c?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Allowing access to variables between source files is done using the extern keyword, for example in header file declaring a variable. If it's all in one file you can use a global variable.

When you want an ISR to be able to change the variable (such as a flag) and have another routine recognize the change (perhaps by polling the flags periodically between doing other stuff), it's best to use the volatile keyword, which prevents the variable from being optimized out of the code (since otherwise the compiler does not see how it could change).

See: https://stackoverflow.com/questions/1433204/how-do-i-share-a-variable-between-source-files-in-c-with-extern-but-how for more info on extern

And: http://www.barrgroup.com/Embedded-Systems/How-To/C-Volatile-Keyword for more info on volatile

\$\endgroup\$
3
  • \$\begingroup\$ Thank you @Spehro Pefhany.But if it's not in same file.What should be done to make the flag change visible in the other source file. \$\endgroup\$
    – Rookie91
    Feb 26, 2014 at 3:56
  • \$\begingroup\$ Declare the variable as extern in a header file, include that header file in each source file that needs to access it. Define it in one file. That has nothing to do with ISRs, it's just C programming. stackoverflow.com/questions/1433204/… \$\endgroup\$ Feb 26, 2014 at 4:41
  • \$\begingroup\$ If it's changed in an ISR, then add the volatile keyword. barrgroup.com/Embedded-Systems/How-To/C-Volatile-Keyword \$\endgroup\$ Feb 26, 2014 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.