2020-07-07 16:45:57 +08:00
|
|
|
|
# PWM
|
|
|
|
|
|
|
|
|
|
|
|
*PWM used to generate analog signals to drive servos or buzzers,etc.*
|
|
|
|
|
|
|
|
|
|
|
|
## ledcSetup()
|
|
|
|
|
|
|
|
|
|
|
|
**Syntax:**
|
|
|
|
|
|
|
2020-11-05 11:39:06 +08:00
|
|
|
|
`void ledcSetup(uint8_t channel, double freq, uint8_t resolution_bits);`
|
2020-07-07 16:45:57 +08:00
|
|
|
|
|
|
|
|
|
|
**Description:Set the PWM channel, frequency and resolution.**
|
|
|
|
|
|
|
|
|
|
|
|
## ledcAttachPin()
|
|
|
|
|
|
|
|
|
|
|
|
**Syntax:**
|
|
|
|
|
|
|
2020-11-05 11:39:06 +08:00
|
|
|
|
`void ledcAttachPin(uint8_t pin, uint8_t channel);`
|
2020-07-07 16:45:57 +08:00
|
|
|
|
|
|
|
|
|
|
**Description:Bind the LEDC channel to the specified IO port to achieve output.**
|
|
|
|
|
|
|
|
|
|
|
|
## ledWrite()
|
|
|
|
|
|
|
|
|
|
|
|
**Syntax:**
|
|
|
|
|
|
|
2020-11-05 11:39:06 +08:00
|
|
|
|
`void ledcWrite(uint8_t channel, uint32_t duty);`
|
2020-07-07 16:45:57 +08:00
|
|
|
|
|
|
|
|
|
|
**Description:Output waveform to the channel channel according to the specified duty cycle.**
|
|
|
|
|
|
|
|
|
|
|
|
**Example:**
|
2021-02-26 17:27:20 +08:00
|
|
|
|
```clike
|
2020-07-07 16:45:57 +08:00
|
|
|
|
#include <M5StickC.h>
|
|
|
|
|
|
int freq = 1800;
|
|
|
|
|
|
int channel = 0;
|
|
|
|
|
|
int resolution_bits = 8;
|
|
|
|
|
|
int buzzer = 2;
|
|
|
|
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
|
|
M5.begin();
|
|
|
|
|
|
ledcSetup(channel, freq, resolution_bits);
|
|
|
|
|
|
ledcAttachPin(buzzer, channel);
|
|
|
|
|
|
}
|
|
|
|
|
|
void loop() {
|
|
|
|
|
|
ledcWrite(channel, 128);
|
|
|
|
|
|
delay(1000);
|
|
|
|
|
|
ledcWrite(channel, 0);
|
|
|
|
|
|
delay(1000);
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|