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;
}