I am trying to understand in details the (great tutorial and) code of Nick Gammon ''Improved sketch using Timer 1'', available here: http://www.gammon.com.au/forum/?id=12075 . I post the whole code at the end of this in the case the web page would go down.
I find the code quite clear: Nick wants to estimate the value of a capacitor by measuring its characteristic discharge time. For this he uses one of the Arduino timers, together with an interrupt raised by the analog comparator. As the Arduino timer he uses is only 16 bits and he uses a pre-factor of 1 at 16MHz, he expects overflow of the timer to occur so he keeps track of the number of overflows that happened:
ISR (TIMER1_OVF_vect)
{
++overflowCount; // count number of Counter1 overflows
} // end of TIMER1_OVF_vect
However, what I do not understand is why he tries to find a 'missed overflow' in the ISR raised by the analog comparator (this is in the ISR (ANALOG_COMP_vect)
function body):
// if just missed an overflow
if ((TIFR1 & bit (TOV1)) && timer1CounterValue < 256)
overflowCopy++;
So my questions are:
How could a missed overflow happen? 'Naively' I would guess that, as soon as the analog comparator ISR is called, the timer stops incrementing so that it should not overflow after the comparator ISR fires, am I wrong?
If there is a timer overflow happening, I understand why one wants to check TIFR1 at TOV1 (this is the overflow flag, set automatically at timer overflow, which should be cleared as a side-effect of execution of
ISR(TIMER1_0VF_vect)
according to datasheet), why add also the&& timer1CounterValue < 256
in the if condition? I understand that256
is a 'small' value, any reason for this exact numerical value?
Copy of the whole code:
/*
Capacitance meter
Author: Nick Gammon
Date: 27 June 2013
Pulse pin (D2): Connect to capacitor via 10K resistor.
Reference voltage of 0.632 of output pin (pulsePin) connected to D7.
In my case I used 3.06V because I measured 4.84 on the 5V pin.
Measure pin (D6) connected to first leg of capacitor, other leg connected to Gnd.
Like this:
Capacitor to test:
D2 ----> 10K ----> D6 ----> capacitor_under_test ----> Gnd
Reference voltage:
+5V ----> 1.8K ---> D7 ---> 3.1K ----> Gnd
*/
const byte pulsePin = 2;
const float resistance = 10000.0;
volatile boolean triggered;
volatile boolean active;
volatile unsigned long timerCounts;
volatile unsigned long overflowCount;
ISR (TIMER1_OVF_vect)
{
++overflowCount; // count number of Counter1 overflows
} // end of TIMER1_OVF_vect
ISR (ANALOG_COMP_vect)
{
// grab counter value before it changes any more
unsigned int timer1CounterValue;
timer1CounterValue = TCNT1; // see datasheet, page 117 (accessing 16-bit registers)
unsigned long overflowCopy = overflowCount;
if (active)
{
// if just missed an overflow
if ((TIFR1 & bit (TOV1)) && timer1CounterValue < 256)
overflowCopy++;
// calculate total count
timerCounts = (overflowCopy << 16) + timer1CounterValue; // each overflow is 65536 more
triggered = true;
digitalWrite (pulsePin, LOW); // start discharging capacitor
}
} // end of ANALOG_COMP_vect
void setup ()
{
pinMode (pulsePin, OUTPUT);
digitalWrite (pulsePin, LOW);
Serial.begin (115200);
Serial.println ("Started.");
ADCSRB = 0; // (Disable) ACME: Analog Comparator Multiplexer Enable
ACSR = bit (ACI) // (Clear) Analog Comparator Interrupt Flag
| bit (ACIE) // Analog Comparator Interrupt Enable
| bit (ACIS0) | bit (ACIS1); // ACIS1, ACIS0: Analog Comparator Interrupt Mode Select (trigger on rising edge)
} // end of setup
void startTiming ()
{
active = true;
triggered = false;
// prepare timer
overflowCount = 0; // no overflows yet
// reset Timer 1
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0; // Counter to zero
TIFR1 = bit (TOV1); // clear overflow bit
// Timer 1 - counts clock pulses
TIMSK1 = bit (TOIE1); // interrupt on Timer 1 overflow
// get on with it
digitalWrite (pulsePin, HIGH); // start charging capacitor
// start Timer 1, no prescaler
TCCR1B = bit (CS10);
} // end of startTiming
void finishTiming ()
{
active = false;
Serial.print ("Capacitance = ");
float capacitance = (float) timerCounts * 1000.0 / 16.0 / resistance;
Serial.print (capacitance);
Serial.println (" nF");
triggered = false;
delay (3000);
} // end of finishTiming
void loop ()
{
// start another test?
if (!active)
startTiming ();
// if the ISR noted the time interval is up, display results
if (active && triggered)
finishTiming ();
} // end of loop