This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPWM_DynamicFreq.ino
100 lines (76 loc) · 2.87 KB
/
PWM_DynamicFreq.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/****************************************************************************************************************************
PWM_DynamicFreq.ino
For ESP32, ESP32_S2, ESP32_S3, ESP32_C3 boards with ESP32 core v2.0.0+
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP32_FastPWM
Licensed under MIT license
This is pure hardware-based PWM
*****************************************************************************************************************************/
/******************************************************************************************************************************
// All GPIO pins (but GPIO34-39) can be used to generate PWM
// For ESP32, number of channels is 16, max 20-bit resolution
// For ESP32_S2, ESP32_S3, number of channels is 8
// For ESP32_C3, number of channels is 6
******************************************************************************************************************************/
#define _PWM_LOGLEVEL_ 4
#include "ESP32_FastPWM.h"
#if ARDUINO_ESP32C3_DEV
#define pinToUse 9
#else
#define pinToUse 16
#endif
// Max resolution is 20-bit
// Resolution 65536 (16-bit) for lower frequencies, OK @ 1K
// Resolution 4096 (12-bit) for lower frequencies, OK @ 10K
// Resolution 1024 (10-bit) for higher frequencies, OK @ 50K
// Resolution 256 ( 8-bit)for higher frequencies, OK @ 100K, 200K
// Resolution 128 ( 7-bit) for higher frequencies, OK @ 500K
int PWM_resolution = 10;
//creates pwm instance
ESP32_FAST_PWM* PWM_Instance;
float frequency;
char dashLine[] = "=====================================================================================";
void printPWMInfo(ESP32_FAST_PWM* PWM_Instance)
{
Serial.println(dashLine);
Serial.print("Actual data: pin = ");
Serial.print(PWM_Instance->getPin());
Serial.print(", PWM DC = ");
Serial.print(PWM_Instance->getActualDutyCycle());
Serial.print(", PWMPeriod = ");
Serial.print(PWM_Instance->getPWMPeriod());
Serial.print(", PWM Freq (Hz) = ");
Serial.println(PWM_Instance->getActualFreq(), 4);
Serial.println(dashLine);
}
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(500);
Serial.print(F("\nStarting PWM_DynamicFreq on "));
Serial.println(ARDUINO_BOARD);
Serial.println(ESP32_FAST_PWM_VERSION);
frequency = 10000.0f;
PWM_Instance = new ESP32_FAST_PWM(pinToUse, frequency, 50.0f);
if (PWM_Instance)
{
PWM_Instance->setPWM();
}
Serial.println(dashLine);
}
void loop()
{
delay(5000);
frequency = 20000.0f;
Serial.print(F("Change PWM Freq to "));
Serial.println(frequency);
PWM_Instance->setPWM(pinToUse, frequency, 50.0f);
printPWMInfo(PWM_Instance);
delay(5000);
frequency = 10000.0f;
Serial.print(F("Change PWM Freq to "));
Serial.println(frequency);
PWM_Instance->setPWM(pinToUse, frequency, 50.0f);
printPWMInfo(PWM_Instance);
}