This article explains interrupt priorities by examples using CAPCOM2 module.
Xc164cm ic includes a 16 channel module named CAPCOM2. Each channel works exactly with the same way as the channels of CAPCOM6 module. It includes a compare-mutch register etc (see Capcom6 example).
Capcom2 interacts with timers T7 and T8. Here we use timer T8 and channel16, channel17 and channel18.
We will make software interrupts (it is easier) and not hardware interrupts (for example using an external signal).
Time that the interrupts are occured: It is the order that the interrupts are occured. Interrupts may be occured at different times or may be occured at the same time.
a) Interrupts are occured at different times. We set at compare-match registers of the channels: channel16=0x500, channel17=0x1000, channel18=0x1500. So, the order of occurance of interrupts is always :
1)channel16 2)channel17 3)channel18
b) Interrupts are occured at the same time. We set at compare-match registers of the
channels: channel16=0x500, channel17=0x500, channel18=0x500.
Priority order of Interrupts. It is set by the programmer. It means that when an interrupt code is runing and durring this time a higher priority interrupt is appeared, then , the microcontroller stops servicing for the moment the lower priority order interrupt, gives attention to the higher priority order interrupt whose code is fully executed and ,after it, comes back to the lower order interrupt at the command that he stoped serviced it. But if the next appeared interrupt is a lower priority interrupt then, the higher priority interrupt code is fully executed and after it, the microcontroller gives attention to the lower priority interrupt code.
watch some examples here. Becarefull! Continue Reading the Explanation.
The C code for examples example3, example4 and example5 is here .
You will find the interrupt code in CC2.C file.
At the examples the interrupts seems to be visible in agreement with their priopity levels. It is ought to time-delay routines inside the code- body of interrupts. Interrupts are serviced by the microcontroller in agreement with the order that they appeared in conjuction with the priority level that they were set,
but they are vissible to us at a different order. The explanation is here. Following the black arrows you will find the real order that interrupts are serviced and this order drives to the order that the interrupts are visible to us marked by red arrows. Interrupts are vissible to us only when the commands : P1L_0=1; P1L_1=1; P1L_2=1; are executed.
To confirm the above black -arrow diagram you can set breakpoints inside the code-body of interrupts and doing precise "run and stop " using simulation only! Good luck.
To watch the order that the interrupts of channel16, channel17 and channel18 are visible (and not serviced) we use the three led that are connected to parallel port PIL on the board.
P1L_0 led for channel16 P1L_1 led for channel17 P1L_2 led for channel18
The initial state of these three led are ON and then they are going OFF following the order that the interrupts are visible. We included and time-delay routines in order to the state of the led be visible.
EXAMPLE 5 for the simulator is here. We set three breakpoints at the interrupt C code at CC2.C file. Open the parallel port P1L window and run again and again. Wait for some time and watch the order that the interrupts are vissible marked with a 'v' at the corresponding pin.
...but the breakpoints are set to wrong places. We lost some delay instructions that were executed. Set breakpoints inside the body-code.
Exercise : Open the examples by DAVE, watch the selections of CAPCOM2 module and play changing the visible order of interrupts doing " drag and drop" .Set breakpoint inside the interrupt's code-body and watch the real order that they are serviced using simulator only !