(MCU AVR) ATmega2560 (Arduino Mega)

提高analogRead()讀取速率


參考資訊:
1. 如何加快analogRead速度提高采樣率Sampling Rate
2. ADC conversion on the Arduino (analogRead)

程式如下:

unsigned short buf[16] = {0};
int cc=0, adc[16] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15};

void setup() {
    Serial.begin(460800);
    ADCSRA |= (1 << ADPS2);
    ADCSRA &= ~(1 << ADPS1);
    ADCSRA &= ~(1 << ADPS0);
    Serial.flush();
}

void loop() {
    for(cc = 0; cc < 16; cc++){
        buf[cc] = analogRead(adc[cc]);
    }
    Serial.write((unsigned char*)buf, 16);
}

P.S. 預設Sample Rate最高只有9600Hz,ADC Prescaler改為16後,Sample Rate可以達到76.8KHz


返回上一頁