#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gptimer.h"
#include "esp_adc/adc_oneshot.h"
static TaskHandle_t adc_task_handle = NULL;
adc_oneshot_unit_handle_t adc1_handle;
static bool IRAM_ATTR timer_callback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx){
BaseType_t taskWakeup = pdFALSE;
vTaskNotifyGiveFromISR(adc_task_handle, &taskWakeup);
return (taskWakeup == pdTRUE);
}
void adc_task(void *args){
int value;
while (true) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
adc_oneshot_read(adc1_handle,ADC_CHANNEL_6,&value);
printf("light level : %d\n",value);
}
}
void app_main() {
/* ADC CONFIGURATION */
adc_oneshot_unit_init_cfg_t init_config1 = {
.unit_id = ADC_UNIT_1,
};
adc_oneshot_new_unit(&init_config1, &adc1_handle);
adc_oneshot_chan_cfg_t config = {
.bitwidth = ADC_BITWIDTH_DEFAULT,
.atten = ADC_ATTEN_DB_12,
};
adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_6, &config);
/* TIMER CONFIGURATION */
gptimer_handle_t gptimer = NULL;
gptimer_config_t timer_config = {
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
.direction = GPTIMER_COUNT_UP,
.resolution_hz = 1 * 1000 * 1000,
};
gptimer_new_timer(&timer_config, &gptimer);
gptimer_alarm_config_t alarm_config = {
.reload_count = 0,
.alarm_count = 1000000,
.flags.auto_reload_on_alarm = true,
};
gptimer_set_alarm_action(gptimer, &alarm_config);
gptimer_event_callbacks_t cbs = {
.on_alarm = timer_callback,
};
gptimer_register_event_callbacks(gptimer, &cbs, NULL);
gptimer_enable(gptimer);
gptimer_start(gptimer);
xTaskCreate(adc_task,"adc_task",2048,NULL,5,&adc_task_handle);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4