stimmer:
It's similar. Basically in free running mode the SAM3X cycles through all enabled channels. So we just enable 2 channels and wait for 2 conversions each time:void setup() {
Serial.begin(9600);
int t=analogRead(0);
ADC->ADC_MR |= 0x80; // these lines set free running mode on adc 7 and adc 6 (pin A0 and A1 - see Due Pinout Diagram thread)
ADC->ADC_CR=2;
ADC->ADC_CHER=0xC0; // this is (1<<7) | (1<<6) for adc 7 and adc 6
}
void loop() {
int t=micros();
int q0=0,q1=0;
int a0,a1;
for(int i=0;i<500000;i++){
while((ADC->ADC_ISR & 0x80)==0);
while((ADC->ADC_ISR & 0x80)==0);// wait for two conversions
a0=ADC->ADC_CDR[7]; // read data on A0 pin
a1=ADC->ADC_CDR[6]; // read data on A1 pin
q0+=a0;
q1+=a1;
}
t=micros()-t;
Serial.print("500000 pairs of conversions in ");Serial.print(t);Serial.println(" micros");
Serial.print("A0 total:");Serial.println(q0);
Serial.print("A1 total:");Serial.println(q1);
}
Obviously there's only one ADC which is shared by the 2 channels so the effective sample rate halves. This means that you will lose some accuracy, because internally the chip is having to switch between two different input signals 1 million times per second, and that won't be as good as it tracking the same input all the time like in the 1-channel example.
This is also working.
But I have some doubts about it:
- Why on the
while((ADC->ADC_ISR & 0x80)==0);
while((ADC->ADC_ISR & 0x80)==0);// wait for two conversions
line, you use the same pin of the ADC to compare?
Wouldn't it be:
while((ADC->ADC_ISR & 0x80)==0);
while((ADC->ADC_ISR & 0x40)==0);// wait for two conversions
or better:
while((ADC->ADC_ISR & 0xC0)==0);// wait for two conversions (pin A0[7] and A1[6])
??
On the other hand, to reduce Jitter and free more time for "processing" in the loop function, could it enabling ADC interrupts be a solution?
Do you know how to accomplish this? I have seen ADC_IER register in the datasheet, but I'm pretty new with Arduino Due (just a couple of days) and don't know how to start.
Thank you again