Pages: [1]   Go Down
Author Topic: Sample rate  (Read 385 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
#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){
 
 }
 
}
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

try using:
Code:
int stp = 0;
int start = micros();

micros() returns int32 values.
Logged

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

the problem continues.

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

Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

forget my previous post. the problem is that the "analogRead(A0)" command already takes 800µs. You can see this using:
Code:
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
Code:
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
« Last Edit: September 02, 2013, 03:10:38 pm by schwingkopf » Logged

Pages: [1]   Go Up
Jump to: