本文是一篇笔记,是在网上一篇英语的教程文档整理摘录过来的,原文地址如有问题,请速联系我。

如何使用ESP32的LED PWM控制器,实现LED渐变和制定频率的方波。

一、整体介绍

本文中实验所使用的的是BPI Team的BPI-Webduino ESP32开发板,该开发板板载蜂鸣器和三色LED,因此不需要外接硬件。

硬件方面,ESP32的LED PWM控制器有16个独立的通道组成,具有可配置的占空比和频率。占空比的精度可以配置为16bit的分辨率。

二、软件设计

1. 参数配置

在代码的开始部分,我们需要先设定一些全局变量,方便后续的更改。

PWM信号的频率

Frequency_Max =

PWM信号通道

Channel_0 ~ Channel_15 (0-15)

PWM信号占空比的分辨率

1-16bit之间的占空比分辨率

// use first channel of 16 channels (started from zero)
#define LEDC_CHANNEL_0 0

// use 5000 Hz as a LEDC base frequency
#define LEDC_BASE_FREQ 4500

// use 13 bit precission for LEDC timer
#define LEDC_TIMER_13_BIT 13

然后,我们调用ledcsetup函数来进行配置,该函数以上面相同的顺序作为函数的入口参数。

ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);

此时,请注意,我们上面设置的通道并不是对应的实际的IO引脚,而是相当于我们命名为通道0的一路PWM信号,下面我还需要把这路PWM信号实际对应到GPIO上面去。

// fade LED PIN (replace with LED_BUILTIN constant for built-in LED)
#define LED_PIN 27

ledcAttachPin(LED_PIN, LEDC_CHANNEL_0);

下面完整的展示下变量定义和PWM参数配置过程:

//LEDC PWM Control
#include "Arduino.h"

// use first channel of 16 channels (started from zero)
#define LEDC_CHANNEL_0 0

// use 13 bit precission for LEDC timer
#define LEDC_TIMER_13_BIT 13

// use 5000 Hz as a LEDC base frequency
#define LEDC_BASE_FREQ 4500

// fade LED PIN (replace with LED_BUILTIN constant for built-in LED)
#define LED_PIN 14

void setup()
{
    // Setup timer and attach timer to a led pin
    ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
    ledcAttachPin(LED_PIN, LEDC_CHANNEL_0);
}

2. 主循环

在此之前,我们先自己写了一个PWM控制函数ledcAnalogWrite来控制PWM信号。当然这个函数内部也调用了ledcWrite函数来输入占空比到制定通道。

// Arduino like analogWrite
// value has to be between 0 and valueMax
void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255)
{
    // calculate duty, 8191 from 2 ^ 13 - 1
    uint32_t duty = (8191 / valueMax) * min(value, valueMax);

    // write duty to LEDC
    ledcWrite(channel, duty);
}

由于我们定义了8位分辨率,因此我们可以指定0到255(2 ^ 8 -1)之间的占空比值。所以,我们将在两个循环之间迭代这些值,一个升序和另一个降序。全部代码如下:

//LEDC
#include <Arduino.h>

// use first channel of 16 channels (started from zero)
#define LEDC_CHANNEL_0 0

// use 13 bit precission for LEDC timer
#define LEDC_TIMER_13_BIT 13

// use 5000 Hz as a LEDC base frequency
#define LEDC_BASE_FREQ 4500

// fade LED PIN (replace with LED_BUILTIN constant for built-in LED)
#define LED_PIN 27


int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

// Arduino like analogWrite
// value has to be between 0 and valueMax
void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255)
{
    // calculate duty, 8191 from 2 ^ 13 - 1
    uint32_t duty = (8191 / valueMax) * min(value, valueMax);

    // write duty to LEDC
    ledcWrite(channel, duty);
}

void setup()
{
    // Setup timer and attach timer to a led pin
    ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
    ledcAttachPin(LED_PIN, LEDC_CHANNEL_0);
}

void loop()
{
    // set the brightness on LEDC channel 0
    ledcAnalogWrite(LEDC_CHANNEL_0, brightness);

    // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;

    // reverse the direction of the fading at the ends of the fade:
    if (brightness <= 0 || brightness >= 255)
    {
        fadeAmount = -fadeAmount;
    }
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
}

这是Arduino IDE 中ESP32开发板示例ESP32>AnalogOut>LEDCSoftwareFade中的代码,我只是引用过来作为样例,结合网上查阅的文档与大家一起学习。

3.相关内容

Arduino-ESP32 LEDC源文件

Arduino-ESP32 LEDC头文件

Arduino IDE示例内容

4. 参考资料

[1]https://github.com/espressif/arduino-esp32#development-status

[2]https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf

最后修改:2018 年 03 月 19 日
如果觉得我的文章对你有用,请随意赞赏