Sample rate

hello,

I am using the following code to have a sampling frequency of 10 kHz specifies. The problem is that the vector of samples (dataV[])is only filled to the 22 position.

#define ARM_MATH_CM3
#include <arm_math.h> 

#define samples 200


float32_t dataV[samples];


void setup() {
  
  
  Serial.begin(9600); 
  
  /*
  ADC:
  ADC_FREQ_MIN = 1000000
  ADC_STARTUP_FAST = 12 us
  */
  analogReadResolution(12);                                           
  adc_init(ADC, SystemCoreClock, ADC_FREQ_MIN, ADC_STARTUP_FAST);  

}

void loop() {

   float stp = 0;
   float start = micros();
  
  for (int i = 0; i < samples ; i++)
  { 
    
    if((stp-start)<= 20000)             //collect samples during 20ms
    {
      
    dataV[i] = analogRead(A0)*(3.3/4096);
    
    delayMicroseconds(100);            //wait 0.1ms 
    
    stp = micros();
    
    }

  }
 
 
 
  for(int i = 0; i < samples ; i++)
{
 
 Serial.print("["); 
 Serial.print(i);
 Serial.print("]: ");  
 Serial.println(dataV[i]);
 Serial.print("\n");
 
}

 
 while(1){
 
 } 
  
}

try using:

int stp = 0;
int start = micros();

micros() returns int32 values.

the problem continues.

Starting: dataV [22] = 0
dataV [23] = 0
...
dataV [199] = 0

forget my previous post. the problem is that the "analogRead(A0)" command already takes 800µs. You can see this using:

int t1,t2;
t1 = micros();
analogRead(A0);
t2 = micros();
Serial.println(t2-t1);

So you won't get more than 1.2kHz sampling rate when you use the "analogRead()" command. There are ways however to speed up the sampling time. You might check other forum posts or the datasheet of the SAM3X8E for that.

*update:

in fact if you remove the line

adc_init(ADC, SystemCoreClock, ADC_FREQ_MIN, ADC_STARTUP_FAST);

from your code, the analogRead() takes only 41µs. Then you only need to replace the delayMicroseconds(100) by delayMicroseconds(59) and you should get roughly a 10kHz sampling