I'm having some troubles using more than one timer with my ESP32. The second interrupt works well, but I seem to be accessing the first one only one time. It must be some variable I'm forgetting to update, but I just can't get it to work.
It seems as if interruptCounter isn't increasing again, but I don't know why it wouldn't.
I attach the relevant parts of the code:
Declarations/Initializations:
Code: Select all
volatile int interruptCounter;
int totalInterruptCounter;
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
volatile int interruptCounter2;
int totalInterruptCounter2;
hw_timer_t * timer2 = NULL;
portMUX_TYPE timerMux2 = portMUX_INITIALIZER_UNLOCKED;
volatile int interruptCounter;
int totalInterruptCounter;
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR onTime(){
portENTER_CRITICAL_ISR(&timerMux);
interruptCounter++;
portEXIT_CRITICAL_ISR(&timerMux);
}
volatile int interruptCounter2;
int totalInterruptCounter2;
hw_timer_t * timer2 = NULL;
portMUX_TYPE timerMux2 = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR onTime2(){
portENTER_CRITICAL_ISR(&timerMux2);
interruptCounter2++;
portEXIT_CRITICAL_ISR(&timerMux2);
}
Code: Select all
pinMode(pinSend, OUTPUT);
timer = timerBegin(0,80,true); //1MHz, prescaler de 80
timerAttachInterrupt(timer, &onTime, true);
timerAlarmWrite(timer, 10, true); //interrumps every 10 micros
timerAlarmEnable(timer);
timer2 = timerBegin(1,80,true); //1MHz, prescaler de 80
timerAttachInterrupt(timer2, &onTime2, true);
timerAlarmWrite(timer2, 501, true); //interrumpe every 501 micros
timerAlarmEnable(timer2);
Code: Select all
if (interruptCounter2 > 0) { //this one works ok!!
if (flag_pulse == 1){
portENTER_CRITICAL(&timerMux2);
interruptCounter2--;
portEXIT_CRITICAL(&timerMux2);
totalInterruptCounter2++;
if (totalInterruptCounter2 < lastDuty) {
digitalWrite(pinSend, HIGH);
}
else {
digitalWrite(pinSend, LOW);
if (totalInterruptCounter2 = 20)
totalInterruptCounter2 = 0;
}}
}
if (interruptCounter > 0) { //this only works one time!
if (flag_data){
portENTER_CRITICAL(&timerMux);
interruptCounter--;
portEXIT_CRITICAL(&timerMux);
totalInterruptCounter++;
flag_samples++;
data[flag_samples] = analogRead(34);
Serial.println(totalInterruptCounter); //for debug purpose only, it only prints once...
if (totalInterruptCounter = 500){
flag_send = 1;
flag_data = 0;
flag_pulse = 0;
totalInterruptCounter = 0;
flag_samples = 0;
}
}
//Send data via BLE
if (flag_send) { //this works ok! although it's sending empty data because it doesn't read the pin
for (int k = 0; k< samples; k++) {
Serial.println((3.3*(data[k]).toInt()/4096));
voltage[k] = String(100-((3.3*(data[k]).toInt()/4096))*20);
pCharacteristic->setValue(voltage[k].c_str());
pCharacteristic->notify();
delay(3);
}
flag_send = 0;
}
}
PS. For a brief explanation of the code, with timer2 I generate pulse trains with different frequencies. With timer I analogread the response. I could read the response just fine, until I added the other interrupt.
If anyone can guide me in the right direction, that would be really helpful!
Thanks in advance