Skip to content

Instantly share code, notes, and snippets.

@JimAllanson
Created Oct 25, 2021
Embed
What would you like to do?
#include "../hardware_api.h"
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32)
#if SOC_MCPWM_SUPPORTED
#else
#include "driver/ledc.h"
#endif
#define _PWM_FREQUENCY 25000 // 25khz
#define _PWM_FREQUENCY_MAX 50000 // 50khz
// configure High PWM frequency
void _setHighFrequency(const long freq, const int pin, const int channel){
ledcSetup(channel, 5000, LEDC_TIMER_13_BIT);
ledcAttachPin(pin, channel);
}
void _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
if(!pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
_setHighFrequency(pwm_frequency, pinA, LEDC_CHANNEL_0);
_setHighFrequency(pwm_frequency, pinB, LEDC_CHANNEL_1);
}
void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC) {
if(!pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
_setHighFrequency(pwm_frequency, pinA, LEDC_CHANNEL_0);
_setHighFrequency(pwm_frequency, pinB, LEDC_CHANNEL_1);
_setHighFrequency(pwm_frequency, pinC, LEDC_CHANNEL_2);
}
void _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC, const int pinD) {
if(!pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
_setHighFrequency(pwm_frequency, pinA, LEDC_CHANNEL_0);
_setHighFrequency(pwm_frequency, pinB, LEDC_CHANNEL_1);
_setHighFrequency(pwm_frequency, pinC, LEDC_CHANNEL_2);
_setHighFrequency(pwm_frequency, pinD, LEDC_CHANNEL_3);
}
void _configure6PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC, const int pinD, const int pinE, const int pinF) {
if(!pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
_setHighFrequency(pwm_frequency, pinA, LEDC_CHANNEL_0);
_setHighFrequency(pwm_frequency, pinB, LEDC_CHANNEL_1);
_setHighFrequency(pwm_frequency, pinC, LEDC_CHANNEL_2);
_setHighFrequency(pwm_frequency, pinD, LEDC_CHANNEL_3);
_setHighFrequency(pwm_frequency, pinE, LEDC_CHANNEL_4);
_setHighFrequency(pwm_frequency, pinF, LEDC_CHANNEL_5);
}
void _writeDutyCycle2PWM(float dc_a, float dc_b, int pinA, int pinB){
uint32_t dutyA = (8191 / 255) * min(255.0f*dc_a, 255.0f);
uint32_t dutyB = (8191 / 255) * min(255.0f*dc_b, 255.0f);
ledcWrite(LEDC_CHANNEL_0, dutyA);
ledcWrite(LEDC_CHANNEL_1, dutyB);
}
void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, int pinA, int pinB, int pinC){
uint32_t dutyA = (8191 / 255) * min(255.0f*dc_a, 255.0f);
uint32_t dutyB = (8191 / 255) * min(255.0f*dc_b, 255.0f);
uint32_t dutyC = (8191 / 255) * min(255.0f*dc_c, 255.0f);
ledcWrite(LEDC_CHANNEL_0, dutyA);
ledcWrite(LEDC_CHANNEL_1, dutyB);
ledcWrite(LEDC_CHANNEL_2, dutyC);
}
void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, int pin1A, int pin1B, int pin2A, int pin2B){
uint32_t duty1A = (8191 / 255) * min(255.0f*dc_1a, 255.0f);
uint32_t duty1B = (8191 / 255) * min(255.0f*dc_1b, 255.0f);
uint32_t duty2A = (8191 / 255) * min(255.0f*dc_2a, 255.0f);
uint32_t duty2B = (8191 / 255) * min(255.0f*dc_2b, 255.0f);
ledcWrite(LEDC_CHANNEL_0, duty1A);
ledcWrite(LEDC_CHANNEL_1, duty1B);
ledcWrite(LEDC_CHANNEL_2, duty2A);
ledcWrite(LEDC_CHANNEL_3, duty2B);
}
void _writeDutyCycle6PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, float dc_3a, float dc_3b, int pin1A, int pin1B, int pin2A, int pin2B, int pin3A, int pin3B ){
uint32_t duty1A = (8191 / 255) * min(255.0f*dc_1a, 255.0f);
uint32_t duty1B = (8191 / 255) * min(255.0f*dc_1b, 255.0f);
uint32_t duty2A = (8191 / 255) * min(255.0f*dc_2a, 255.0f);
uint32_t duty2B = (8191 / 255) * min(255.0f*dc_2b, 255.0f);
uint32_t duty3A = (8191 / 255) * min(255.0f*dc_3a, 255.0f);
uint32_t duty3B = (8191 / 255) * min(255.0f*dc_3b, 255.0f);
ledcWrite(LEDC_CHANNEL_0, duty1A);
ledcWrite(LEDC_CHANNEL_1, duty1B);
ledcWrite(LEDC_CHANNEL_2, duty2A);
ledcWrite(LEDC_CHANNEL_3, duty2B);
ledcWrite(LEDC_CHANNEL_4, duty3A);
ledcWrite(LEDC_CHANNEL_5, duty3B);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment