#include <driver/adc.h>
#include <driver/timer.h>
hw_timer_t *timer = NULL;
const int bufferSize = 4096;
uint16_t rawData[bufferSize]; // Buffer for ADC capture
volatile int sampleCnt = 0;
volatile int timerstop = 0;
void IRAM_ATTR onTimer() {
if (sampleCnt < 4096) {
rawData[sampleCnt++] = adc1_get_raw(ADC1_CHANNEL_0) >> 4; // Reduce 12-bit to 8-bit
} else {
timerstop = 1;
}
}
void setup() {
Serial.begin(115200);
Serial.println(" ");
// ADC Setup
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_11); // GPIO36 (VP)
// 32000 Hz
timer = timerBegin(40000000);
timerAttachInterrupt(timer, &onTimer);
timerAlarm(timer, 1250, true, 0);
}
void loop() {
static unsigned long elapse = micros();
if (timerstop) {
elapse = micros() - elapse;
timerStop(timer);
float freq = 1000000.0 * (sampleCnt + 1) / elapse;
Serial.println(freq, 2);
Serial.println(rawData[10]);
sampleCnt = 0;
timerstop = 0;
timerStart(timer);
elapse = micros();
}
}