Arduino Due: Interrupt + ADC+ SERIAL

Hi there,
I am looking to read the adc when there is an external interrupt.
The problem I have is that if the signal interrupt less than 100 micro seconds, the interrupt does not occur.
Is there a way to reduce this time down?

int led = 13;
int interr = 12;
unsigned long values;

void bink(){
  
  while((ADC->ADC_ISR & 0x80)==0); // wait for conversion
  values=ADC->ADC_CDR[7]; //get values
  //digitalWrite(led, HIGH);
  //delayMicroseconds(100);
  //digitalWrite(led, LOW);
  Serial.println(values);
  delayMicroseconds(100);
 
}
void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(interr, OUTPUT);//generator interrupt
  pinMode(3, INPUT);
  Serial.begin(250000);  
  ADC->ADC_MR |= 0x80;  //set free running mode on ADC
  ADC->ADC_CHER = 0x80; //enable ADC on pin A0
  attachInterrupt(3, bink, RISING);
}

// the loop routine runs over and over again forever:
void loop() {
   digitalWrite(interr, HIGH);
   delayMicroseconds(100);               
   digitalWrite(interr, LOW);
   delayMicroseconds(100);              
}

testadcinterrupt.ino.ino (878 Bytes)

Some thoughts:

1/ A Serial.println() takes several tens of us

2/ Avoid any Serial print inside an interrupt. Instead, set a Flag in the interrupt routine, test this Flag inside lopp() to Serial print (or not).

3/ You want to debounce the input interrupt signal

Hi ard_newbie,
I mean the time to jump to the function interrupt (bink()) when the arduino due has external interrupt.
I just sent data from arduino due to computer and didn't transfer from computer to arduino due so I can't have serial interrupt.

ard_newbie:
Some thoughts:

1/ A Serial.println() takes several tens of us

2/ Avoid any Serial print inside an interrupt. Instead, set a Flag in the interrupt routine, test this Flag inside lopp() to Serial print (or not).

3/ You want to debounce the input interrupt signal

Thanks you,
Ruoi

If you want to dump things to Serial, you can't do them inside your ISR.

const int led = 13;
const int interr = 12;
const int sampleSize = 100;

unsigned int values[sampleSize];
unsigned int index;
volatile bool done;

void bink() {
  if (done) return;

  while ((ADC->ADC_ISR & 0x80) == 0); // wait for conversion
  values[index++] = ADC->ADC_CDR[7]; //get values
  if (index == sampleSize) done = true;
}

void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(interr, OUTPUT);//generator interrupt
  pinMode(3, INPUT);
  Serial.begin(250000);
  ADC->ADC_MR |= 0x80;  //set free running mode on ADC
  ADC->ADC_CHER = 0x80; //enable ADC on pin A0
  index = 0;
  done = false;
  attachInterrupt(3, bink, RISING);
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(interr, HIGH);
  delayMicroseconds(100);
  digitalWrite(interr, LOW);
  delayMicroseconds(100);
  if ( done ) {
    detachInterrupt(3);
    for( int i=0; i<sampleSize; ++i ) {
      Serial.println(values[i])
    }
    index = 0;
    done = false;
    attachInterrupt(3, bink, RISING);
  }
}

Hi blh64,
Thank you for comment.
I will try it.
Best regards,
Ruoi

blh64:
If you want to dump things to Serial, you can't do them inside your ISR.

const int led = 13;

const int interr = 12;
const int sampleSize = 100;

unsigned int values[sampleSize];
unsigned int index;
volatile bool done;

void bink() {
  if (done) return;

while ((ADC->ADC_ISR & 0x80) == 0); // wait for conversion
  values[index++] = ADC->ADC_CDR[7]; //get values
  if (index == sampleSize) done = true;
}

void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(interr, OUTPUT);//generator interrupt
  pinMode(3, INPUT);
  Serial.begin(250000);
  ADC->ADC_MR |= 0x80;  //set free running mode on ADC
  ADC->ADC_CHER = 0x80; //enable ADC on pin A0
  index = 0;
  done = false;
  attachInterrupt(3, bink, RISING);
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(interr, HIGH);
  delayMicroseconds(100);
  digitalWrite(interr, LOW);
  delayMicroseconds(100);
  if ( done ) {
    detachInterrupt(3);
    for( int i=0; i<sampleSize; ++i ) {
      Serial.println(values[i])
    }
    index = 0;
    done = false;
    attachInterrupt(3, bink, RISING);
  }
}