Program using atmega8 not working

I have the code which measures frequency ...which runs using Arduino Uno, but when i compile selecting Atmega8 from Minicore library its says TCCR2A not declared.
The program is:

#include <Arduino.h>
#include <avr/io.h>
#include <Wire.h>
volatile unsigned long totalCounts;
volatile bool nextCount;
volatile unsigned long Timer1overflowCounts;
volatile unsigned long overflowCounts;
unsigned int counter, countPeriod;
//=================================================================
void setup()
{
Serial.begin(9600);

}
//=================================================================
void loop()
{
startCount(1000);
while(!nextCount) {}
Serial.println(totalCounts);
}

//=================================================================
ISR(TIMER1_OVF_vect)
{
Timer1overflowCounts++;
}
//=================================================================
ISR (TIMER2_COMPA_vect)
{
overflowCounts = Timer1overflowCounts;
counter++;
if(counter < countPeriod) return;

TCCR1A = 0; TCCR1B = 0; //Timer 1 reset
TCCR2A = 0; TCCR2B = 0; //Timer 2 reset
TIMSK1 = 0; TIMSK2 = 0; //Timer 1 & 2 disable interrupts

totalCounts = (overflowCounts * 65536) + TCNT1;
nextCount = true;
}
//=================================================================
void startCount(unsigned int period)
{
nextCount = false;
counter = 0;
Timer1overflowCounts = 0;
countPeriod = period;

//Timer 1: overflow interrupt due to rising edge pulses on D5
//Timer 2: compare match interrupt every 1ms
noInterrupts();
TCCR1A = 0; TCCR1B = 0; //Timer 1 reset
TCCR2A = 0; TCCR2B = 0; //Timer 2 reset
TIMSK1 |= 0b00000001; //Timer 1 overflow interrupt enable
TCCR2A |= 0b00000010; //Timer 2 set to CTC mode
OCR2A = 124; //Timer 2 count upto 125
TIMSK2 |= 0b00000010; //Timer 2 compare match interrupt enable
TCNT1 = 0; TCNT2 = 0; //Timer 1 & 2 counters set to zero
TCCR2B |= 0b00000101; //Timer 2 prescaler set to 128
TCCR1B |= 0b00000111; //Timer 1 external clk source on pin D5
interrupts();
}

How many timers does the ATMega8 have?

It has three timers....same as Atmega328
Two 8 bit and one 16 bit

It has three timers, but not the same as the 328.
The datasheet is always your best friend.

Note also that you cannot read a 32 bit variable atomically, so in non-interrupt context, you should only access the counter variable with interrupts disabled.

But the error is showing in TCCR2A not bring declared.
Im not an expert in register level coding....so from where should i start...?

Please edit your posts to add code tags. For posting instructions, see the "How to get the best out of this forum" post, linked at the head of every forum category.

Download the ATmega8 data sheet and go to the timer section. There you will see that Timer2 does not have the TCCR2A register, only TCCR2. You will need to read that section carefully to see if the ATmega8 Timer2 can do what you want it to do.

Because the ATMega8 doesn't have a TCCR2A register.

ok ...i will thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.