Hi,
Just started playing with some code on my mini and trying to understand how interrupt timers work, along with delaymicroseconds() and micros().
I found the class FrequencyTimer2 and coded up a quick test:
#include <FrequencyTimer2.h>
int ledPin = 13;
int state = 0;
unsigned long period;
unsigned long mics1;
void timeO() {
mics1 = micros();
state = !state;
digitalWrite(ledPin, state);
period = FrequencyTimer2::getPeriod();
Serial.print(" state ");
Serial.print(state);
Serial.print(" period ");
Serial.print(period);
Serial.print(" mics ");
Serial.println(mics1);
}
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("start");
FrequencyTimer2::setPeriod(200000);
FrequencyTimer2::enable();
FrequencyTimer2::setOnOverflow(timeO);
}
void loop()
{
}
In the timer interrupt routine, I make a call to micros() and also to getPeriod() and print the values returned by each of these.
First, I would have expected the value returned by getPeriod to be 20,000, instead of 32K.
Secondly, I would have expected the value returned by micros() to increase with every call as it should be returning the time in uS since start. Subtracting the previous from the current value should give something approximating the return value from getPeriod (plus code execution time). It fact it looks like getPeriod is resetting this value.
Can anyone help with this please?
Thanks
Phil
PS: these are the println results:
state 1 period 32768 mics 5588
state 0 period 32768 mics 7128
state 1 period 32768 mics 6632
state 0 period 32768 mics 7160
state 1 period 32768 mics 6664
state 0 period 32768 mics 6168
state 1 period 32768 mics 6696
state 0 period 32768 mics 6200
state 1 period 32768 mics 6728
state 0 period 32768 mics 6232
state 1 period 32768 mics 6760
state 0 period 32768 mics 6264
state 1 period 32768 mics 6792
state 0 period 32768 mics 6296