ADC Interrupt

Thread Starter

Dritech

Joined Sep 21, 2011
901
Hi all,

I am using a one current sensor to monitor the current of 3 different motors which will be operating sequentially (not at the same time). I will be using the PIC18f ADC to convert the analogue voltage to a digital value.
Now my question is: Can I include an interrupt to the ADC which will cause the program to stop executing and enter an ISR when a predefined value is exceeded?
 

spinnaker

Joined Oct 29, 2009
7,830
This thread should be in the embedded forum.

The only interrupt for analog in is for when conversion is complete. There is no threshold interrupt. You could have a timer interrupt that polls the analog in.
 

gdallas

Joined Apr 25, 2012
74
use an LTC1662, for its Vref, use the same Vcc for your AI. then before you sleep your device, write the current value of AI voltage to you external DAC (1662), plus however many of additional levels for hysteresis/windowing. then feedback that into the inverting input on comparator on your PIC, with sensor on the non-inverting input. repeat for low threshold but subtract this time. Just implemented the same thing. The 18f has an internal DAC, but its only 5 bit, wasnt good enough for my reqs, might be for yours. this could also be fedbacl to feed your internal comp ref. but will consume way more power than the 1662 listed above.

hope that helps u
 

ErnieM

Joined Apr 24, 2011
8,377
Sure you can, why not?

If the A2D upon completion can trigger an interrupt then that routine can check the value.

Your next question may be "how can I get my A2D to automatically check several inputs all by itself?" That too is possible using the Timer facilities.
 

spinnaker

Joined Oct 29, 2009
7,830
use an LTC1662, for its Vref, use the same Vcc for your AI. then before you sleep your device, write the current value of AI voltage to you external DAC (1662), plus however many of additional levels for hysteresis/windowing. then feedback that into the inverting input on comparator on your PIC, with sensor on the non-inverting input. repeat for low threshold but subtract this time. Just implemented the same thing. The 18f has an internal DAC, but its only 5 bit, wasnt good enough for my reqs, might be for yours. this could also be fedbacl to feed your internal comp ref. but will consume way more power than the 1662 listed above.

hope that helps u
No 18F that I have worked with has a DAC.

They do have a Comparator Voltage Reference if that is what you mean.
 

spinnaker

Joined Oct 29, 2009
7,830
Sure you can, why not?

If the A2D upon completion can trigger an interrupt then that routine can check the value.
I don't see how that helps. The OP wants to trigger an interrupt when a certain threshold is crossed.

Checking the threshold on trigger on completion will make things more efficient but you are still going to have to poll the analog in. Or am I the one that is missing something?
 

Brownout

Joined Jan 10, 2012
2,390
Checking the threshold on trigger on completion will make things more efficient but you are still going to have to poll the analog in. Or am I the one that is missing something?
I think you are. Upon interrupt, the ISR checks the ADC value and compares it against the target value. If comparison returns not equal ( or whatever ) then the ISR returns and program proceeds as usual. If the ISR returns equal, then a routine processes the data before returning. Nothing needs to be polled.
 

spinnaker

Joined Oct 29, 2009
7,830
I think you are. Upon interrupt, the ISR checks the ADC value and compares it against the target value. If comparison returns not equal ( or whatever ) then the ISR returns and program proceeds as usual. If the ISR returns equal, then a routine processes the data before returning. Nothing needs to be polled.

I always thought that interrupt ADC complete was triggered when the conversion is actually complete when reading the ADC. The GO_DONE bit can be polled or you can use the conversion complete interrupt.

I would like to read more about that interrupt. In every datasheet I ever read it is not very well spelled out of how to implement it. I have always just used polling of the GO_DONE bit.
 

Brownout

Joined Jan 10, 2012
2,390
I think the GO_DONE bit generates the interrupt. You reset the bit and it gets set when data is available. It can be polled, or the interrupt can be enabled. I didn't look at my code, but that's what I remember.
 

spinnaker

Joined Oct 29, 2009
7,830
I think the GO_DONE bit generates the interrupt. You reset the bit and it gets set when data is available. It can be polled, or the interrupt can be enabled. I didn't look at my code, but that's what I remember.
It does not. That interrupt needs to be enabled and is generated by the mcu itself when the conversion is complete. Yes it could be triggered when GO_DONE is set by the pic.

Either way, that interrupt will not help the OP except to make the code more efficient by eliminating polling of GO_DONE. IMHO it is a lot more simple to poll the bit.

Without an external comparator triggering an interrupt on change bit the analog input will still need to be polled.
 

Brownout

Joined Jan 10, 2012
2,390
It does not. That interrupt needs to be enabled and is generated by the mcu itself when the conversion is complete. Yes it could be triggered when GO_DONE is set by the pic.
Sounds like what I wrote:


You reset the bit and it gets set when data is available. It can be polled, or the interrupt can be enabled.
In other words, the bit can be polled, or the interrupt that gets generated when the bit goes active can be enabled, in which case, the interrupt is generated when data is available AKA when conversion is complete AKA when the GO_DONE bit is active. The advantage of using the interrupt is the processor doesn't have to waste time polling the GO_DONE bit. It might be simpler to poll, but it can be more efficient to use interrupts.
 

Brownout

Joined Jan 10, 2012
2,390
OK, my bad. The GO_DONE bit does not set the interrupt. The ADIF bit in PIR3 register does, when the ADIE bit in PIE3 register is set. However, using the interrupt means the MCU does not waste time polling the GO_DONE bit, so some efficiency may be gained.

Also, I said the GO_DONE bit is reset and checked for set, but the GO_DONE bit is actually set and checked for reset.
 

Brownout

Joined Jan 10, 2012
2,390
Sure it will. He wants the interrupt to change the program flow when he gets a threshold value. The procedure I outlined does just that. He needs only to compare values to make it happen.
 
Last edited:

THE_RB

Joined Feb 11, 2008
5,438
... Can I include an interrupt to the ADC which will cause the program to stop executing and enter an ISR when a predefined value is exceeded?
The standard way to do this is to use a timer interrupt.

The timer interrupt occurs every X uS, (or mS, whatever you choose) and in the timer inteuupt you start an ADC conversion.

The next time the timer interrupt occurs you check the ADC value that was recorded last time. Then if the ADC value exceeds the threshold you perform the task in the interrupt, or if it is a large slow task you set a flag in the interrupt, and your main() code will detect the flag and perform the task.

The result is that you have a nice simple interrupt that will detect when ADC > threshold. :)
 

Brownout

Joined Jan 10, 2012
2,390
Ah-Ha! It finally dawned on me that spinnaker might be talking about the comparators when he mentioned polling the analog channels. I looked it up, and in fact, you can set up the comparators to interrupt the processor when they get a pre-determined value. And so, the OP can do exactly what he asked, but use the comparators instead of the A/D convertor. Though, using the A/D would most likely work also.
 

Thread Starter

Dritech

Joined Sep 21, 2011
901
Thanks a lot for the replies.

The standard way to do this is to use a timer interrupt.

The timer interrupt occurs every X uS, (or mS, whatever you choose) and in the timer inteuupt you start an ADC conversion.
Can that cause problems when using serial communication (USART) ?

I looked it up, and in fact, you can set up the comparators to interrupt the processor when they get a pre-determined value. And so, the OP can do exactly what he asked, but use the comparators instead of the A/D convertor. Though, using the A/D would most likely work also.
So basically I can use the internal comparator with interrupt to detect any over-current, no matter where the code is being executed, right?

Thanks in advance.
 
Top