I'm trying to get my head around how the sparkfun frequency counter code works and it's doing my head in.
Reading through it has me understanding that it's doing a lot of counting with timer 1 yet somehow it's using pin 5 to trigger everything and for the life of me I cannot see where pin 5 which is associated with timer 0 actually fits into the code.
The following snippet in the code would indicate that this is where pin 5 is being worked into the mix
// External clock source on D5, trigger on rising edge:
TCCR1B = (1<<CS12) | (1<<CS11) | (1<<CS10);
But from what I can gather TCCR1B, CS12, CS11 and CS10 are all related to timer 1 and would have no relationship to pin5
Ultimately what I want to do is move the input pin from pin 5 to one of my free pins (0 to 4 and 11 to 13) but even if it's not actually possible I still cannot see how it's working from pin 5 to see how or why it wouldn't work off another pin.
/*
Frequency Counter Arduino Sketch
by: Jim Lindblom
SparkFun Electronics
License: Beerware
This isn't the most accurate frequency counter, but it's simple to
program and understand, and it works in a pinch. I've tested it to
be accurate to at least 96% (usually around 99.5%). It should be
able to measure frequencies up to about 6.5MHz(8MHz optimally)
The circuit: Powered at 5V (5V LCD), Arduino running at 16MHz
D2 - RS (LCD)
D3 - R/W (LCD)
D4 - E (LCD)
D5 - Frequency input
D6 - DB4 (LCD)
D7 - DB5 (LCD)
D8 - DB6 (LCD)
D9 - DB7 (LCD)
D10 - Gate of NPN transistor (Collector tied to 5V, emitter tied to LCD backlight pin)
For the LiquidCrystal library, much thanks to:
David A. Mellis
Limor Fried (http://www.ladyada.net)
Tom Igoe
*/
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 6, 7, 8, 9);
int brightness;
unsigned int tovf1 = 0;
unsigned long frequency = 0;
unsigned long temp = 0;
// Timer 1 is our counter
// 16-bit counter overflows after 65536 counts
// tovfl will keep track of how many times we overflow
ISR(TIMER1_OVF_vect)
{
tovf1++;
}
void setup() {
pinMode(5, INPUT); // This is the frequency input
pinMode(10, OUTPUT); // Backlight control pin
digitalWrite(10, HIGH); // Turn backlight on
// Timer 1 will be setup as a counter
// Maximum frequency is Fclk_io/2
// (recommended to be < Fclk_io/2.5)
// Fclk_io is 16MHz
TCCR1A = 0;
// External clock source on D5, trigger on rising edge:
TCCR1B = (1<<CS12) | (1<<CS11) | (1<<CS10);
// Enable overflow interrupt
// Will jump into ISR(TIMER1_OVF_vect) when overflowed:
TIMSK1 = (1<<TOIE1);
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
// Print a splash screen to the LCD.
lcd.print("FrequencyCounter");
lcd.setCursor(0, 1);
lcd.print(" v1.0 ");
delay(2000);
}
void loop() {
// Delay a second. While we're delaying Counter 1 is still
// reading the input on D5, and also keeping track of how
// many times it's overflowed
delay(1000);
lcd.clear();
frequency = (TCNT1H<<8)|TCNT1L;
frequency = (TCNT1H<<8)|TCNT1L;
// Correct weird counter bug
// A small range of frequencies (~30k-50k) are getting
// 42949 appended to the front of them
// Will look into this but this works for now
if (frequency > 40000000)
frequency -= 4294900000;
// 65536 (2^16) is maximum of counter
// We'll multiply that by how many times we overflowed
temp = 65536*(unsigned long)tovf1;
// Add the overflow value to frequency
frequency += temp;
// Print the proper amount of spaces to make it look pretty
lcd.setCursor(0,1);
if (frequency < 100)
lcd.print(" ");
else if (frequency < 10000)
lcd.print(" ");
else if (frequency < 1000000)
lcd.print(" ");
else
lcd.print(" ");
lcd.print(frequency);
lcd.print(" Hz");
// Reset all counter variables and start over
TCNT1H = 0;
TCNT1L = 0;
tovf1 = 0;
}