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

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:


Code:
#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




Logged

Waterloo, Canada
Offline Offline
Full Member
***
Karma: 1
Posts: 242
Engineer
View Profile
WWW
 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.
Logged

Pages: [1]   Go Up
Jump to: