Cattledog ..... your code works very nicely on my mega 2560 (as was expected!)
I'll paste your code (with the timer numbers and pin numbers in some comments for aligning with the mega 2560).
I'll also paste some results from the serial monitor.
cattledog:
Using the external clock source as a high speed rpm counter is very much recommended when interrupts techniques start to fail.
A very significant and great recommendation cattledog!
unsigned long count_period = 2000; // units of millisec
unsigned int count_to_rpm = 60000UL / count_period; //convert counts to RPM
byte number_of_sensors = 1;
unsigned long final_counts;
unsigned long start_time = millis();
unsigned long measured_time;
unsigned long current_time = millis();
void setup()
{
Serial.begin(115200);
TCCR5A = 0; //initialize Timer5
TCCR5B = 0;
TCNT5 = 0;
pinMode( 47, INPUT_PULLUP); //external source pin for timer 5 of MEGA 2560
//set external clock source pin T5 (digital pin 47) to clock on a rising edge
TCCR5B = bit (CS50) | bit (CS51) | bit (CS52);
//Test pulses to confirm circuit 50 pps = 3000
pinMode(11, OUTPUT);
tone(11, 50);//test pulse jumper pin 11 to pin 47 of MEGA 2560
}
void loop()
{
current_time = millis();
if (current_time - start_time >= count_period)
{
TCCR5B = 0; //stop counter
final_counts = TCNT5; //frequency limited by unsigned int TCNT5 without rollover counts
TCNT5 = 0;
measured_time = current_time - start_time;
start_time = current_time;;
unsigned int rpm = (final_counts * count_to_rpm) / number_of_sensors;
TCCR5B = bit (CS50) | bit (CS51) | bit (CS52); //restart external clock source
Serial.print(measured_time);
Serial.print("\t\t");
Serial.print(final_counts);
Serial.print("\t\t");
Serial.println(rpm);
}
}
2000 101 3030
2000 100 3000
2000 100 3000
2000 100 3000
2000 100 3000
2000 100 3000
2000 101 3030
2000 100 3000
2000 100 3000