Need to generate single channel output frequency between X Hz to Y MHz. No limitations on the waveform or duty cycle, so it can be a squarewave with 30% DC, for example, and I'll be very happy.
Requirements:
a. to have the widest range of X and Y.
b. Have the ability to change the output frequency at runtime
Questions:
1. Can I have X be 1Hz?
2. Can I have Y be 1MHz?
What are some realistic numbers for X and Y using the least amount of code and/or using onboard peripherals?
I know that I can generate low frequency (X) by simply bitbanging a GPIO with the appropriate delay, but I would like to use hardware internal to the ESP32, like PWM/Timer
My concern is that, for X = 1Hz, the width of the timer register would be too small to count upto seconds and it would overflow multiples times because the clock rate is in nS on the ESP32 (is that correct?)
I can program using C/C++/Python, so language is not a problem for me
If I made any incorrect assumptions about the ESP32, please correct me
single channel frequency generator
Re: single channel frequency generator
LEDC peripheral can be used to generate PWM signals between 40 MHz (half of APB clock) and approximately 0.001 Hz.
Please check the LEDC chapter in Technical Reference Manual.
Here's a quick example which generates a ~1Hz signal.
https://gist.github.com/igrr/2875a9ca3a ... 47bee9850f
Please check the LEDC chapter in Technical Reference Manual.
Here's a quick example which generates a ~1Hz signal.
https://gist.github.com/igrr/2875a9ca3a ... 47bee9850f
Re: single channel frequency generator
There is arduino examples for sub Hz PWM?
-
- Posts: 1
- Joined: Wed Mar 25, 2020 11:46 am
Re: single channel frequency generator
Code: Select all
/*
* Example: Generate 1 MHz clock signal with ESP32
note 24.3.2020/pekka
ESP32: LEDC peripheral can be used to generate clock signals between 40 MHz (half of APB clock) and approximately 0.001 Hz.
Please check the LEDC chapter in Technical Reference Manual. Example code below.
*/
#include "driver/ledc.h"
#include "driver/periph_ctrl.h"
void set_1MHz_clock_on_GPIO2(void)
{
periph_module_enable(PERIPH_LEDC_MODULE);
// Set up timer
ledc_timer_config_t ledc_timer = {
.duty_resolution = LEDC_TIMER_1_BIT, // We need clock, not PWM so 1 bit is enough.
.freq_hz = 1000000, // Clock frequency, 1 MHz
.speed_mode = LEDC_HIGH_SPEED_MODE,
.timer_num = LEDC_TIMER_0,
// .clk_cfg = LEDC_USE_APB_CLK // I think this is needed for neweer espressif software, if problem, try uncommenting this line
};
ledc_timer_config(&ledc_timer);
// Set up GPIO PIN
ledc_channel_config_t channel_config = {
.channel = LEDC_CHANNEL_0,
.duty = 1,
.gpio_num = 2, // GPIO pin
.speed_mode = LEDC_HIGH_SPEED_MODE,
.timer_sel = LEDC_TIMER_0
};
ledc_channel_config(&channel_config);
}
Who is online
Users browsing this forum: Bing [Bot] and 94 guests