Interrupt is neither same for all hardware’s nor for the compilers. Some hardwares support multi level interrupt or nested interrupt and some chips use single interrupt flag. The Atmel architecture interrupt works as following
1. As Interrupt triggered processor finishes pending instruction
2. Stops fetching further instruction
3. Clear Global interrupt enable bit(This is why arduino denies nested interrupt, but you can use)
4. Push PC(program counter) on stack
5. Jump to interrupt vector(specific address where the corresponding interrupt handler expected to be)
6. Next is the ISR code in case of C compiler do a lot job for you and if you are using assembly all that have to do youself.
Push the status register to stack and also any other register that you may use in this ISR block
Execute the actual ISR handler code i.e. whatever you want to do when the specific interrupt occours
When the actual task is done, rollback the things you changed i.e. POP the stack to status register and other register you had pussed
7. Now last assembly code RETI which rollback the PC from stack and Reenables Global Interrupt
So in the whole process all except the step 6 and 7 are automatic user need not worry about the steps.
ISR in C
use variables with volatile type modifier other compiler optimiser may think the varible is static(assuming not changed in main())
- Keep the ISR code as short possible in execution time not in length.
- Don’t activate or deactivate interrupt inside ISR
- Don’t call another function that uses interrupt
Now the above rules are for the ideal people, you can do it if u know what the hardware and the compiler does. Suppose you have to do some of the banned things immediately after the interrupt triggered, i have to methods one is the method being ideal another is to break the rules.
A.If your main loop contains things that only run when a interrupt is triggered and interrupt are not overlapping, in this case u can set special flag to activate a part of main code (code u want to run in ISR), the flag need to set in the original ISR and cleared in main loop part of ISR.
B. Next method is really complicated. You need to mess with the hardware and the compiler, you know when interrupt occurs global interrupt enable is cleared ,PC pushed to stack by hardware and Status register and other registers moved to stack by compiler, So here
if you want to get another interrupt you have to set GIE and if interrupt occours it will again push pc and but the compiler part is complex.
Some people make multilevel interrupt with software. The main interrupt handler acts as the first interrupt handler which catches the interrupt event and stores necessary data and exit ISR(the basic isr) and start the secondary interrupt handler. exiting the fast interrupt handler triggers RETI so all interrupts resumed.