Hi,
I used the code written by StephenFromNYC and modified it to try to achieve the highest ADC speed of maple mini, but failed. The code is below. My board is maple mini.
#include <wirish/wirish.h>
#include <libmaple/adc.h>
#define COMM SerialUSB // use this for Maple
// #define COMM Serial // use this for Arduino
unsigned long start=0;
unsigned long stop=0;
unsigned long counter=0;
unsigned long limit=100000;
int outPin = 18;
int val = 0;
void setup()
{
// the following line is needed for Maple
pinMode(3, INPUT_ANALOG);
pinMode(outPin, OUTPUT);
// adc_init(ADC1); //rcc_clk_enable(ADC1->clk_id), Must be the first adc command!
// adc_init(ADC2);
//
// adc_enable(ADC1); //ADC_CR2_ADON_BIT = 1
// adc_enable(ADC2);
//
// adc_calibrate(ADC1); //Optional
// adc_calibrate(ADC2);
adc_set_prescaler(ADC_PRE_PCLK2_DIV_2);
adc_set_sample_rate(ADC1, ADC_SMPR_1_5);
// the following line is needed for Arduino
// COMM.begin(57600);
}
void loop()
{
COMM.println("\nStarting loops:");
start=millis();
counter=0;
while (counter++ < limit)
{
//digitalWrite(outPin, HIGH);
val = analogRead(3);
//digitalWrite(outPin, LOW);
}
stop=millis();
COMM.println("Stop loops:");
COMM.print("Elapsed Time: ");
COMM.print(stop-start);
COMM.print(" milliseconds (for ");
COMM.print(limit);
COMM.println(" analog reads)");
COMM.println(" val = ");
COMM.println(val);
COMM.println(" PCLK2 = ");
COMM.println(PCLK2);
}
// Force init to be called *first*, i.e. before static object allocation.
// Otherwise, statically allocated objects that need libmaple may fail.
__attribute__((constructor)) void premain() {
init();
}
int main(void) {
setup();
while (true) {
loop();
}
return 0;
}
For the above code, if I apply those lines below, there will be nothing from my Serial Port monitor. So I have to comment them out. Why?
// adc_init(ADC1); //rcc_clk_enable(ADC1->clk_id), Must be the first adc command!
// adc_init(ADC2);
//
// adc_enable(ADC1); //ADC_CR2_ADON_BIT = 1
// adc_enable(ADC2);
//
// adc_calibrate(ADC1); //Optional
// adc_calibrate(ADC2);
My Serial Port monitor shows:
Starting loops:
Stop loops:
Elapsed Time: 190 milliseconds (for 100000 analog reads)
val =
2119
PCLK2 =
72000000
Starting loops:
Stop loops:
Elapsed Time: 189 milliseconds (for 100000 analog reads)
val =
2138
PCLK2 =
72000000
As you can see, the ADC read speed is around 1.9us/sample. But it could reach to 1us at most, how to achieve it then?