Go Down

Topic: possible bug in micros() or analog read? (Read 655 times) previous topic - next topic

ssclark88

I have narrowed down a section of code that is giving me strange results. I am triggering an analog read inside some nested loops and getting times that are faster that what the ADC is capable of.

Code: [Select]

#define NOP __asm__ __volatile__ ("nop\n\t")

int t = 0;
int x = 0;
int y = 0;
int z = 0;

uint16_t a0[10][20][1];    //array size

void setup() {
  Serial.begin(115200);
  delay(50);
  Serial.flush();
  //Processor Instructions
  ADC->ADC_MR = 0x00000000;     
  ADC->ADC_CHER = 0x08;           //Enable channel A4;
  ADC->ADC_WPMR = 0x00;         //Disables the write protect key, WPEN
  ADC->ADC_IER |= 0x1F000008;   //enable interrupts
  ADC->ADC_CR = 2;                //begin first conversion
  delay(1);                      //wait for them to finish
  ADC->ADC_CDR[3];

}

void loop() {
  t = micros();
  for(z=0;z<1;z++){
    for(y=0;y<20;y++){
      for(x=0;x<10;x=x++){
     /*   for(int u=0;u<1;u++){                   //Uncommenting this for loop reduces the time to complete 200 conversions
             NOP;
         }*/
        ADC->ADC_CR = 2;
        while((ADC->ADC_ISR & 0x8) != 0x8){}   //wait for conversion to finish
        a0[x][y][z] = ADC->ADC_CDR[3];        //save value
      }
    }
  }
  t = micros()-t;
  Serial.print(t);Serial.print("\t"); Serial.println("Microseconds");
  delay(1000);
}


Adding a delay loop reduces the micros() timer from 201us to 172us.
Code: [Select]
     /*   for(int u=0;u<1;u++){                   //Uncommenting this for loop reduces the time to complete 200 conversions
             NOP;
         }*/


The analog data coming out seems to be valid so i'm wondering if it has something to do with the micros() function. Any Ideas?

DuaneB

There was a bug in mircos which has now been resolved, are you using an older version of the Arduino software ?

Duane B
rcarduino.blogspot.com
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com


Go Up