I'm using a dsPIC30F4011 in my project with XC16, and I'm currently programming its ADC. But my question is also general:

Do I have to turn off and then back on the peripheral (ADC in this case) in every interrupt?

Here's my code:

void __attribute__((__interrupt__, auto_psv)) _ADCInterrupt(void) {
    ADCON1bits.ADON = 0;   //is this really necessary?
    IFS0bits.ADIF = 0;   //turn interrupt flag off
    //read buffers and save data
    ADCON1bits.ADON = 1;   //is this really necessary?
  • \$\begingroup\$ What led you to do that in code in the first place? \$\endgroup\$ – Matt Young Sep 18 '17 at 2:06
  • \$\begingroup\$ I don't think //do stuff is adequate. Without seeing what the "stuff" is, no one is going to be able to give you an accurate answer. \$\endgroup\$ – D Krueger Sep 18 '17 at 2:12
  • \$\begingroup\$ @MattYoung I'm using the ADC to cycle through every input read it automatically. When it finishes reading the last one, it fires this interrupt. \$\endgroup\$ – Iaka Noe Sep 18 '17 at 2:52
  • \$\begingroup\$ @DKrueger It reads and saves all buffers in an array, to be used when needed. \$\endgroup\$ – Iaka Noe Sep 18 '17 at 2:53
  • \$\begingroup\$ Let me rephrase my question. Why did you think you need disable and reenable the ADC? \$\endgroup\$ – Matt Young Sep 18 '17 at 2:55

You don't have to turn off your ADC on each interrupt, unless you want to abort the current ongoing conversion (the conversion that generated the interrupt remains on the ADCBUF - see section 20.5 of the datasheet).

However, aborting the next conversion and then restarting the ADC at the end of the interrupt would not make much sense. One could simply disable the autotrigger, and trigger the ADC at the end of the interrupt...

Instead, as you did, you must clear the interrupt flag by software, as shown in the dsPIC family reference manual (http://ww1.microchip.com/downloads/en/DeviceDoc/70046E.pdf), on section 6.4.

IFSx: Interrupt Flag Status Registers

All interrupt request flags are maintained in the IFSx registers, where ‘x’ denotes the register number. Each source of interrupt has a Status bit, which is set by the respective peripherals or external signal and is cleared via software.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.