Pages: [1]   Go Down
Author Topic: possible bug in micros() or analog read?  (Read 583 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
     /*   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?
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using 1.5.2
Logged

Pages: [1]   Go Up
Jump to: