新增PWM调光部分

This commit is contained in:
jarvis 2025-03-07 16:26:42 +08:00
parent dd4f44f9eb
commit 86ffd82ffe
6 changed files with 56 additions and 8 deletions

View File

@ -13,5 +13,8 @@
"board/esp32s3-builtin.cfg"
],
"idf.portWin": "COM6",
"idf.flashType": "UART"
"idf.flashType": "UART",
"files.associations": {
"ledc.h": "c"
}
}

View File

@ -25,7 +25,7 @@
"uiExportFolderPath": "D:\\yuwell_workspace\\FluxDC\\SquareLineProj\\Export",
"projectExportFolderPath": "D:\\yuwell_workspace\\FluxDC\\SquareLineProj\\Export",
"custom_variable_prefix": "uic",
"backup_cnt": 220,
"backup_cnt": 221,
"autosave_cnt": 0,
"group_color_cnt": 0,
"lvgl_version": "8.3.6",

View File

@ -7306,7 +7306,7 @@
"strtype": "SLIDER/Range",
"intarray": [
0,
100
8192
],
"InheritedType": 7
},
@ -16317,7 +16317,7 @@
"uiExportFolderPath": "D:\\yuwell_workspace\\FluxDC\\SquareLineProj\\Export",
"projectExportFolderPath": "D:\\yuwell_workspace\\FluxDC\\SquareLineProj\\Export",
"custom_variable_prefix": "uic",
"backup_cnt": 219,
"backup_cnt": 220,
"autosave_cnt": 0,
"group_color_cnt": 0,
"lvgl_version": "8.3.6",

View File

@ -4,6 +4,9 @@
// Project name: ESP32S3_UI
#include "ui.h"
#include "driver/ledc.h"
#include "esp_err.h"
#include "esp_log.h"
void ReadSdCallFunc(lv_event_t * e)
{
@ -29,9 +32,12 @@ void F3callbackFunc(lv_event_t * e)
void lightSliderCallback(lv_event_t * e)
{
// Your code here
lv_obj_t * slider = lv_event_get_target(e);
lv_slider_get_value(slider);
uint32_t value = lv_slider_get_value(e->target);
//ESP_LOGI("lightSliderCallback", "value = %u", value);
printf("value = %lu\n", value);
// Set duty to 50%
ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, value));
// Update duty to apply the new value
ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0));
}

View File

@ -16,6 +16,7 @@
#include "esp_lcd_panel_ops.h"
#include "esp_spiffs.h"
#include "driver/gpio.h"
#include "driver/ledc.h"
#include "lvgl.h"
/*导入Modbus控制头文件*/
@ -87,6 +88,41 @@ static SemaphoreHandle_t lvgl_mux = NULL;
extern void example_lvgl_demo_ui(lv_disp_t *disp);
/*利用 PWM进行背光亮度调节*/
#define LEDC_TIMER LEDC_TIMER_0
#define LEDC_MODE LEDC_LOW_SPEED_MODE
#define LEDC_OUTPUT_IO (EXAMPLE_PIN_NUM_BK_LIGHT) // 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 (4096) // Set duty to 50%. (2 ** 13) * 50% = 4096
#define LEDC_FREQUENCY (4000) // Frequency in Hertz. Set frequency at 4 kHz
static void example_ledc_init(void)
{
// Prepare and then apply the LEDC PWM timer configuration
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_MODE,
.duty_resolution = LEDC_DUTY_RES,
.timer_num = LEDC_TIMER,
.freq_hz = LEDC_FREQUENCY, // Set output frequency at 4 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));
}
static bool example_notify_lvgl_flush_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx)
{
lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx;
@ -410,6 +446,9 @@ void app_main(void)
ESP_LOGI(TAG, "Display LVGL animation");
/*测试git*/
/*配置LEDC进行背光亮度调节 */
example_ledc_init();
// Lock the mutex due to the LVGL APIs are not thread-safe
if (example_lvgl_lock(-1)) {