Page 1 of 1

ESP32 timers vs AVR timers

Posted: Sun Mar 15, 2020 11:34 am
by doomslayer666
Hi,

I am trying to port a few lines of code written for an Arduino Uno over to an ESP32.

This is the original code (found here https://github.com/madlabdk/touche/blob ... _graph.ino):

Code: Select all

#define steps 128

void setup ()
{
  pinMode (9, OUTPUT); 
  TCCR1A = 0;
  TCCR1B = 0;
  TCCR1A |= (1 << COM1A0);   // Toggle OC1A on Compare Match.
  TCCR1B |= (1 << WGM12);    // CTC mode
  Serial.begin(57600);
}

void loop () {
  for (int i = 0; i < steps; i++) {
    TCCR1B &= 0xFE; // turns off timer
    TCNT1 = 0;      // resets timer counter register
    OCR1A = i;      // changes frequency step
    TCCR1B |= 0x01; // turns on timer
    Serial.print(i,DEC);
    Serial.print(" ");
    Serial.println(analogRead(0), DEC);
  }
}
It's manipulating internal Timer 1 in order to generate different output frequencies on PIN 9 (which are then read from PIN 0).

From reading the ESP32 docs, concepts like "Output Compare Register" don't seem to exist in the ESP32 realm. Is there any way to make this work on an ESP32?

Re: ESP32 timers vs AVR timers

Posted: Sat Apr 04, 2020 8:01 am
by doomslayer666
I am still struggling with this. If what I'm asking for is not that simple, I would also highly appreciate any resources that explain timer manipulation in a more approachable way than the ESP32 documentation.

Re: ESP32 timers vs AVR timers

Posted: Sat Apr 04, 2020 2:07 pm
by mikemoy
Are you aware of the examples on github ?
You can find some examples in there.
https://github.com/espressif/esp-idf/tr ... eripherals

Re: ESP32 timers vs AVR timers

Posted: Sun Apr 05, 2020 9:10 am
by markkuk
Timers aren't used for signal generation on ESP32. Use the LEDC, MCPWM or RMT units instead.

Re: ESP32 timers vs AVR timers

Posted: Thu Jun 25, 2020 4:49 pm
by doomslayer666
markkuk wrote: Timers aren't used for signal generation on ESP32. Use the LEDC, MCPWM or RMT units instead.
Thank you for your response!

I have been trying to use the LEDC units, however I am failing horribly.
I am not even able to configure the timer.
I took a look at the LEDC example, however it won't compile.

Code: Select all

ledc_timer_config_t ledc_timer = {
        .duty_resolution = LEDC_TIMER_13_BIT, // resolution of PWM duty
        .freq_hz = 5000,                      // frequency of PWM signal
        .speed_mode = LEDC_LS_MODE,           // timer mode
        .timer_num = LEDC_LS_TIMER            // timer index
        .clk_cfg = LEDC_AUTO_CLK,              // Auto select the source clock
    };
The ledc_timer_config() throws an error "request for member 'clk_cfg' in 'LEDC_TIMER_1', which is of non-class type 'ledc_timer_t'"

I don't know if there is any ESP-IDF ressource I am missing besides the docs and the ESP32 manual?
For instance, besides the above code from the example, I wasn't even able to find out what the "LEDC timer configure struct" even is.

Re: ESP32 timers vs AVR timers

Posted: Fri Jun 26, 2020 7:43 am
by ESP_Sprite
Have you #include'd the correct headers? Worst case, just copy/paste all that the example uses (they're in the first few lines of the file usually)

Re: ESP32 timers vs AVR timers

Posted: Sun Jun 28, 2020 9:04 am
by markkuk
doomslayer666 wrote:
Thu Jun 25, 2020 4:49 pm
I took a look at the LEDC example, however it won't compile.

Code: Select all

ledc_timer_config_t ledc_timer = {
        .duty_resolution = LEDC_TIMER_13_BIT, // resolution of PWM duty
        .freq_hz = 5000,                      // frequency of PWM signal
        .speed_mode = LEDC_LS_MODE,           // timer mode
        .timer_num = LEDC_LS_TIMER            // timer index
        .clk_cfg = LEDC_AUTO_CLK,              // Auto select the source clock
    };
The ledc_timer_config() throws an error "request for member 'clk_cfg' in 'LEDC_TIMER_1', which is of non-class type 'ledc_timer_t'"
That form of structure initialization works only in C code (C99 or later), not in C++ or Arduino. The error message "non-class type" looks like it's coming from C++.