Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef PWM_CONTROLLER_H
- #define PWM_CONTROLLER_H
- #include "settings.h"
- class PWMController {
- public:
- PWMController() {
- for (int i = 0; i < 4; i++) { // Все 4 канала
- ledcSetup(pwmChannels[i], frequencies[i], getResolution(frequencies[i]));
- ledcAttachPin(pwmPins[i], pwmChannels[i]);
- ledcWrite(pwmChannels[i], 0);
- }
- }
- void setFrequency(int channel, int freq) {
- if (channel >= 0 && channel < 4 && freq >= 1 && freq <= 20000) {
- frequencies[channel] = freq;
- ledcSetup(pwmChannels[channel], freq, getResolution(freq));
- }
- }
- void setDuty(int channel, int duty) {
- if (channel >= 0 && channel < 4 && duty >= 0 && duty <= 100) {
- duties[channel] = duty;
- int pwmValue = map(duty, 0, 100, 0, (1 << getResolution(frequencies[channel])) - 1);
- ledcWrite(pwmChannels[channel], pwmValue);
- }
- }
- private:
- int getResolution(int freq) {
- if (freq < 10) return 16;
- if (freq < 100) return 14;
- if (freq < 1000) return 12;
- return 10;
- }
- };
- #endif
- ///
- ///settings.h
- ///
- #ifndef SETTINGS_H
- #define SETTINGS_H
- // Пины для ШИМ
- const int pwmPins[] = {1, 2, 3, 10}; // GPIO
- const int pwmChannels[] = {0, 1, 2, 3}; // Каналы LEDC
- extern int frequencies[] = {1, 100, 1000, 1000};
- extern int duties[] = {5, 25, 50, 100};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement