环境搭建:
- 克隆esp32 git库(速度慢可以用码云)。
- 下载并安装esp-idf-tool。
编译:
代码示例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "driver/adc.h"
#include "driver/gpio.h"
#include "freertos/queue.h"
void adc1task(void* arg)
{
// initialize ADC
adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC_CHANNEL_4, ADC_ATTEN_11db); //gpio32
adc1_config_channel_atten(ADC_CHANNEL_6, ADC_ATTEN_11db); //gpio34
int last_val_32 = 4095;
int last_val_34 = 4095;
while(1){
int new_val_34 = adc1_get_raw(ADC_CHANNEL_6);
if (new_val_34 == 0 && last_val_34 != 0) {
printf("%d\n", new_val_34);
}
last_val_34 = new_val_34;
int new_val_32 = adc1_get_raw(ADC_CHANNEL_4);
if (new_val_32 == 0 && last_val_32 != 0) {
printf("%d\n", new_val_32);
}
last_val_32 = new_val_32;
vTaskDelay(1);
// vTaskDelay(1/portTICK_PERIOD_MS);
}
}
void app_main(void)
{
xTaskCreate(adc1task, "adc1task", 1024*3, NULL, 10, NULL);
}
打开ESP-IDF 4.4 PowerShell,使用idf.py build编译cmake文件
得到烧录信息:
烧录:
将上一步得到的信息填入ESP32 DOWNLOAD TOOL,并勾选如下配置:
点击Start
ps:记得先关闭串口