Hi, this is my 1st project. I'm testing frequency aspects of both Digital and analog inputs/Outputs.
Found enough on ADC clock manipulation around (fastest readings (though values look odd) is about 2us per sample, even faster with noInterrupts() but not sure that allows analog to pick up real readings).
But wanted to check with you if there is a way to make pulses faster than this on Digital ports (2.667MHz):
void loop() {
// port toggle test, max rate
while (true) {
//delayMicroseconds(3);
// Freq 2.667Mhz
// duty cycle: 70% (because of the loop delay it's not 50%)
PORTA &= !B00000001; // Off
PORTA |= B00000001; // On sets Arduino pin 22 (Digital)
}
}
void setup() {
// initialize digital pin LED_BUILTIN as an output.
Serial.begin (115200);
Serial.println ();
ADCSRA &= ~(bit (ADPS0) | bit (ADPS1) | bit (ADPS2)); // clear prescaler bits
// uncomment as required
ADCSRA |= bit (ADPS0); // 2
// ADCSRA |= bit (ADPS1); // 4
// ADCSRA |= bit (ADPS0) | bit (ADPS1); // 8
// ADCSRA |= bit (ADPS2); // 16
// ADCSRA |= bit (ADPS0) | bit (ADPS2); // 32
// ADCSRA |= bit (ADPS1) | bit (ADPS2); // 64
// ADCSRA |= bit (ADPS0) | bit (ADPS1) | bit (ADPS2); // 128
}
// 1000 datapoints
int resultBuff[1000] = {};
// Only print once.
bool done = false;
void loop() {
// port toggle test, max rate
// while (true) {
// // Freq 2.667Mhz
// // duty cycle: 70% (because of the loop delay it's not 50%)
// PORTA = B00000001; // On
// PORTA = B00000000; // Off
// PORTA = B00000001; // On
// PORTA = B00000000; // Off
// PORTA = B00000001; // On
// PORTA = B00000000; // Off
// }
// Analog reading max rate test
if (! done) {
// Fast AnalogRead test
noInterrupts();
readSignal(false);
interrupts();
readSignal(true);
done = true;
}
// digitalWrite(healthPin, LOW);
//delayMicroseconds(60);
}
/**
* With 1000 datapoints I can describe at this sample rates:
* 500 Hz wave, 1.5 cycles. (good enouh to find max values)
* 20KHz Is the max freq I Can describe accurately.
* 30KHz Is the max freq I Can describe accurately with noInterrupts() on.
* 40KHz Still can see a signall, about 8 datapoints peak to peak with noInterrupts() on.
* Good to find max valtage in range.
*
* Clock speed: 16MHz
* About 2x gains with `noInterrupt();` call before sampling.
*/
void readSignal(bool withInterrupts) {
unsigned long startTime = micros ();
for (int i = 0; i < 1000; i++) {
resultBuff[i] = analogRead (A1);
}
unsigned long duration = micros () - startTime;
for (int i = 0; i < 1000; i++) {
Serial.println(resultBuff[i]);
}
Serial.print("Interrupts: ");
Serial.println(withInterrupts ? "true" : "false");
Serial.print("Duration: ");
Serial.println(duration);
Serial.println();
}
Something odd is Arduino seems to not read negative values, Scpe shows a nice sine wave while Excel plot just the positive peaks.