ESP32 timers vs AVR timers

doomslayer666
Posts: 3
Joined: Sun Mar 15, 2020 11:05 am

ESP32 timers vs AVR timers

Postby doomslayer666 » Sun Mar 15, 2020 11:34 am

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?

doomslayer666
Posts: 3
Joined: Sun Mar 15, 2020 11:05 am

Re: ESP32 timers vs AVR timers

Postby doomslayer666 » Sat Apr 04, 2020 8:01 am

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.

mikemoy
Posts: 604
Joined: Fri Jan 12, 2018 9:10 pm

Re: ESP32 timers vs AVR timers

Postby mikemoy » Sat Apr 04, 2020 2:07 pm

Are you aware of the examples on github ?
You can find some examples in there.
https://github.com/espressif/esp-idf/tr ... eripherals

markkuk
Posts: 37
Joined: Wed Mar 27, 2019 11:50 am

Re: ESP32 timers vs AVR timers

Postby markkuk » Sun Apr 05, 2020 9:10 am

Timers aren't used for signal generation on ESP32. Use the LEDC, MCPWM or RMT units instead.

doomslayer666
Posts: 3
Joined: Sun Mar 15, 2020 11:05 am

Re: ESP32 timers vs AVR timers

Postby doomslayer666 » Thu Jun 25, 2020 4:49 pm

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.

ESP_Sprite
Posts: 9016
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 timers vs AVR timers

Postby ESP_Sprite » Fri Jun 26, 2020 7:43 am

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)

markkuk
Posts: 37
Joined: Wed Mar 27, 2019 11:50 am

Re: ESP32 timers vs AVR timers

Postby markkuk » Sun Jun 28, 2020 9:04 am

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++.

Who is online

Users browsing this forum: No registered users and 65 guests