Skip to content

Commit bf21695

Browse files
authored
Added example scanning maximum ledc frequencies (espressif#7460)
1 parent 9b88092 commit bf21695

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* This sketch will map the maximum frequency depending on the bit resolution for the current SoC.
3+
* Run the sketch and wait for the Final report.
4+
* Ignore the error messages from incorrect settings such as these:
5+
* "E (4190) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=255"
6+
*
7+
* Date: 11 Nov 2022
8+
* Author: Tomas Pilny
9+
*/
10+
11+
#include "soc/soc_caps.h"
12+
13+
#define PIN 2
14+
15+
void setup() {
16+
analogWrite(PIN,128);
17+
18+
uint32_t min_frequency;
19+
uint32_t max_frequency;
20+
uint32_t frequency;
21+
uint32_t successful_frequency;
22+
uint32_t max_freq_array[SOC_LEDC_TIMER_BIT_WIDE_NUM];
23+
uint32_t min_freq_array[SOC_LEDC_TIMER_BIT_WIDE_NUM];
24+
25+
// Find Max Frequency
26+
for(uint8_t resolution = 1; resolution <= SOC_LEDC_TIMER_BIT_WIDE_NUM; ++resolution){
27+
max_freq_array[resolution-1] = 0;
28+
min_frequency = 0;
29+
max_frequency = UINT32_MAX;
30+
successful_frequency = 0;
31+
while(min_frequency != max_frequency && min_frequency+1 != max_frequency){
32+
frequency = min_frequency + ((max_frequency-min_frequency)/2);
33+
if(ledcChangeFrequency(analogGetChannel(PIN), frequency, resolution)){
34+
min_frequency = frequency;
35+
successful_frequency = frequency;
36+
}else{
37+
max_frequency = frequency;
38+
}
39+
} // while not found the maximum
40+
max_freq_array[resolution-1] = successful_frequency;
41+
} // for all resolutions
42+
43+
// Find Min Frequency
44+
for(uint8_t resolution = 1; resolution <= SOC_LEDC_TIMER_BIT_WIDE_NUM; ++resolution){
45+
min_freq_array[resolution-1] = 0;
46+
min_frequency = 0;
47+
max_frequency = max_freq_array[resolution-1];
48+
successful_frequency = max_frequency;
49+
while(min_frequency != max_frequency && min_frequency+1 != max_frequency){
50+
frequency = min_frequency + ((max_frequency-min_frequency)/2);
51+
if(ledcChangeFrequency(analogGetChannel(PIN), frequency, resolution)){
52+
max_frequency = frequency;
53+
successful_frequency = frequency;
54+
}else{
55+
min_frequency = frequency;
56+
}
57+
} // while not found the maximum
58+
min_freq_array[resolution-1] = successful_frequency;
59+
} // for all resolutions
60+
61+
printf("Bit resolution | Min Frequency [Hz] | Max Frequency [Hz]\n");
62+
for(uint8_t r = 1; r <= SOC_LEDC_TIMER_BIT_WIDE_NUM; ++r){
63+
size_t max_len = std::to_string(UINT32_MAX).length();
64+
printf(" %s%d | %s%u | %s%u\n",
65+
std::string (2 - std::to_string(r).length(), ' ').c_str(), r,
66+
std::string (max_len - std::to_string(min_freq_array[r-1]).length(), ' ').c_str(),
67+
min_freq_array[r-1],
68+
std::string (max_len - std::to_string(max_freq_array[r-1]).length(), ' ').c_str(),
69+
max_freq_array[r-1]);
70+
}
71+
}
72+
73+
void loop()
74+
{
75+
delay(1000);
76+
}

0 commit comments

Comments
 (0)