Dlloydev / ESP32-ESP32S2-AnalogWrite

ESP32 PWM, Servo, Easing and Tone. Smart GPIO pin management and advanced control features.
MIT License
100 stars 17 forks source link

How to implement without library--const uint16_t phase[2] = {510,0}; #25

Closed he77PC011 closed 1 year ago

he77PC011 commented 1 year ago

I am a novice How to implement without library--

const uint16_t phase[2] = {510,0};

define LED1 19

define LED2 21

define LED1_OFF digitalWrite(LED1, HIGH)//关灯1

define LED1_ON digitalWrite(LED1, LOW)//开灯1

define LED1_PWM digitalWrite(LED1, !digitalRead(LED1))//灯1闪烁

define LED2_OFF digitalWrite(LED2, HIGH)//关灯2

define LED2_ON digitalWrite(LED2, LOW)//开灯2

define LED2_PWM digitalWrite(LED2, !digitalRead(LED2))//灯2闪烁

int freq1 = 39000; // 频率 int channel1 = 0; // 通道0,共16个通道,0~15 int resolution1 = 11; // 分辨率,取值0~20,duty最大取值为2^resolution-1

int freq2 = 39000; // 频率 int channel2 = 0; // 通道1,共16个通道,0~15 int resolution2 = 11; // 分辨率,取值0~20,duty最大取值为2^resolution-1

void setup() { ledcSetup(channel1, freq1, resolution1, .hpoint ); // 设置通道0 ledcSetup(channel2, freq2, resolution2 ); // 设置通道1 ledcAttachPin(LED1, channel1 ); // 将通道0与引脚19连接 ledcAttachPin(LED2, channel2 ); // 将通道1与引脚22连接 }

void loop() { // 逐渐变亮 int dutyCycle = 512; { ledcWrite(channel1, dutyCycle); // 输出PWM ledcWrite(channel2, 1023 - dutyCycle); // 输出PWM delay(5); } }

Dlloydev commented 1 year ago

Not an issue with the library, so converting to discussion. Maybe you could try something like this ...

#include "driver/ledc.h"

const uint16_t phase[2] = {510, 0};

#define LED1 19
#define LED2 21
#define LED1_OFF digitalWrite(LED1, HIGH)//关灯1
#define LED1_ON digitalWrite(LED1, LOW)//开灯1
#define LED1_PWM digitalWrite(LED1, !digitalRead(LED1))//灯1闪烁
#define LED2_OFF digitalWrite(LED2, HIGH)//关灯2
#define LED2_ON digitalWrite(LED2, LOW)//开灯2
#define LED2_PWM digitalWrite(LED2, !digitalRead(LED2))//灯2闪烁

int freq1 = 39000; // 频率
int channel1 = 0; // 通道0,共16个通道,015
int resolution1 = 11; // 分辨率,取值020,duty最大取值为2^resolution-1

int freq2 = 39000; // 频率
int channel2 = 0; // 通道1,共16个通道,015
int resolution2 = 11; // 分辨率,取值020,duty最大取值为2^resolution-1

int speedMode = 0;
int dutyCycle = 512;

void setup()
{
  ledcSetup(channel1, freq1, resolution1); // 设置通道0
  ledc_set_duty_with_hpoint((ledc_mode_t)speedMode, (ledc_channel_t)channel1, dutyCycle, phase[0]);

  ledcSetup(channel2, freq2, resolution2 ); // 设置通道1
  ledcAttachPin(LED1, channel1 ); // 将通道0与引脚19连接
  ledcAttachPin(LED2, channel2 ); // 将通道1与引脚22连接
}

void loop()
{
  // 逐渐变亮
  {
    ledcWrite(channel1, dutyCycle); // 输出PWM
    ledcWrite(channel2, 1023 - dutyCycle); // 输出PWM
    delay(5);
  }
}