Dual Frequency Counters on the Mega2560?

Good Day All,

I’m seeking some guidance on how setup dual frequency counters on my Mega 2560. By using standard library examples, the hardware and code functions flawlessly. At the moment, I’m using a single counter via Frequency.h on pin 47.

There is very little out there with respect to code examples for the ATMega2560, so my studies switched to the internals of the Atmel processor to help me understand what I’m coding, and how certain commands work. Downloading the datasheet from the Atmel website was a good start, as well as the pin to header legend which I’ve linked. I’ve also mapped out the port layouts for a visual if anyone is interested.

By using the datasheet, and studying the examples of cpp files, I have successfully setup inputs on pins D47 and D38 to count two separate frequencies. The problem is, the code examples use a delay of 1000 msecs which produce a slow update in data flow.

After messing around with variables, and removing the delay, etc. I’m not able to increase the data update rate (in Serial Monitor) without effecting the frequency output.

IE: With 1000 msec delay, the output is fine (The Serial Monitor output = frequency generator). With a reduced delay of 10 msec, the output reads 1/10th the frequency generator.

I realize the one second delay is required for the timer to count “cycles per second”, and hence return the proper frequency, but that’s not suitable for my application. It appears that editing the code to remove the delay is not going to work based on how the Atmel circuitry is designed (at least with my limited knowledge). Am I stuck, or do I need to look at modifying the .h/.cpp files to allow an additional input pin to count pulses?

Thanks for any advice!

Datasheet Link:

Sample Code:

#include "FreqCounter.h"
#include <FreqMeasure.h>

#include <util/delay.h>
//#include <util/millis.h>

int timeCounter = 100, timePassed; 
unsigned long  tachometer=0;
unsigned int tovf0=0;
unsigned long temp0=0;
 
unsigned long speedometer=0;
unsigned int tovf5=0;
unsigned long temp5=0;
 
//timer 0, 8 bit counter
ISR(TIMER0_COMPA_vect) { tovf0++; }
 
//timer 5, 16 bit counter
ISR(TIMER5_OVF_vect) { tovf5++; }
 
void setup(){
                Serial.begin(57600);
                PORTD = (1 << 7);                                                                 //PD7 Pullup, Input D38
                PORTL = (1 << 2);                                                                  //PL2 Pullup, Input D47
 
                TCCR0A = 0;
                TCCR0B = (1 << CS02) | (1 << CS01) | (1 << CS00);   // Setting external clock, rising edge
                TIMSK0 = (1 << OCIE0A);                                                                   // Interrupt enable    
 
                TCCR5A = 0;
                TCCR5B = (1 << CS52) | (1 << CS51) | (1 << CS50);   // Setting external clock, rising edge
                TIMSK5 = (1 << TOIE5);                                                      // Interrupt enable    
 
 
                _delay_ms(1000);                            // One second delay?
                reset_Counters();
}
 
void loop(){
 
              _delay_ms(1000);                                           
               tachometer = TCNT0;
              speedometer = (TCNT5H << 8)|TCNT5L;
               temp0 = 256*(unsigned long)tovf0;
              temp5 = 65536*(unsigned long)tovf5;
 
              tachometer += temp0;
             speedometer += temp5;

   
               Serial.print(tachometer);
                Serial.print(',');
                Serial.println(speedometer);




}
 
void reset_Counters(){
  
                TCNT0 = 0;
                tovf0 = 0;
                TCNT5H = 0;
                TCNT5L = 0;
                tovf5 = 0;
}

Update:

I went ahead and re-wrote the .h, and .cpp files.

Essentially, all of the variables and functions were appended with "Two" where applicable.

I'm not able to get the second counter to work, and after a few hours of troubleshooting I'm
throwing up my hands for help.

Immediately I notice that my new .h file is not highlighted orange in the #include line. Why is this?
This is the same with the function calls within the code, and I suspect the cause is within the .h file?

I'm including all of the original files, and my edited files along with some screen shots.

Serial.print(frequency) outputs the correct value (688.2)
Serial.print(frequencyTwo) outputs zero (0.00)

Thank you in advance for any tips on where to look.

#include <FreqMeasure.h>
#include <FreqMeasureTwo.h>
 
void setup() {
  Serial.begin(57600);
  FreqMeasure.begin();
  FreqMeasureTwo.begin();
  
}
 
double sum=0, sumTwo=0;
int count=0, countTwo=0;
float frequency, frequencyTwo;
 
void loop() {

    if(FreqMeasure.available){
    // average several reading together
    sum = sum + FreqMeasure.read();
    count = count + 1;
    if (count > 30) {
      frequency = FreqMeasure.countToFrequency(sum / count);
      sum = 0;
      count = 0;
    }
}

  if(FreqMeasureTwo.available){
    // average several reading together
    sumTwo = sumTwo + FreqMeasureTwo.read();
    countTwo = countTwo + 1;
    if (count > 30) {
      frequencyTwo = FreqMeasureTwo.countToFrequency(sumTwo / countTwo);
      sumTwo = 0;
      countTwo = 0;
    }
  }
     Serial.print(frequency);
     Serial.print(',');
     Serial.println(frequencyTwo);
}

F2_root_folder_content.jpg

F2_util_folder_content.jpg

FreqMeasureTwo.cpp (3.45 KB)

FreqMeasureTwo.h (346 Bytes)

keywordsTwo.txt (110 Bytes)

FreqMeasureCaptureTwo.h (1.17 KB)

FreqMeasureCapture.h (1.13 KB)

FreqMeasure.cpp (2.98 KB)

FreqMeasure.h (310 Bytes)

keywords.txt (108 Bytes)

One issue fixed. The keywords were not highlighting because I named the second text file, "keywordsTwo.txt"

The IDE is searching for "keywords.txt"; I made a separate folder for my edited library and now
I see the highlighted orange function calls, etc. in the code.

Now onto getting the second counter to work! Anybody? I know this is more advanced, but there
are a few people here that are experienced in this area.