AVR to ESP32 Timer

Hello everyone,

I am struggling with an issue here. I was using Arduino but now i have to transfer my code to ESP32 WRover B. Below i have part of my code. The timer suppose to count and interrupt function should work. How can i transfer it to ESP32? Are there anything like OCR2B or OCR1A in ESP32? Thank you for helping.

void startPlayback()
{
  pinMode(SPEAKER, OUTPUT);
  audioRunning = true;
  

  // Set up Timer 2 to do pulse width modulation on the speaker pin.
  ASSR &= ~(_BV(EXCLK) | _BV(AS2));                         // Use internal clock (datasheet p.160)

  TCCR2A |= _BV(WGM21) | _BV(WGM20);                        // Set fast PWM mode  (p.157)
  TCCR2B &= ~_BV(WGM22);

  TCCR2A = (TCCR2A | _BV(COM2B1)) & ~_BV(COM2B0);           // Do non-inverting PWM on pin OC2B (p.155)
  TCCR2A &= ~(_BV(COM2A1) | _BV(COM2A0));                   // On the Arduino this is pin 3.
  TCCR2B = (TCCR2B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10); // No prescaler (p.158)

  OCR2B = pgm_read_byte(&idle_data[0]);                     // Set initial pulse width to the first sample.
  OCR2B = pgm_read_byte(&idle_data2[0]);  

  // Set up Timer 1 to send a sample every interrupt.
  cli();

  TCCR1B = (TCCR1B & ~_BV(WGM13)) | _BV(WGM12);             // Set CTC mode (Clear Timer on Compare Match) (p.133)
  TCCR1A = TCCR1A & ~(_BV(WGM11) | _BV(WGM10));             // Have to set OCR1A *after*, otherwise it gets reset to 0!

  TCCR1B = (TCCR1B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10); // No prescaler (p.134)

  OCR1A = F_CPU / BASE_RATE;                                // Set the compare register (OCR1A).
                                                            // OCR1A is a 16-bit register, so we have to do this with
                                                            // interrupts disabled to be safe.

  TIMSK1 |= _BV(OCIE1A);                                   // Enable interrupt when TCNT1 == OCR1A (p.136)

  //lastSample = pgm_read_byte(&idle_data2[idle_len2-1]);
  curEngineSample = 0;

  sei();
  


}



ISR(TIMER1_COMPA_vect) {
  OCR1A = currentSmpleRate;


  if (curEngineSample >= idle_len ) { // Loop the sample

      curEngineSample = 0; 
         
  }


  if ((curEngineSample) % 2 == 0) { 

      //analogWrite(SPEAKER,vol);

      //OCR2B = pgm_read_byte(&idle_data[j]);
      ++j;

      if (j >= idle_len ) {

      j = 0; 

      }
 
  }

   if ((curEngineSample) % 2 != 0) { 

      //analogWrite(SPEAKER,vol2);
      

      OCR2B = pgm_read_byte(&idle_data2[i]);
       ++i;

       if (i >= idle_len ) {

      i = 0; 

       }

        
  }   

      
      ++curEngineSample;

      
      
}

Hi tkaracali,

i'm not an expert about AVR-timers nor for ESP32-timers.
Your sourcefile has a part

void startPlayback()
{
  pinMode(SPEAKER, OUTPUT);
  audioRunning = true;

There is a reason why questions should include not the shortest question possible but quite a lot backround information. From this small codesnippet I have no idea which library you are using.
Now I have to ask for these things:

So I guess your project wants to playback audio from somewhere.
For the arduino-IDE there are so many ready to use libraries that there is a good chance to find a library that does the same thing and is usable with ESP32-cores. On such a low-level like hardware-timers there are a lot of differencies between different microcontrollers. For sure it is not done with exchangeing some names.

Have you ever looked up the library-manager if keyword "ESP32" if there is an equalent ESP32-library?

Inside the arduino-IDE in the main-menu click on "tools" and choose "library-manager"

Another approach: if you write about the whole project that uses this StartPlayback-function maybe another and easier to achieve solution can be suggested.

best regards

Stefan

Hello Stefan,

Thank you for your answer.

The main thing i want to do is playing two wav files (which are converted into hex) together or overlap with %50 maybe.

Other part of it is while playing two wav files repeatedly, i want to change the pitch of them with potentiometre.( I can do it now)

Lastly, while pitch is rising i want one wav file volume to decrease and other one increase according to pitch value. Like when one song fading out while other fading in.

This is a summary of my project. I can do most of the stuff at arduino but now i want to do it in ESP32.

Best Regards
Tunç

Are there anything like OCR2B or OCR1A in ESP32?

Yes, but they function differently and have different names. The code that uses them would have to be completely re-written to work on ESP32.