Hi There,
I wrote some code that really belongs as a separate library for a flow sensor. I use various interrupts in the code and need to set some timer registers. In particular I set:
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 16000000/1024/1;
TCCR1B |= (1 << WGM12);
TCCR1B |= 5;
TIMSK1 |= (1 << OCIE1A);
I have a private method in the library called initTimer, which sets the above. This method is called from the constructor. Presumably when I call:
flowSensor flowMonitor(flowSensorPin, 1);
initTimer sets the above registers and I should get the appropriate interrupt. In reality, Timer 1 interrupts never happen. When I change initTimer to be a public method and call it explicitly from the Setup() function everything works fine.
To play around I called initTimer from the constructor and in my Setup() function.
To help debugging I wrote a function that spits out the register values over serial. I call this immediately in Setup() after calling Serial.begin(115200) and I see that after timerInit is called from the the constructor the values don't match what they are set to in initTimer. After I call initTimer as a public method (later in Setup()) the values match and everything is fine.
Not a big deal since everything works, but I'm wondering if there is a way to set these register values from a constructor method and avoid having the extra call in Setup(). I assume that the constructor actually does set the registers to the correct values, but that there's additional arduino stuff that gets called before Setup() which resets the values to some default. Is that correct? Thanks!
D