Frequency Counter using ATMega8A, 7 Segment display keeps flickering only shows 1 out of 4 digits

I am trying the following code to read from the ArduinoPreformatted text digital pin 2 for my frequency counter, but it doesn't display the data properly on my 4 digit 7 segments. These are connected in multiplexing.
Only 1 digit is being displayed and the rest keep on flickering.

// Declare the pins for the segments:
int a = 3;
int b = 4;
int c = 5;
int d = 6;
int e = 7;
int f = 8;
int g = 9;
int dp = 10;

// Declare the pins for the digits:
int d1 = A3;
int d2 = A2;
int d3 = A1;
int d4 = A0;

// Control variables:
int refRate = 10; //Controls the refresh rate of the display
int rCount;

int freqCounter = 0;
long preMillis = 0;

void isr() //interrupt service routine
{
  freqCounter++;
}

void setup()
{
// Set all the pins of the LEDs to output:
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
pinMode(dp, OUTPUT);
pinMode(d1, OUTPUT);
pinMode(d2, OUTPUT);
pinMode(d3, OUTPUT);
pinMode(d4, OUTPUT);
displayNumber();
attachInterrupt(0, isr, RISING);
}

void loop()
{
while((millis()-preMillis)<1000);
 detachInterrupt(0);           //detaches the interrupt
 displayNumber();
 freqCounter = 0;
 preMillis = millis();
 attachInterrupt(0,isr,RISING);  //attaching the interrupt again
}

void displayNumber()
{
pickDigit(3);
pickNumber(freqCounter / 1000);
delay(5);

pickDigit(2);
pickNumber((freqCounter % 1000)/100);
delay(5);

pickDigit(1);
pickNumber((freqCounter % 100)/10);
delay(5);

pickDigit(0);
pickNumber(freqCounter % 10);
delay(5);
}

// Function to select the LED
void pickDigit(int x)
{
// Initially turn all four LEDs off:

digitalWrite(d4, LOW);
digitalWrite(d3, LOW);
digitalWrite(d2, LOW);
digitalWrite(d1, LOW);

switch(x)
   {
   case 0:
   digitalWrite(d4, HIGH);
   digitalWrite(d3,LOW);
   digitalWrite(d2,LOW);
   digitalWrite(d1,LOW);
   break;
   case 1:
   digitalWrite(d3, HIGH);
   digitalWrite(d2,LOW);
   digitalWrite(d1,LOW);
   digitalWrite(d4,LOW);
   break;
   case 2:
   digitalWrite(d2, HIGH);
   digitalWrite(d3,LOW);
   digitalWrite(d4,LOW);
   digitalWrite(d1,LOW);
   break;
   default:
   digitalWrite(d1, HIGH);
   digitalWrite(d3,LOW);
   digitalWrite(d2,LOW);
   digitalWrite(d4,LOW);
   break;
   }
}


// Function to control number displayed on each LED. Here x is the number to be displayed:
void pickNumber(int x)
{
   switch(x)
   {
      default:
      zero();
      break;
      case 1:
      one();
      break;
      case 2:
      two();
      break;
      case 3:
      three();
      break;
      case 4:
      four();
      break;
      case 5:
      five();
      break;
      case 6:
      six();
      break;
      case 7:
      seven();
      break;
      case 8:
      eight();
      break;
      case 9:
      nine();
      break;
   }
}

