ACELERAR FRECUENCIA DE MUESTREO

Con este tema empecé mal pero como siempre me motiva a estudiar/aprender.
He hecho algunas pruebas con mi DUE que empezaron muy mal.
puse una señal de 1KHz y me encuentro con que cada analogRead requiere 40useg.
Asi que dije.. noo algo estoy haciendo mal. Investigo a ver y encuentro un artículo que sugiere esto:
El valor por defecto del ADC Mode Register del DUE es 0x0c y eso da un tiempo de conversión de 36.6 useg.
Pero ese artítulo sugiere cambiar ese valor por 2 usando esta línea de código (perdonen no encuentro el artículo ahora).

REG_ADC_MR = (REG_ADC_MR & 0xFFF0FFFF) | 0x00020000;

Con esta linea mi tiempo de conversión bajó a 3.98useg ( aprox 250KS/seg).
Una mejora de 9 veces.
En el manual del SAM3X en la pagina 1334 dice que

Asi que me dije: no está habilitado.
El registro ADC Mode Register debe ponerse en 1 en su bit 7

REG_ADC_MR = (REG_ADC_MR & 0xFFFFFF0F) | 0x00000080; //habilita FREERUN mode

El tiempo de conversión bajó a 3.15uSeg.
Luego encontré en un comentario del foro Arduino que esto

analogReadResolution(12);

mejoraba la velocidad de sample y si lo hizo apenas.
Medí 3.12uSeg, pero lo mas sorprendente es que recuperé la resolución a 12 bits.

Pero en ese mismo tema surge que debe comentarse una linea del

/adc_disable_channel(ADC, ulChannel);

que esta en
\arduino-1.XXX\hardware\arduino\sam\cores\arduino\wiring_analog.c

Exactamente dice:

Totv:
Careful examination of the file c:\arduino-1.5.1r2\hardware\arduino\sam\cores\arduino\wiring_analog.c showed that the function analogRead() always finished with disabling of the analog channel. This is completely unnecessary, if the analog inputs are used only for streaming read from the ADC.
It was enough to comment out the line 164 where the disabling operation of the analog channel executes:

163: // Disable the corresponding channel
164: // adc_disable_channel (ADC, ulChannel);

Only one this comment dramatically increased the speed of the ADC. Now the function analogRead () holds less than 2 uS. Reading 12 channels in FOR loop now takes exactly 24 uS. This is with analogReadResolution(12). I think that for many applications this is sufficient.
However, for such a relatively powerful processor, as SAM3X8E, the low-level code inserting into Arduino will always be necessary. We need more new libraries to use all features of this processor.
Thanks all for advises.

Y esto si fue una sorpresa mayor porque el tiempo de conversión cayó a 1.79uSeg
Ya estoy en 558 Khz de sampleo.
Excelente!!! ni yo me lo imaginaba.

Pero no se que buscaba y leo que en las especificaciones del DUE dicen 12Bits 1MS/seg entonces realmente alcanza 1MSample/seg?

Decidí buscar a ver si alguien ya lo había resuelto y claro que si...
Me encuentro conque para lograrlo no usan analogRead, ya que el limite con analogRead son esos 550KS/seg

Este es el supercódigo que encontré MARAVILLOSO
El hilo se llama Speed of analogRead

Y este código garantiza 1useg de tiempo de conversión o sea 1MS/seg

unsigned long start_time;
unsigned long stop_time;
unsigned long values[1000];

void setup() {        
  Serial.begin(9600);  
  ADC->ADC_MR |= 0x80;  //set free running mode on ADC
  ADC->ADC_CHER = 0x80; //enable ADC on pin A0
}

void loop() {
  unsigned int i;
    
  start_time = micros();
  for(i=0;i<1000;i++){
    while((ADC->ADC_ISR & 0x80)==0); // wait for conversion
    values[i]=ADC->ADC_CDR[7]; //get values
  }
  stop_time = micros();

  Serial.print("Total time: ");
  Serial.println(stop_time-start_time); 
  Serial.print("Average time per conversion: ");
  Serial.println((float)(stop_time-start_time)/1000);

  Serial.println("Values: ");
  for(i=0;i<1000;i++) {
    Serial.println(values[i]);
  }  
  delay(2000);
}

Espero este ultimo código si te sirva y te motivo a que lo pruebes porque ha sido una grata sorpresa para mi esta búsqueda.

Leyendo el manual otra cosa que descubrí es que el ADC del DUE tiene un Programmable GAIN Amplifier o sea podes controlar la Ganancia de la señal con algunas limitaciones
1 2 4 para Canales individuales
0.5 1 2 2 para Canales diferenciales
No lo probé pero lo haré.

1 Like