【ESP32-S2-Kaluga-1测评】3、ADC 按键测试
本帖最后由 justd0 于 2020-9-2 22:55 编辑<p>ESP32-S2-Kaluga-1的按键采用的ADC的设计,将六个按键分别接通不同阻值的电阻到一个io口上,</p>
<p>然后主控通过ADC读取引脚模拟量来分辨出到底按了哪个按键,</p>
<p>这种设计对IO资源有限的设备十分友好,而且多个按键按下的时候可以根据阻值计算得到相应的电压值。</p>
<p>下图是官方的原理图</p>
<p>对应的读取程序如下:</p>
<pre>
<code class="language-cpp">void led_task(void *arg)
{
double voltage = 0;
while (1) {
xQueueReceive(adc_queue, &voltage, portMAX_DELAY);
if (voltage > 2.6) {
continue;
} else if (voltage > 2.41 - DEVIATION&& voltage <= 2.41 + DEVIATION) {
ESP_LOGI(TAG, "rec(K1) -> red");
ESP_ERROR_CHECK(strip->set_pixel(strip, 0, 255, 0, 0));
ESP_ERROR_CHECK(strip->refresh(strip, 0));
} else if (voltage > 1.98 - DEVIATION && voltage <= 1.98 + DEVIATION) {
ESP_LOGI(TAG, "mode(K2) -> green");
ESP_ERROR_CHECK(strip->set_pixel(strip, 0, 0, 255, 0));
ESP_ERROR_CHECK(strip->refresh(strip, 0));
} else if (voltage > 1.65 - DEVIATION && voltage <= 1.65 + DEVIATION) {
ESP_LOGI(TAG, "play(K3) -> blue");
ESP_ERROR_CHECK(strip->set_pixel(strip, 0, 0, 0, 255));
ESP_ERROR_CHECK(strip->refresh(strip, 0));
} else if (voltage > 1.11 - DEVIATION && voltage <= 1.11 + DEVIATION) {
ESP_LOGI(TAG, "set(K4) -> yellow");
ESP_ERROR_CHECK(strip->set_pixel(strip, 0, 255, 255, 0));
ESP_ERROR_CHECK(strip->refresh(strip, 0));
} else if (voltage > 0.82 - DEVIATION && voltage <= 0.82 + DEVIATION) {
ESP_LOGI(TAG, "vol(K5) -> purple");
ESP_ERROR_CHECK(strip->set_pixel(strip, 0, 255, 0, 255));
ESP_ERROR_CHECK(strip->refresh(strip, 0));
} else if (voltage > 0.38 - DEVIATION && voltage <= 0.38 + DEVIATION) {
ESP_LOGI(TAG, "vol+(K6) -> write");
ESP_ERROR_CHECK(strip->set_pixel(strip, 0, 255, 255, 255));
ESP_ERROR_CHECK(strip->refresh(strip, 0));
}
}
vTaskDelete(NULL);
}</code></pre>
<p>下面是按键按下的效果:</p>
<p></p>
<p>ADC在旧版本IDF有问题,最新版本就没问题了</p>
页:
[1]