ADC Interrupt

Discussion in 'Embedded Systems and Microcontrollers' started by Dritech, Mar 8, 2014.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    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?
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,373
    494
    What does datasheet say?
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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.
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    What you want is a comparator...
     
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    And you could use the out put of the comparator to trigger an interrupt on change.
     
  6. gdallas

    Member

    Apr 25, 2012
    74
    0
    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
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    No 18F that I have worked with has a DAC.

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

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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?
     
  10. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    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.
     
    ErnieM likes this.
  11. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988

    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.
     
  12. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    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.
     
  13. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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.
     
  14. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    Sounds like what I wrote:


    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.
     
  15. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    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.
     
  16. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988

    And it is what I wrote way back several posts ago when you mentioned I was the one that was mistaken. This interrupt will not satisfy the OPs. requirements.
     
  17. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    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: Mar 8, 2014
  18. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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. :)
     
  19. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    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.
     
  20. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Thanks a lot for the replies.

    Can that cause problems when using serial communication (USART) ?

    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.
     
Loading...