1

I'm trying to use a library function in my ISR function that uses the internal timer to trigger every 1 ms. The chip that I'm using is an ESP32-C3-MINI. Here is my current code:

main.cpp

#include <Arduino.h>
hw_timer_t *_timer = NULL;
myClass a();

void IRAM_ATTR isr()
{
    a.incrementCount();
}


void setup()
{
    coin_timer = timerBegin(0, 80, true);
    timerAttachInterrupt(_timer, &isr, true);
    timerAlarmWrite(_timer, 1000, true);
    timerAlarmEnable(_timer);
}

myClass.h

#include <Arduino.h>

class myClass
{
private:
    unsigned long count;

public:
    myClass();
    ~myClass();

    void incrementCount();  
}

myClass.cpp

#include <Arduino.h>
#include "myClass.h"

myClass::myClass(){

}

void myClass::incrementCount(){
    count++;
}

This is just an example of the code that I wrote. In reality, the incrementCount function is a little more complicated and involves multiple variables and arrays within the library. This current code compiles and works without an issue. However, I would like to know if this method is bad practice or can cause problems in certain situations. Secondly, I notice if I declare count variable as volatile, the MCU just crashes and enters a reboot cycle.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.