Pages: [1]   Go Down
Author Topic: FrequencyTimer2 puzzlement  (Read 460 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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(" period ");
  Serial.print(" mics ");

void setup()   {
  pinMode(ledPin, OUTPUT);    

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?


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


Waterloo, Canada
Offline Offline
Full Member
Karma: 1
Posts: 242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some guesses quickly reviewing your code and the frequencytimer2 page in the playground...

At 20,000 I'm pretty sure you've exceeded the upper bound limit of the setPeriod() member function (although the description in the playground is not as clear as it could be).

Also, you're printing out a lot of info in your interrupt routine through serial at 9600 baud.  Another interrupt gets queued long before the last interrupt gets a chance to finish... which would be related to the problem above where the interrupt happens at a frequency that is not what you intend.

Pages: [1]   Go Up
Jump to: