-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
libraries/ESP32/examples/AnalogOut/ledcFrequency/ledcFrequency.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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: 10 Nov 2022 | ||
* Author: Tomas Pilny | ||
*/ | ||
|
||
#include "soc/soc_caps.h" | ||
|
||
#define PIN 2 | ||
|
||
void setup() { | ||
ledcAttachPin(PIN,analogGetChannel(PIN)); | ||
analogWrite(PIN,128); | ||
|
||
uint32_t min_frequency; | ||
uint32_t max_frequency; | ||
uint32_t frequency; | ||
uint32_t freq_array[SOC_LEDC_TIMER_BIT_WIDE_NUM]; | ||
delay(500); | ||
|
||
for(uint8_t resolution = 1; resolution <= SOC_LEDC_TIMER_BIT_WIDE_NUM; ++resolution){ | ||
freq_array[resolution-1] = 0; | ||
min_frequency = 0; | ||
max_frequency = UINT32_MAX; | ||
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; | ||
}else{ | ||
max_frequency = frequency; | ||
if(frequency == 1){ // Cannot go lower, report max frequency "0" | ||
frequency = 0; | ||
} | ||
} | ||
} // while not found the maximum | ||
freq_array[resolution-1] = frequency; | ||
} // for all resolutions | ||
|
||
printf("Bit resolution | Max Frequency [Hz]\n"); | ||
for(uint8_t r = 1; r <= SOC_LEDC_TIMER_BIT_WIDE_NUM; ++r){ | ||
printf(" %d | %u\n", r, freq_array[r-1]); | ||
} | ||
} | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
void loop() | ||
{ | ||
delay(1000); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.