single channel frequency generator

ESP32Newb
Posts: 1
Joined: Wed Jan 31, 2018 9:41 pm

single channel frequency generator

Postby ESP32Newb » Wed Jan 31, 2018 9:53 pm

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

ESP_igrr
Posts: 1596
Joined: Tue Dec 01, 2015 8:37 am

Re: single channel frequency generator

Postby ESP_igrr » Thu Feb 01, 2018 4:14 am

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

ArtemN
Posts: 13
Joined: Mon Sep 03, 2018 4:20 am

Re: single channel frequency generator

Postby ArtemN » Tue Feb 12, 2019 5:56 am

There is arduino examples for sub Hz PWM?

pekka.lehtikoski
Posts: 1
Joined: Wed Mar 25, 2020 11:46 am

Re: single channel frequency generator

Postby pekka.lehtikoski » Wed Mar 25, 2020 11:51 am

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: AndreiGramakov, Majestic-12 [Bot], tabulous and 40 guests