Last active
April 4, 2017 19:01
-
-
Save JMishou/0647b6af58af98671ee4d673dc72271b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Timer and Counter example | |
// Original Code @ http://www.gammon.com.au/timers | |
// Edited by Jason Mishou | |
// Input: Pin D5 | |
// This program uses Timer 1 to count input events on D5 | |
// and the output compare of timer 1 to trigger an interrupt | |
// original code is a little more accurate but monopolizes both timer 1 and timer 2. | |
// these are checked for in the main program | |
volatile unsigned long timerCounts; | |
volatile boolean counterReady = false; | |
// internal to counting routine | |
unsigned long freqStartCounting; | |
unsigned long freqStopCounting; | |
unsigned int timerTicks; | |
//Function that sets up Timer 1 as an input counter and compare interrupt | |
//This function starts the timer and enables the interrupt | |
//The ISR stops the timer and disables the interrupt. | |
void startCounting (uint16_t counts) | |
{ | |
counterReady = false; // time not up yet | |
timerTicks = 0; // reset interrupt counter | |
// reset Timer 1 and Timer 2 | |
TCCR1A = 0; | |
TCCR1B = 0; | |
counts--; | |
OCR1A = counts; // timer 1 count up to counts (zero relative!!!!) | |
TCNT1 = 0; // counter to zero | |
freqStartCounting = millis(); //timestamp start counting inputs | |
TIMSK1 = bit (OCIE1A); // interrupt on Timer 1 output compare A | |
// External clock source on T1 pin (D5). Clock on rising edge. | |
TCCR1B = bit (CS10) | bit (CS11) | bit (CS12); | |
} // end of startCounting | |
//****************************************************************** | |
// Timer1 Interrupt Service is invoked by hardware Timer 1 once the | |
// input counts have reached the value of OCR1A register | |
ISR (TIMER1_COMPA_vect) | |
{ | |
// grab counter value before it changes any more | |
freqStopCounting = millis(); | |
unsigned int timer1CounterValue; | |
timerCounts = TCNT1; // see datasheet, page 117 (accessing 16-bit registers) | |
// end of gate time, measurement ready | |
TCCR1A = 0; // stop timer 1 | |
TCCR1B = 0; | |
TIMSK1 = 0; // disable Timer1 Interrupt | |
counterReady = true; // set global flag for end count period | |
} // end of TIMER1_COMPA_vect | |
void setup () | |
{ | |
Serial.begin(115200); | |
Serial.println("Frequency Counter"); | |
} // end of setup | |
void loop () | |
{ | |
startCounting (15000); // how many input pulses to count before calculating frequency | |
while (!counterReady) | |
{ } // loop until count over | |
// adjust counts by counting interval to give frequency in Hz | |
float frq = (timerCounts * 1000.0) / (freqStopCounting-freqStartCounting); | |
Serial.print ("Frequency: "); | |
Serial.print ( frq); | |
Serial.println (" Hz."); | |
// let serial stuff finish | |
delay(200); | |
} // end of loop | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment