ESP32从零开始系列,
适合没有基础的各位工程师,
愿作为钥匙替各位打开ESP32开发的大门。
ESP-IDF中PWM控制LED灯的例子位于 esp-idf/examples/peripherals/ledc,通过命令复制到 Eclipse 的 workspace。
cp -rf ~/esp/esp-idf/examples/peripherals/ledc ~/eclipse/workspace/
导入方法与上一篇 ESP32从零开始系列之环境搭建 中的第三部分,这里不再复述。
记得执行命令 make menuconfig 先配置一次。
配置好后可以看到代码一片片飘红,这是因为项目找不到头文件导致的,因此添加一下依赖的头文件。
选择C/C++General的子项Paths and Symbols,在Includes页面选择GNU C,然后通过Add…按钮添加头文件路径,具体添加哪些路径请参考下图。
实际上我是随便加的 ╮( ̄▽  ̄)╭
添加好后点击按钮Apply and Close,就可以看到代码文件里的一片片的红色消失啦。
分析源码,其实我一看大概就明白了,其实我并不想分析源码, 注释非常详细。
以下为个人理解,如有错误,欢迎指出。
/*
* Prepare and set configuration of timers
* that will be used by LED Controller
*/
ledc_timer_config_t ledc_timer = {
.duty_resolution = LEDC_TIMER_13_BIT, // resolution of PWM duty 占空比精度
.freq_hz = 5000, // frequency of PWM signal 频率
.speed_mode = LEDC_HS_MODE, // timer mode 高速模式
.timer_num = LEDC_HS_TIMER, // timer index timer0
.clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock 自动选择timer时钟
};
// Set configuration of timer0 for high speed channels 配置timer0
ledc_timer_config(&ledc_timer);
// Prepare and set configuration of timer1 for low speed channels
ledc_timer.speed_mode = LEDC_LS_MODE; 低速模式
ledc_timer.timer_num = LEDC_LS_TIMER; timer1
ledc_timer_config(&ledc_timer); 配置timer1
以上这段代码主要设置了timer0和timer1的一些参数,其中timer0是高速模式,timer1是低速模式,低速跟高速有什么区别暂时不管。
/*
* Prepare individual configuration
* for each channel of LED Controller
* by selecting:
* - controller's channel number
* - output duty cycle, set initially to 0
* - GPIO number where LED is connected to
* - speed mode, either high or low
* - timer servicing selected channel
* Note: if different channels use one timer,
* then frequency and bit_num of these channels
* will be the same
*/
ledc_channel_config_t ledc_channel[LEDC_TEST_CH_NUM] = {
{
.channel = LEDC_HS_CH0_CHANNEL,
.duty = 0,
.gpio_num = LEDC_HS_CH0_GPIO,
.speed_mode = LEDC_HS_MODE,
.hpoint = 0,
.timer_sel = LEDC_HS_TIMER
},
{
.channel = LEDC_HS_CH1_CHANNEL,
.duty = 0,
.gpio_num = LEDC_HS_CH1_GPIO,
.speed_mode = LEDC_HS_MODE,
.hpoint = 0,
.timer_sel = LEDC_HS_TIMER
},
{
.channel = LEDC_LS_CH2_CHANNEL,
.duty = 0,
.gpio_num = LEDC_LS_CH2_GPIO,
.speed_mode = LEDC_LS_MODE,
.hpoint = 0,
.timer_sel = LEDC_LS_TIMER
},
{
.channel = LEDC_LS_CH3_CHANNEL,
.duty = 0,
.gpio_num = LEDC_LS_CH3_GPIO,
.speed_mode = LEDC_LS_MODE,
.hpoint = 0,
.timer_sel = LEDC_LS_TIMER
},
};
// Set LED Controller with previously prepared configuration
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
ledc_channel_config(&ledc_channel[ch]);
}
注释真的很详细,个人简单理解就是配置了四个PWM输出口。
// Initialize fade service.
ledc_fade_func_install(0);
遇到不明白的暂且真的不需要过于纠结这里做了什么,还是简单理解为ledc模块不可缺少的前置步骤即可。
while (1) {
printf("1. LEDC fade up to duty = %d\n", LEDC_TEST_DUTY);
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) { //令四个channel的占空比逐渐升高
ledc_set_fade_with_time(ledc_channel[ch].speed_mode, //显示出来的实际效果是灯逐渐变暗
ledc_channel[ch].channel, LEDC_TEST_DUTY, LEDC_TEST_FADE_TIME); //并且效果是渐变的
ledc_fade_start(ledc_channel[ch].speed_mode,
ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
}
vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);
//可以注意到这里有一个延迟,就是说我们设置了占空比后
//它可能是在另一个线程去做占空比逐渐变化这件事,主线程可以继续做其他工作
printf("2. LEDC fade down to duty = 0\n");
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) { //此处则是令四个channel的占空比逐渐降低
ledc_set_fade_with_time(ledc_channel[ch].speed_mode, //实物的效果则是灯逐渐变亮
ledc_channel[ch].channel, 0, LEDC_TEST_FADE_TIME);
ledc_fade_start(ledc_channel[ch].speed_mode,
ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
}
vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);
printf("3. LEDC set duty = %d without fade\n", LEDC_TEST_DUTY);
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) { //令四个channel的占空比立刻升高,实物效果是灯立刻变暗
ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, LEDC_TEST_DUTY);
ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
printf("4. LEDC set duty = 0 without fade\n");
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) { //令四个channel的占空比立刻降低,实物效果是灯立刻变亮
ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, 0);
ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
在这里我们做一个令灯颜色不断变化的效果,需要用到RGB全彩LED灯。
RGB全彩灯原理是控制RGB三个IO不同的PWM实现任意颜色,类似三原色可以混出各种颜色,只不过在软件上的体现是三个不同占空比的PWM可以混出各种颜色。
#include
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/ledc.h"
#include "esp_err.h"
#define LEDC_HS_TIMER LEDC_TIMER_0 //只使用一个timer就可以了,因为使用同样的频率
#define LEDC_HS_MODE LEDC_HIGH_SPEED_MODE //高速模式
#define LEDC_HS_CH0_GPIO (18) //IO口18
#define LEDC_HS_CH0_CHANNEL LEDC_CHANNEL_0
#define LEDC_HS_CH1_GPIO (19) //IO口19
#define LEDC_HS_CH1_CHANNEL LEDC_CHANNEL_1
#define LEDC_HS_CH2_GPIO (5) //IO口5
#define LEDC_HS_CH2_CHANNEL LEDC_CHANNEL_2
#define LEDC_TEST_CH_NUM (3) //3个IO,3个channel
#define LEDC_TEST_DUTY (8000) //8000频率
#define LEDC_TEST_FADE_TIME (3000) //逐渐变化的时间
void app_main(void)
{
int ch;
//配置timer
ledc_timer_config_t ledc_timer = {
.duty_resolution = LEDC_TIMER_13_BIT,
.freq_hz = 8000,
.speed_mode = LEDC_HS_MODE,
.timer_num = LEDC_HS_TIMER,
.clk_cfg = LEDC_AUTO_CLK,
};
ledc_timer_config(&ledc_timer);
//配置channel
ledc_channel_config_t ledc_channel[LEDC_TEST_CH_NUM] = {
{
.channel = LEDC_HS_CH0_CHANNEL,
.duty = 0,
.gpio_num = LEDC_HS_CH0_GPIO,
.speed_mode = LEDC_HS_MODE,
.hpoint = 0,
.timer_sel = LEDC_HS_TIMER
},
{
.channel = LEDC_HS_CH1_CHANNEL,
.duty = 0,
.gpio_num = LEDC_HS_CH1_GPIO,
.speed_mode = LEDC_HS_MODE,
.hpoint = 0,
.timer_sel = LEDC_HS_TIMER
},
{
.channel = LEDC_HS_CH2_CHANNEL,
.duty = 0,
.gpio_num = LEDC_HS_CH2_GPIO,
.speed_mode = LEDC_HS_MODE,
.hpoint = 0,
.timer_sel = LEDC_HS_TIMER
},
};
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
ledc_channel_config(&ledc_channel[ch]);
}
//不知道是干嘛用的不可以漏的一行代码
ledc_fade_func_install(0);
//主要逻辑思路如下
//channel0设置3秒渐变到8000,并开始
//延迟一秒
//channel1设置3秒渐变到8000,并开始,此时channel已经渐变了一秒,大概变化到了5300左右
//延迟一秒
//channel2设置3秒渐变到8000,并开始,此时channel0已经渐变了两秒,channel已经渐变了一秒
//延迟一秒
//channel0此时三秒已经过去,设置3秒渐变到0,并开始
//延迟一秒
//channel1此时三秒已经过去,设置3秒渐变到0,并开始,此时channel0第二次渐变已经渐变了一秒,大概变化到了2700左右
//延迟一秒
//channel2此时三秒已经过去,设置3秒渐变到0,并开始
//延迟一秒
//如此循环,三个IO的占空比就在一种微妙的不断变化并且不会一致,使得LED颜色不断变化
while (1) {
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
ledc_set_fade_with_time(ledc_channel[ch].speed_mode,
ledc_channel[ch].channel, LEDC_TEST_DUTY, LEDC_TEST_FADE_TIME);
ledc_fade_start(ledc_channel[ch].speed_mode,
ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
vTaskDelay(LEDC_TEST_FADE_TIME / 3 / portTICK_PERIOD_MS);
}
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
ledc_set_fade_with_time(ledc_channel[ch].speed_mode,
ledc_channel[ch].channel, 0, LEDC_TEST_FADE_TIME);
ledc_fade_start(ledc_channel[ch].speed_mode,
ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
vTaskDelay(LEDC_TEST_FADE_TIME / 3 / portTICK_PERIOD_MS);
}
}
}