Hi again cattledog!
Getting closer to the reason now ..... which relates to what you already mentioned about software timing.
The serial comms activity/software-loop could be affecting the timing.
When I run the code with a few serial.print commands for outputting results to serial monitor, I see counts ranging from 91 to 95.
The code below gives counts of 91 to 95 for 1 millisecond capture intervals at 50 kHz. Counts of approx 50 is expected.
unsigned long count_period = 1000; // units of MICROsec
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 = micros();
unsigned long measured_time;
unsigned long current_time = micros();
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, 50000);//test pulse jumper pin 11 to pin 47 of MEGA 2560
}
void loop()
{
current_time = micros();
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);
}
}
Results:
1844 91 5460
1916 95 5700
1920 95 5700
1912 95 5700
1888 93 5580
1912 93 5580
1848 93 5580
1912 95 5700
1920 95 5700
1916 94 5640
1916 95 5700
When a couple of those serial.print lines are removed, the count values become the ones we want to see ...... ie. approx. counts of 50.
unsigned long count_period = 1000; // units of MICROsec
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 = micros();
unsigned long measured_time;
unsigned long current_time = micros();
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, 50000);//test pulse jumper pin 11 to pin 47 of MEGA 2560
}
void loop()
{
current_time = micros();
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.println(final_counts);
}
}
Results:
50
50
50
50
50
50
50
50
50
50
49
50
50