是的,您无需等待完成即可开始 ADC 转换。不要使用analogRead
,而是查看 Nick Gammon在此处的示例,在“无阻塞读取”部分。
要实现常规采样率,您可以:
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
.