Skip to content

Added example scanning maximum ledc frequencies #7460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions libraries/ESP32/examples/AnalogOut/ledcFrequency/ledcFrequency.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* This sketch will map the maximum frequency depending on the bit resolution for the current SoC.
* Run the sketch and wait for the Final report.
* Ignore the error messages from incorrect settings such as these:
* "E (4190) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=255"
*
* Date: 11 Nov 2022
* Author: Tomas Pilny
*/

#include "soc/soc_caps.h"

#define PIN 2

void setup() {
analogWrite(PIN,128);

uint32_t min_frequency;
uint32_t max_frequency;
uint32_t frequency;
uint32_t successful_frequency;
uint32_t max_freq_array[SOC_LEDC_TIMER_BIT_WIDE_NUM];
uint32_t min_freq_array[SOC_LEDC_TIMER_BIT_WIDE_NUM];

// Find Max Frequency
for(uint8_t resolution = 1; resolution <= SOC_LEDC_TIMER_BIT_WIDE_NUM; ++resolution){
max_freq_array[resolution-1] = 0;
min_frequency = 0;
max_frequency = UINT32_MAX;
successful_frequency = 0;
while(min_frequency != max_frequency && min_frequency+1 != max_frequency){
frequency = min_frequency + ((max_frequency-min_frequency)/2);
if(ledcChangeFrequency(analogGetChannel(PIN), frequency, resolution)){
min_frequency = frequency;
successful_frequency = frequency;
}else{
max_frequency = frequency;
}
} // while not found the maximum
max_freq_array[resolution-1] = successful_frequency;
} // for all resolutions

// Find Min Frequency
for(uint8_t resolution = 1; resolution <= SOC_LEDC_TIMER_BIT_WIDE_NUM; ++resolution){
min_freq_array[resolution-1] = 0;
min_frequency = 0;
max_frequency = max_freq_array[resolution-1];
successful_frequency = max_frequency;
while(min_frequency != max_frequency && min_frequency+1 != max_frequency){
frequency = min_frequency + ((max_frequency-min_frequency)/2);
if(ledcChangeFrequency(analogGetChannel(PIN), frequency, resolution)){
max_frequency = frequency;
successful_frequency = frequency;
}else{
min_frequency = frequency;
}
} // while not found the maximum
min_freq_array[resolution-1] = successful_frequency;
} // for all resolutions

printf("Bit resolution | Min Frequency [Hz] | Max Frequency [Hz]\n");
for(uint8_t r = 1; r <= SOC_LEDC_TIMER_BIT_WIDE_NUM; ++r){
size_t max_len = std::to_string(UINT32_MAX).length();
printf(" %s%d | %s%u | %s%u\n",
std::string (2 - std::to_string(r).length(), ' ').c_str(), r,
std::string (max_len - std::to_string(min_freq_array[r-1]).length(), ' ').c_str(),
min_freq_array[r-1],
std::string (max_len - std::to_string(max_freq_array[r-1]).length(), ' ').c_str(),
max_freq_array[r-1]);
}
}

void loop()
{
delay(1000);
}