// #include<tools/sdk/esp32/include/driver/include/driver/ledc.h>
#include<driver/ledc.h>
void setup() {
// put your setup code here, to run once:
// Serial.begin(115200);
// Serial.println("Hello, ESP32!");
analogWriteResolution(12);
pinMode(18, OUTPUT);
pinMode(19, OUTPUT);
// for setup
analogWrite(18, 0);
analogWrite(19, 0);
}
// based on the implementation of analogWrite() and ledcWrite()
// without the remapping of 0..4095 to 0..4096
void set_raw_pwm_channel(uint8_t pin, uint32_t duty) {
uint8_t chan = analogGetChannel(pin);
ledc_mode_t group = (ledc_mode_t)(chan/8);
ledc_channel_t channel = (ledc_channel_t)(chan%8);
ledc_set_duty(group, channel, duty);
ledc_update_duty(group, channel);
}
void loop() {
/* Old code:
// these two work fine
analogWrite(18, 4093);
analogWrite(19, 4094);
// these used to have a bug
analogWrite(18, 4095); // these should both be 100 % on because
analogWrite(19, 4096); // analogWrite() maps 4095 to 4096 internally
*/
set_raw_pwm_channel(18, 4093);
set_raw_pwm_channel(19, 4094);
delay(1000);
// these don't
set_raw_pwm_channel(18, 4095); // this should be 4095/4096 = 99.975 % on
set_raw_pwm_channel(19, 4096); // this should be 100 % on
delay(1000);
}