#include <stdio.h>
#include <driver/gpio.h>
#include <driver/adc.h>
#include <esp_log.h>
#include <math.h>
#include "driver/ledc.h"
#include "esp_adc_cal.h"
#include <stdint.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_timer.h"
#define CP_PWM_PIN 18
#define READ_PWD_SIGNAL ADC1_CHANNEL_5 // Pin 33
void set_cp_pwm_duty(uint32_t duty) {
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, duty);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
}
void pwd() {
// int current = 22; // Example current value
// int duty_cycle = current / 0.6; // duty cycle
// // 1024 = 10-bit resolution
// // 1024 * (duty_cycle / 100) gives the duty cycle in bits
// int duty_cycle_res = 1024 * (duty_cycle / 100.0);
int x = 0 ;
while (x<4095){
set_cp_pwm_duty(x);
vTaskDelay(pdMS_TO_TICKS(50));
x=x+100;
}
printf("duty_cycle_res: %d\n", x);
}
void configure_adc() {
adc1_config_width(ADC_WIDTH_BIT_11);
adc1_config_channel_atten(READ_PWD_SIGNAL, ADC_ATTEN_DB_11);
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_HIGH_SPEED_MODE,
.timer_num = LEDC_TIMER_0,
.duty_resolution = LEDC_TIMER_12_BIT,
.freq_hz = 500,
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&ledc_timer);
ledc_channel_config_t ledc_channel = {
.speed_mode = LEDC_HIGH_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = CP_PWM_PIN,
.duty = 0,
.hpoint = 0
};
ledc_channel_config(&ledc_channel);
}
void app_main(void) {
configure_adc();
pwd();
}