Interrupt the ADC - Page 1
Forum section: electronics / general

author: PerranOak
author_rank: Frequent Contributor
timestamp: June 11, 2018, 06:02:59 pm
content: If an ADC is in progress when an interrupt (Timer1) occurs, will the conversion be interrupted? This is for a PIC 16F1827.If so, to where does the interrupt return? Into the middle of the ADC?

author: BocaDev
author_rank: Contributor
timestamp: June 11, 2018, 06:28:17 pm
content: Quote from: PerranOak on June 11, 2018, 06:02:59 pmInteresting, I was wondering that myself except for the 8051 devices (I know, I know, are 8051 devices still around?)

author: NorthGuy
author_rank: Super Contributor
timestamp: June 11, 2018, 06:33:57 pm
content: No. The conversion is done by the ADC module. Interrupt only affects the CPU.After the interrupt, the CPU will return to where it was before the interrupt has happened. If CPU was waiting for ADC, it'll continue waiting for ADC after the interrupt. Most likely the conversion will finish while CPU executes the interrupt.

author: Ian.M
author_rank: Super Contributor
timestamp: June 11, 2018, 06:35:30 pm
content: The ADC of all 8 bit PIC families is a hardware module. You set it up by configuring various bits in a couple of registers and enable it with the ADON control flag. Then to perform a conversion you select an input channel to convert, wait briefly for the input to stabilise after switching input channels(or if converting a single channel, you can initialise it with that channel selected and skip further channel selection and the brief wait), then trigger the conversion by setting the Go_/Done control flag. The conversion happens autonomously, and its completion and the presence of an updated result in the ADRESH:ADRESL register pair is signalled by it clearing the Go_/Done control flag and setting the ADIF interrupt flag.Except under certain special circumstances involving the FRC conversion clock and SLEEP mode, it doesn't matter what code the CPU core is running while the ADC is converting. This includes going off to service your Timer 1 interrupt. As an ADC conversion takes at most a few uS, the odds are the conversion will complete while the Timer 1 ISR is running, but the usual ADC code that loops polling the Go_/Done flag wont even notice - it will just see conversion complete and carry on to fetch the result.By using a CCP module in compare: special event mode, the ADC module can run almost entirely autonomously, performing an ADC conversion at a predetermined time without further CPU core intervention. However you do then need an ADC ISR to handle fetching the result and setting the channel and time for the next conversion.

author: 0xFFF0
author_rank: Regular Contributor
timestamp: June 11, 2018, 08:33:56 pm
content: Is there an automatic conversion loop mode? Or do I always have to trigger the GO again and again?

author: Ian.M
author_rank: Super Contributor
timestamp: June 11, 2018, 08:59:30 pm
content: 8 bit PIC CCP compare: special event mode resets the 16 bit timer that CCP module is using, so, assuming you initially started the timer from zero, it will continue to start ADC conversions at repeats of the same interval without further intervention. However there is no DMA hardware to get the result in ADRESH:ADRESL into an array in RAM so you need an ADC ISR with code to copy the result to RAM.

author: 0xFFF0
author_rank: Regular Contributor
timestamp: June 11, 2018, 09:52:24 pm
content: No expierience with that. Does it work with a timer interrupt too? A timer triggers, let's say every 50us, a GO. ADC-isr writes ADRESL into RAM.

author: Ian.M
author_rank: Super Contributor
timestamp: June 11, 2018, 11:34:59 pm
content: You can do it that way, with two ISRs, but the ISR overhead is fairly high relative to the conversion time, so it makes more sense to use a CCP module to trigger the ADC and reset the timer. Alternatively, especially if you are programming in C so have more entry/exit overhead in the ISR, it can be preferable to do a normal software driven complete ADC conversion in the timer ISR.