Delay between each ADC Conversion

Good day everyone. I faced a problem when writing my code, here is what I want my code to do:

  1. Timer Overflow interrupt start the ADC Conversion
  2. ADC Conversion starts
  3. ADC value is recorded

I was hoping each whole process to take probably less than 1ms, therefore i used prescaler of 16 for ADC conversion and 32 for timer

volatile int adc_pin = 15; // A0 = 14 (pins_arduino.h)
volatile int adc_value;
volatile bool adc_busy;
volatile bool adc_done;
volatile bool trig_conv=0;
volatile unsigned short starttime;
volatile unsigned short endtime;
volatile unsigned short trigtime;

void setup() {
  Serial.begin (115200);
  Serial.println("ADC with interrupt");
 //ADC Registers
 ADCSRA = bit(ADEN) // Turn ADC on
           | bit(ADIE) // Enable interrupt
           | bit(ADPS2); // Prescaler of 16
           //(1/(16000000/16))*13.5 = 0.0000135s=0.0135ms
 ADMUX  = bit(REFS0) // AVCC
           | ((adc_pin - 14) & 0x07); // Arduino Uno to ADC pin

 //Timer Registers
 TIMSK2 = (TIMSK2 & B11111110) | 0x01; //Enable timer overflow (last bit)
 TCCR2B = (TCCR2B & B11111000) | 0x04; //011,Divisor of 32 - (1/(16000000/32))*255 = 0.00051
 
}
ISR(TIMER2_OVF_vect){
   TCNT2=0;
   trig_conv=1;
   //TCCR2B=0;
   //TIMSK2=0;
}

// ADC complete ISR
ISR(ADC_vect) {
  adc_value = ADC;
  endtime=micros();//record the time once the ADC conversion is completed
  adc_done = true;
  adc_busy = false;
}


void loop() {
  if (adc_done)
    {
    // do something with the reading, for example, print it
    Serial.print("end :");
    Serial.println (endtime);
    adc_done = false;
    }
   // Start new conversion
    //if (!adc_busy) {
      //adc_busy = true;
     // start the conversion
     if(trig_conv){
      ADCSRA = (ADCSRA & B10111111)|0x40;//ADSC
      trig_conv=0;
      //}
      starttime=micros();
      Serial.print("start :");
      Serial.println(starttime);
    }
  /*
  if(trig_conv)
  {
    starttime=micros();
    Serial.print("time: ");
    Serial.println(starttime);
    trig_conv=0;
    //TCCR2B=0;
  }
  */
}

Here is my question: By right the codes should be running in a loop, that whenever the timer overflows, ADC conversion starts and ends and wait for the next timer overflow, but when I serial print it out, the time showed has a delay in between, do any of you know the reasons and how to solve it? here is the picture of the serial monitor

Screenshot 2022-09-02 235543

Not looked the code deeply but …Time taken to print ? Try a different baud rate to see if has any effect.

Be useful to know the project , looks like you are creating a lot of data

The value 0x04 in the lowest 3 bits of TCCR2B sets a prescaler of 64, not 32.
So your interrupt handler is probably called about every millisecond.

At 115200 baud printing 11 characters need about 1ms. For every round you print about 28 characters, so it need almost 2.5ms, that's about what you see.

Your time variables need to be unsigned long not unsigned short and you can not use them in your ISR and main code without disabling interrupts and making a copy or you risk corruption.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.