Arduino: use on ESP32 LEDC (PWM) (3)

Arduino core for the ESP32 no general Arduino in analogWrite (pin, value) for outputting the PWM method, there is instead a ESP32 LEDC, it is designed to control the LED.
ESP32 of LEDC a total of 16 channels (0 to 15), is divided into two high-low, high-speed channel (0 ~ 7) driven by a 80MHz clock, the low-speed channel (8 to 15) driven by a 1MHz clock.

purpose

LEDC implemented using breathing light

Supporting Introduction

Authoring Tools: vscode + platformIO installation tutorial

Hardware: bpibit

The main function

  • double ledcSetup(uint8_t channel, double freq, uint8_t resolution_bits)
    Setting the frequency and counting the number of bits (resolution duty ratio) corresponding to the channel LEDC,
    the method returns a final frequency

The final channel frequency = clock / (division factor * (1 << digit count)); (division factor of up to 1024)

parameter Features
channel Is the channel number, ranging from 0 to 15
freq Set the desired frequency
resolution_bits Counting the number of bits, ranging from 0 to 20 (this value determines duty cycle later ledcWrite write process value, such as 10 to write this value, the maximum duty cycle that is written 1023 (1 << resolution_bits) -1)
  • void ledcWrite(uint8_t channel, uint32_t duty)
    Certain specified duty cycle waveform channel output

  • double ledcWriteTone(uint8_t channel, double freq)
    Arduino similar to the tone, the time when the external passive buzzer may emit a sound (depending on the frequency varies)

  • double ledcWriteNote(uint8_t channel, note_t note, uint8_t octave)
    The method is further encapsulated in the above process, a signal may be output directly specified modal scale and sound

parameter Features
note Tuning, the equivalent of do, re, mi, fa ...... these values ​​is NOTE_C, NOTE_Cs, NOTE_D, NOTE_Eb, NOTE_E, NOTE_F, NOTE_Fs, NOTE_G, NOTE_Gs, NOTE_A, NOTE_Bb, NOTE_B

octave scale, ranging from 0 to 7;
music theory related content may refer to the following articles:
http://www.360doc.com/content/17/1231/01/47685146_717797647.shtml
https://www.musicbody.net/sns/ index.php? s = / news / index / detail / id / 406.html

  • uint32_t ledcRead(uint8_t channel)
    Returns the specified channel duty ratio

  • double ledcReadFreq(uint8_t channel)
    Returns the current frequency channel (if the current duty ratio is 0, the method returns 0)

  • void ledcAttachPin(uint8_t pin, uint8_t channel)
    The LEDC is bound to a channel in order to achieve the output port IO

  • void ledcDetachPin(uint8_t pin)
    LEDC released IO port function

Examples of Use

#include <Arduino.h>

int freq = 2000;    // 频率
int channel = 0;    // 通道
int resolution = 8;   // 分辨率

const int led = 18;
void setup()
{

  ledcSetup(channel, freq, resolution); // 设置通道
  ledcAttachPin(led, channel);  // 将通道与对应的引脚连接
}

void loop()
{
  // 逐渐变亮
  for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle = dutyCycle + 5)
  {
    ledcWrite(channel, dutyCycle);  // 输出PWM
    delay(20);
  }

  // 逐渐变暗
  for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle = dutyCycle - 5)
  {
    ledcWrite(channel, dutyCycle);  // 输出PWM
    delay(20);
  }
}

to sum up

It did not like the Arduino for outputting the PWM analogWrite (pin, value) method Arduino core for the ESP32, but rather with LEDC (LED Control) functions to implement PWM

Guess you like

Origin blog.csdn.net/weixin_43474408/article/details/90743082