是的,你可以開始ADC轉換而不用等待它完成。不要使用analogRead
,請查閱Nick Gammon的示例here,在「無需讀取的讀取」部分。
爲了實現一個常規的採樣率,您可以:
1)讓它在自由運行模式,它需要的樣品一樣快,因爲它可以操作,或
2)使用定時器ISR啓動ADC,或者
3)使用millis()
定期啓動轉換(一種常見的「輪詢」解決方案)。請務必通過向先前計算轉換時間加入到當前的時間步驟到下一個轉換時間,不:
uint32_t last_conversion_time;
void setup()
{
...
last_conversion_time = millis();
}
void loop()
{
if (millis() - last_conversion_time >= ADC_INTERVAL) {
<start a new conversion here>
// Assume we got here as calculated, even if there
// were small delays
last_conversion_time += ADC_INTERVAL; // not millis()+ADC_INTERVAL!
// If there are other delays in your program > ADC_INTERVAL,
// you won't get back in time, and your samples will not
// be regularly-spaced.
不管你如何週期性地開始轉換,你可以完成任何調查或附加一個ISR,當它完成時調用。
請確保將volatile
關鍵字用於ISR和loop
之間共享的變量。