# Does an external interrupt interrupt ADC or does ADC stall interrupt?

I'm working on an RTC application with a 1Hz clock attached to the external interrupt pin on a PIC16f887. I also have continuous ADC operations happening along with the 1Hz clock.

My question is whether the ADC or the interrupt would get priority if both occur at the same time? What if the interrupt occurs during an ADC operation? Is the operation halted to allow the interrupt to be executed? This would obviously invalidate the reading. Or does the interrupt wait for the ADC to finish?

No, the interrupt occurring has no effect on the A/D. The A/D runs from the instruction clock or its own clock, depending on how you set it up. Both these keep going during a interrupt unless you deliberatly execute a SLEEP instruction to stop the processor clock.

At most, the 1 Hz interrupt could delay the interrupt routine processing the A/D conversion done, if you are doing this by using interrupts at all. There is no requirement that A/D results be handled by using interrupts. Even if so, the A/D will perform its conversion and write the result into ADRESH:ADRESL regardless of whether the processor is taking a interrupt or not. In fact, the processor itself is not really "in" a interrupt. That's only a software abstration. When the right conditions are met for a interrupt, the processor executes a call to location 4 and turns off the GIE bit in INTCON. That's all. The rest is up to firmware.

Once the A/D has finished a conversion, the result will be available in ADRESH:ADRESL. It is up to the firmware what to do with that. The value will stay there until a new conversion is completed. If the firmware doesn't start a new conversion until reading the result of the previous, then nothing can be lost. If a new conversion is started automatically, then it is possible for the previous data to be lost if the firmware didn't get around to reading ADRESH:ADRESL in time. Look at the acquisition and conversion time of the A/D and the instruction rate, and you will see there are generally plenty of instruction cycles to grab a conversion result, even if a new conversion is started automatically by the special event trigger of a CCP module. Just make sure the clock tick interrupt code is not so long that it takes longer than it takes the A/D to make a new result. This should be easy. Any lengthy processing is best handled by setting a flag and letting the foreground code get around to it when it gets around to it. The interrupt code should handle only the immediate servicing of the device.

I didn't have much hands-on experience with the PIC family, but I can tell you what would happen in an AVR chip. I suspect they are very similar. The ADC module in your microprocessor works in parallel to your program routine. The ADC will update its reading after the conversion. Should an interrupt happen between initializing the ADC reading and updating the output, the following sequence will happen:

1. In your main() function, the ADC reading is initialized.
3. In parallel:
1. The interrupt is called
2. ADC conversion ends, the result is stored in a special register
4. The interrupt eventually ends, your processor returns to main() and reads the ADC reading result.

This isn't the most appropriate use for waveform diagrams but I hope it pictures this sequence well enough for you:

• Of course you can disable interrupts during conversion and re-enable afterwards, there is an instruction for that (on AVR). – jippie Sep 27 '12 at 18:19
• If I remember correctly, that's what happens on that PIC too. – AndrejaKo Sep 27 '12 at 18:23
• @jippie Indeed, good point! If the ADC reading is time-critical or the interrupt may take a long time, SEI() and CEI() will come in handy. – Jonny B Good Sep 27 '12 at 18:26

The PIC you use has only one interrupt, which is effectively the OR of a number of interrupt sources. When two interrupt sources occur simultaneously the effect is still that an intterupt occurs. It is up to your interrupt routine to handle AND CLEAR the interrupt sources it wants to handle. If one of the interrupt sources is still active when the interrupt routine finishes a new interrupt will occur and the interrupt routine will be called again and again, untill all (enabled) interrupt sources are cleared.

In the general situation, it's up to the designer to prioritise interupts

In your specific case, if you have something more important than the ADC is to you, then you may choose to abandon the ADC reading. But usually a 1 Hz clock update will tolerate being ignored for a variable period while the ADC completes.