Go Down

### Topic: Sample rate (Read 833 times)previous topic - next topic

#### mk74

##### Sep 02, 2013, 09:16 pm
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: [Select]
`#define ARM_MATH_CM3#include <arm_math.h> #define samples 200float32_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){ }   }`

#### schwingkopf

#1
##### Sep 02, 2013, 09:57 pm
try using:
Code: [Select]
`int stp = 0;int start = micros();`

micros() returns int32 values.

#### mk74

#2
##### Sep 02, 2013, 10:05 pm
the problem continues.

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

#### schwingkopf

#3
##### Sep 02, 2013, 10:06 pmLast Edit: Sep 02, 2013, 10:10 pm by schwingkopf Reason: 1
forget my previous post. the problem is that the "analogRead(A0)" command already takes 800µs. You can see this using:
Code: [Select]
`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: [Select]
`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

Go Up

Please enter a valid email to subscribe