Skip to main content

ISR Usage (interrupt service routine) and How to use ISR

ISR Usage (interrupt service routine)


1.understand how often the interrupt occurs
2.understand how much time it takes to service each interrupt
3.Make sure there is enough time to service all interrupts and to still get work done in the main loop
there's only 1 sec of compute time per second to get everything done!

-Keep ISRs short and simple.  (short = short time, not short code length)
Do only what has to be done then RETI.
Long ISRs may preclude others from being run
Ask yourself, “Does this code really need to be here?”

-Example:  alarm clock with 1Sec interrupts:
-at interrupt, just increment the “seconds counter” and return
-in the main code loop we can
-do binary to BCD conversion 
-lookup display code
-write to display

Effecting main program flow with ISRs


ISRs are never called by the main routine.  Thus,
-nothing can be passed to them (there is no “passer”)
-they can return nothing (its not a real function call)
So, how can it effect the main program flow?
-use a global?  yech!
-use volatile type modifier

Compiler has a optimizer component (let’s digress a moment...)
-02 level optimization can optimize away some variables
-it does so when it sees that the variable cannot be changed within
           the scope of the code it is looking at
-variables changed by the ISR are outside the scope of main()
-thus, they get optimized away 

Volatile tells the compiler that the variable is shared and may be subject to outside change elsewhere.  

Comments

Popular posts from this blog

CAN INTERVIEW QUESTIONS

Qualifiers and Modifier in C

RTOS Real time operating system interview questions

CAN INTERVIEW QUESTIONS 2

What is UDS protocol

Memory mapping in c

TOP IOT PLATFORMS