From 07ec16ee53563cf0497ce3901341db54f0b182b9 Mon Sep 17 00:00:00 2001 From: zwsd Date: Tue, 27 Sep 2022 14:42:04 +0800 Subject: [PATCH] update --- main/heating_plate.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- main/heating_plate.h | 4 +++- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/main/heating_plate.c b/main/heating_plate.c index 8234873..6c89e5c 100644 --- a/main/heating_plate.c +++ b/main/heating_plate.c @@ -10,6 +10,16 @@ */ #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 @@ -20,13 +30,38 @@ 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) {} +void T_heating_plate_start(void) +{ + T_heating_plate_set_and_update_duty(LEDC_DUTY); +} -void T_heating_plate_stop(void) {} +void T_heating_plate_stop(void) +{ + T_heating_plate_set_and_update_duty(0); +} void T_heating_plate_schedule(void) { @@ -41,4 +76,12 @@ void T_heating_plate_schedule(void) 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)); } \ No newline at end of file diff --git a/main/heating_plate.h b/main/heating_plate.h index aadd374..fbb910a 100644 --- a/main/heating_plate.h +++ b/main/heating_plate.h @@ -1,5 +1,6 @@ #pragma once #include +#include typedef struct { @@ -13,4 +14,5 @@ void T_heating_plate_init(heating_plate_structer_t *heating_plate_structer); void T_heating_plate_registered_cb(get_temp_callback_t cb); void T_heating_plate_start(void); void T_heating_plate_stop(void); -void T_heating_plate_schedule(void); \ No newline at end of file +void T_heating_plate_schedule(void); +void T_heating_plate_set_and_update_duty(uint32_t duty); \ No newline at end of file