/** * @file heating_plate.c * @author Finny (tianjialong0106@163.com) * @brief * @version 0.1 * @date 2022-09-26 * * @copyright Copyright (c) 2022 * */ #include "heating_plate.h" #include "driver/ledc.h" #include "esp_err.h" #define LEDC_TIMER LEDC_TIMER_0 // #define LEDC_MODE LEDC_LOW_SPEED_MODE // #define LEDC_OUTPUT_IO (20) // Define the output GPIO #define LEDC_CHANNEL LEDC_CHANNEL_0 // #define LEDC_DUTY_RES LEDC_TIMER_13_BIT // Set duty resolution to 13 bits #define LEDC_DUTY (4095) // Set duty to 50%. ((2 ** 13) - 1) * 50% = 4095 #define LEDC_FREQUENCY (5000) // Frequency in Hertz. Set frequency at 5 kHz #define heating_plate_target_temp 48.0 #define heating_plate_variable_temperature_range 0.5 static heating_plate_structer_t *heating_plate_structer_s; static get_temp_callback_t get_temp_cb_s; void T_heating_plate_init(heating_plate_structer_t *heating_plate_structer) { heating_plate_structer_s = heating_plate_structer; // Prepare and then apply the LEDC PWM timer configuration ledc_timer_config_t ledc_timer = { .speed_mode = LEDC_MODE, .timer_num = LEDC_TIMER, .duty_resolution = LEDC_DUTY_RES, .freq_hz = LEDC_FREQUENCY, // Set output frequency at 5 kHz .clk_cfg = LEDC_AUTO_CLK}; ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer)); // Prepare and then apply the LEDC PWM channel configuration ledc_channel_config_t ledc_channel = { .speed_mode = LEDC_MODE, .channel = LEDC_CHANNEL, .timer_sel = LEDC_TIMER, .intr_type = LEDC_INTR_DISABLE, .gpio_num = LEDC_OUTPUT_IO, .duty = 0, // Set duty to 0% .hpoint = 0}; ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel)); } void T_heating_plate_registered_cb(get_temp_callback_t cb) { get_temp_cb_s = cb; } void T_heating_plate_start(void) { T_heating_plate_set_and_update_duty(LEDC_DUTY); } void T_heating_plate_stop(void) { T_heating_plate_set_and_update_duty(0); } void T_heating_plate_schedule(void) { if (heating_plate_structer_s->heating_plate_preheat_start_flag) { if (get_temp_cb_s() >= heating_plate_target_temp + heating_plate_variable_temperature_range) { T_heating_plate_stop(); } else if (get_temp_cb_s() <= heating_plate_target_temp - heating_plate_variable_temperature_range) { T_heating_plate_start(); } } } void T_heating_plate_set_and_update_duty(uint32_t duty) { // Set duty ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, duty)); // Update duty to apply the new value ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL)); }