关于ESP32的PWM外设说明请见此处详细说明:点击打开链接
这里我就直接贴出示例代码
bl_control.h
#ifndef __BL_CONTROL_H__
#define __BL_CONTROL_H__
#ifdef __cplusplus
extern "C" {
#endif
void BLC_Init(); //初始化
void BLC_Set_Duty(unsigned char duty); //设置占空比0~256
#ifdef __cplusplus
} /*extern "C"*/
#endif
#endif
bl_control.c
#include "bl_control.h"
#include "driver/ledc.h"
#define TFT_BL 15 //引脚定义
void BLC_Init()
{
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_8_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = 5000, // Set output frequency at 5 kHz
.clk_cfg = LEDC_USE_APB_CLK
};
ledc_channel_config_t ledc_channel = {
.gpio_num = TFT_BL, //背光引脚
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = LEDC_TIMER_0,
.duty = 100, // Set duty to 100%
.hpoint = 0
};
ledc_timer_config(&ledc_timer);
ledc_channel_config(&ledc_channel);
}
void BLC_Set_Duty(unsigned char duty)
{
ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, duty);
ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
}