Go Down

Topic: error in Nick Gammon 'Frequency Timer' sketch? (Read 207 times) previous topic - next topic

iunnius

Aug 08, 2014, 06:49 pm Last Edit: Aug 09, 2014, 10:39 am by robtillaart Reason: 1
Code: [Select]
// Frequency timer
// Author: Nick Gammon
// Date: 10th February 2012

// Input: Pin D2

volatile boolean first;
volatile boolean triggered;
volatile unsigned long overflowCount;
volatile unsigned long startTime;
volatile unsigned long finishTime;

// here on rising edge
void isr ()
{
 unsigned int counter = TCNT1;  // quickly save it
 
 // wait until we noticed last one
 if (triggered)
   return;

 if (first)
   {
   startTime = (overflowCount << 16) + counter;
   first = false;
   return;  
   }
   
 finishTime = (overflowCount << 16) + counter;
 triggered = true;
 detachInterrupt(0);  
}  // end of isr

// timer overflows (every 65536 counts)
ISR (TIMER1_OVF_vect)
{
 overflowCount++;
}  // end of TIMER1_OVF_vect


void prepareForInterrupts ()
 {
 // get ready for next time
 EIFR = bit (INTF0);  // clear flag for interrupt 0
 first = true;
 triggered = false;  // re-arm for next time
 attachInterrupt(0, isr, RISING);    
 }  // end of prepareForInterrupts
 

void setup ()
 {
 Serial.begin(115200);      
 Serial.println("Frequency Counter");
 
 // reset Timer 1
 TCCR1A = 0;
 TCCR1B = 0;
 // Timer 1 - interrupt on overflow
 TIMSK1 = bit (TOIE1);   // enable Timer1 Interrupt
 // zero it
 TCNT1 = 0;  
 overflowCount = 0;  
 // start Timer 1
 TCCR1B =  bit (CS20);  //  no prescaling

 // set up for interrupts
 prepareForInterrupts ();  
 
 } // end of setup

void loop ()
 {

 if (!triggered)
   return;

 unsigned long elapsedTime = finishTime - startTime;
 float freq = F_CPU / float (elapsedTime);  // each tick is 62.5 nS at 16 MHz
 
 Serial.print ("Took: ");
 Serial.print (elapsedTime);
 Serial.print (" counts. ");

 Serial.print ("Frequency: ");
 Serial.print (freq);
 Serial.println (" Hz. ");

 // so we can read it  
 delay (500);

 prepareForInterrupts ();  
}   // end of loop



In void setup:
TCCR1B =  bit (CS20);  //  no prescaling,
The error is that CS20 is in TCCR2B, not 1B.
Because we have used TCCR1B = 0 in a previous step, to stablish any prescaling in TCCR1B, CS10, CS11, and CS12 should be used.

update moderatore: removed snippet to make question more explicit

robtillaart

Not dived into the timer details, but I expect CS20 is just a value to get a bit set and works for both timers.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up