// Function to clear the LEDs:
void clearLEDs()
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 0:
void zero()
{
digitalWrite(a,!HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,!HIGH);
digitalWrite(e,!HIGH);
digitalWrite(f,!HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 1:
void one()
{
digitalWrite(a,HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 2:
void two()
{
digitalWrite(a,!HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,!HIGH);
digitalWrite(e,!HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,!HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 3:
void three()
{
digitalWrite(a,!HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,!HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,!HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 4:
void four()
{
digitalWrite(a,HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,!HIGH);
digitalWrite(g,!HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 5:
void five()
{
digitalWrite(a,!HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,!HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,!HIGH);
digitalWrite(g,!HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 6:
void six()
{
digitalWrite(a,!HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,!HIGH);
digitalWrite(e,!HIGH);
digitalWrite(f,!HIGH);
digitalWrite(g,!HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 7:
void seven()
{
digitalWrite(a,!HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}
// Function to display 8:
void eight()
{
digitalWrite(a,!HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,!HIGH);
digitalWrite(e,!HIGH);
digitalWrite(f,!HIGH);
digitalWrite(g,!HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 9:
void nine()
{
digitalWrite(a,!HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,!HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,!HIGH);
digitalWrite(g,!HIGH);
digitalWrite(dp,HIGH);
}

Well one thing I notice is not declaring freqCounter volatile - essential whenever a variable is used in ISR and main program:

volatile int freqCounter = 0;   // must be volatile if used in both ISR and main code.

Then your are using detachInterrupt() - almost never a good idea, this will lead to false triggering of interrupts in some circumstances. However you must use a critical section for talking to the ISR via variables:

void loop()
{
  noInterrupts () ; // read and reset counter in critical section
  int frequency = freqCounter ;
  freqCounter = 0;
  interrupts () ;

  preMillis = millis();  // setup time for next sampling

  while((millis()-preMillis)<1000)
    displayNumber (frequency) ;  // while waiting multiplex the display constantly.
}

Note that the multiplexing has to keep running constantly, otherwise only one digit will be lit. So I've moved the multiplexing into the wait loop.

Also you need to switch off all the LEDS before changing digits or you'll ghost the previous digit value on the new position:


void displayNumber(int frequency)
{
  for (byte i = 0 ; i < 4 ; i++)  // display trailing digit first so can do %, / loop
  {
    pickDigit (i) ;
    pickNumber(frequency % 10);
    frequency /= 10 ;
    delay(1);
  }
}

// Function to select the LED
void pickDigit(int x)
{
  // Initially turn _all_ lines off to avoid ghosting
  clearLEDs () ;
  digitalWrite(d4, LOW);
  digitalWrite(d3, LOW);
  digitalWrite(d2, LOW);
  digitalWrite(d1, LOW);

  // select digit (still dark)
  switch(x)
    {
    case 0:
      digitalWrite(d4, HIGH);
      break;
    case 1:
      digitalWrite(d3, HIGH);
      break;
    case 2:
      digitalWrite(d2, HIGH);
      break;
    default:
      digitalWrite(d1, HIGH);
      break;
    }
}

I've speeded up the multiplexing to make it less flickery (delay(1)), which ought to help with timing accuracy too. The digits are scanned least significant first as that's an easy look using repeated division by 10. Added for loops whenever possible to avoid code duplication.

Note I've tidied up pickDigit() to avoid duplicate actions on the digit lines, and made sure it clears the segments before changing digits.

Can you post a schematic showing how you wired it showing all power and ground connections, not a frizzy picture. Also post links to technical information on the hardware devices. Azon is generally useless it is sales information.

Thanks a lot sir !
This really solved my problem, but there is another issue I am not able to sample more than 100KHz on my controller. I am using external 16MHz clock.

/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 9/8/2012
Author  : sherif
Company : 
Comments: 


Chip type           : ATmega8
Program type        : Application
Clock frequency     : 8.000000 MHz
Memory model        : Small
External RAM size   : 0
Data Stack size     : 256
*****************************************************/

#include <mega8.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
#include <delay.h>
#include <stdlib.h>  // this library is used to display variables on lcd

unsigned long int freq;  // to store value of frequency value
unsigned int i=0,dur; //i=number of overflows in one second
// dur to store the value of TCNT1 register
char buffer[8]; // to store the frequency value as a string to be displayed on lcd
float freqf; 
// used to display the fractions of frequency with the suitable unit as shown later

// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
i++ ;      // count the number of overflows in one second
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x04;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// LCD module initialization
lcd_init(16);

// Global enable interrupts
#asm("sei")

while (1)
      {
      // Place your code here
      TIMSK=0x04;
      TCCR1B=0x07; 
      delay_ms(1000);
      TCCR1B=0x00;  
      TIMSK=0x00;
      dur=TCNT1;
      freq = dur + i*65536;
      TCNT1=0x0000;
      i=0; 
      lcd_gotoxy(0,0);
      lcd_putsf("freq=");
      lcd_gotoxy(0,1);
      if(freq>=1000000)
      {
      freqf=(float)freq/1000000;
      ftoa(freqf,3, buffer);
      lcd_puts(buffer); 
      lcd_putsf("MHZ");
      }
      else if (freq>=1000)
      {
      freqf=(float)freq/1000;
      ftoa(freqf,3, buffer);
      lcd_puts(buffer); 
      lcd_putsf("KHZ");
      }
      else
      {
      ltoa(freq, buffer);
      lcd_puts(buffer); 
      lcd_putsf("HZ");
      }    
      };
}

Also I found this code online which uses Timer1 on Digital Pin 5 of Atmega8a. Is there a way to make it compatible with arduino ide.

I am providing the TTL Signal to Digital Pin 5 of my Arduino with ATMega8A attached to it. This code works fine with an LCD module but doesn't work in Arduino IDE. I need to see the Serial Output as well but cannot figure out how to convert the existing code for Arduino IDE.

/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 9/8/2012
Author  : sherif
Company : 
Comments: 


Chip type           : ATmega8
Program type        : Application
Clock frequency     : 8.000000 MHz
Memory model        : Small
External RAM size   : 0
Data Stack size     : 256
*****************************************************/

#include <mega8.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
#include <delay.h>
#include <stdlib.h>  // this library is used to display variables on lcd

unsigned long int freq;  // to store value of frequency value
unsigned int i=0,dur; //i=number of overflows in one second
// dur to store the value of TCNT1 register
char buffer[8]; // to store the frequency value as a string to be displayed on lcd
float freqf; 
// used to display the fractions of frequency with the suitable unit as shown later

// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
i++ ;      // count the number of overflows in one second
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x04;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// LCD module initialization
lcd_init(16);

// Global enable interrupts
#asm("sei")

while (1)
      {
      // Place your code here
      TIMSK=0x04;
      TCCR1B=0x07; 
      delay_ms(1000);
      TCCR1B=0x00;  
      TIMSK=0x00;
      dur=TCNT1;
      freq = dur + i*65536;
      TCNT1=0x0000;
      i=0; 
      lcd_gotoxy(0,0);
      lcd_putsf("freq=");
      lcd_gotoxy(0,1);
      if(freq>=1000000)
      {
      freqf=(float)freq/1000000;
      ftoa(freqf,3, buffer);
      lcd_puts(buffer); 
      lcd_putsf("MHZ");
      }
      else if (freq>=1000)
      {
      freqf=(float)freq/1000;
      ftoa(freqf,3, buffer);
      lcd_puts(buffer); 
      lcd_putsf("KHZ");
      }
      else
      {
      ltoa(freq, buffer);
      lcd_puts(buffer); 
      lcd_putsf("HZ");
      }    
      };
}

If you want to display your results on the serial monitor, you should put some extra code in your sketch.
Also your sketch is missing a setup function and a loop function.
Open a new file in IDE. That will give you the correct base structure.

In setup put :
Serial.begin(9600);

In loop of your sketch write everything to serial monitor:
Serial.println("whatever");

Or


char sometext[] = "blabla";
Serial.println(sometext);

loop() can replace your while(1) statement.
Hope this will get you started

I tried that already but the arduino ide does not recognise

interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
i++ ;      // count the number of overflows in one second
}

Then you will have to look into how an interrupt procedure should be made in arduino IDE and how the timer registers can be accessed.
You should also add
#include <arduino.h>
This is not easy stuff... did you write your sketch yourself or did you 'borrow' it? You really need to know what is going on to convert this code...
Can you add serial output in your other ODE and then monitor with aduino monitor? Or any random serial monitor?

Lookup Arduino ISR for Interrupt Service Handler syntax and semantics.

You did not explain what your code is measuring. Have you seen the timer Input Capture feature for exact time stamps without using interrupts?

You could try replacing this:

with:

ISR(TIMER1_OVF_vect)

to see if you get any further.

Ideally, the variable 'i' would be declared additionally as volatile:

But it has a main, so that's OK.

1 Like

==> while(1){} block/section

In Arduino IDE, we don't have explicit main() function! We have only the setup(), loop(), and UDFs.

My corrections in bold

1 Like

You are a linguist! Salute!!

I did not know that... learned something today!

I have changed the code a bit for Arduino IDE but still the serial monitor is showing 1.0 value. I am using Digital Pin 5 on Atmega8 to read the frequency of a square wave.

/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 9/8/2012
Author  : sherif
Company : 
Comments: 


Chip type           : ATmega8
Program type        : Application
Clock frequency     : 8.000000 MHz
Memory model        : Small
External RAM size   : 0
Data Stack size     : 256
*****************************************************/



unsigned long int freq;  // to store value of frequency value
volatile unsigned int i=0,dur; //i=number of overflows in one second
// dur to store the value of TCNT1 register
char buffer[8]; // to store the frequency value as a string to be displayed on lcd
float freqf; 
// used to display the fractions of frequency with the suitable unit as shown later

// Timer 1 overflow interrupt service routine
ISR(TIMER1_OVF_vect)
{
// Place your code here
i++ ;      // count the number of overflows in one second
}

// Declare your global variables here

void setup()
{
  Serial.begin(9600);
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x04;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
}

void loop()
      {
        noInterrupts();
        float f = i;
        i=0;
        interrupts();
        int preMillis = millis();
        while(TCNT1<1000)
        Serial.println(f);
        TIMSK=0x04;
        TCCR1B=0x07; 
        delay(1000);
        TCCR1B=0x00;  
        TIMSK=0x00;
        dur=TCNT1;
        freq = dur + i*65536;
        TCNT1=0x0000;
        i=0; 
   
         
}

Hi everyone, I need help in making a frequency counter that can measure the square wave signals upto 1MHz, currently I have a code that can measure only around 110KHz. This code used the Digital Pin 2 of the Arduino.

Please note that I am using Atmega8a with arduino not an Atmega328.

I have already used the freqcount library but that library is not compatible with Atmega8a.

The code I am using currently on Digital Pin 2 :

#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object

volatile float freqCounter = 0.000;  
float preMillis = 0.000;
int indicator=A0;


void isr() //interrupt service routine
{
  freqCounter++;
}

void setup()
{
  Serial.begin(9600);
  pinMode(indicator,OUTPUT);
  byte numDigits = 3;
  byte digitPins[] = {A3, A2, A1};
  byte segmentPins[] = {3,4,5,6,7,8,9,10};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_ANODE; // See README.md for options
  bool updateWithDelays = false; // Default 'false' is Recommended
  bool leadingZeros = true; // Use 'true' if you'd like to keep the leading zeros
  bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected
  
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
  updateWithDelays, leadingZeros, disableDecPoint);
  sevseg.setBrightness(45);

displayNumber(freqCounter);
delay(10);
attachInterrupt(0, isr, RISING);
}

void loop()
{
  noInterrupts () ; // read and reset counter in critical section
  float frequency = freqCounter;
  freqCounter = 0;
  interrupts () ;

  preMillis = millis();  // setup time for next sampling

  while((millis()-preMillis)<1000)
    displayNumber(frequency);   // while waiting multiplex the display constantly.
}

void displayNumber(float frequency)
{
  if (frequency < 1000)
  {
    sevseg.setNumber(frequency);
    
  }
  else if (frequency >= 1000.00 && frequency <10000.00)
  {
    sevseg.setNumberF(frequency/1000,2);
    
  }
  else if (frequency >=10000.00 && frequency <100000.00)
  {
    sevseg.setNumberF(frequency/1000,1);
    
  }
  else if (frequency >=100000.00 && frequency <540000.00)
  {
    sevseg.setNumber(frequency/1000);
  }
  else
  {
    sevseg.setChars("OVA");
  }
  Serial.println(frequency);
sevseg.refreshDisplay();
}

This makes millis() and micros() stay at their initial value 0. You can not sample any more anything at a specific time interval.

You better keep your hands off of parts of the hardware which you don't understand or don't use.

Could try a library ... FreqCount: best for 1 kHz to 8 MHz. (code)