1

I have a development board of atmega128L. I have configured its timer1 in CTC mode and Timer 0 in overflow mode with the interrupts of both the timers enabled. My CPU clock frequency is 8Mhz. I am using two counter variables for both the timers which will track how many times both the timers execute their isr and based on that i am taking an action in my main code. based on the value with which i am comparing counter variable of timer 0 , it should take action after every 16 seconds. But it is taking an action after every 4 seconds. I have tried with different prescaler I have been able to take action for timer1 Successfully . The Fuse Byte settings are High:1F, Low:E4, Extended: FF I have attached a relevant portion of my code for reference:

int main()
{
    cli();
    timer1_init();
    timer0_init();
    sei();

    while (1) 
    {   
        if(meter_interrupt_flag==1)
        {
            meter_interrupt_flag=0;
            transmitData(meter_interrupt_msg,strlen(meter_interrupt_msg));    
            //send some string "reading meter"

        }
        if(connection_interrupt_flag==1);
        {
            connection_interrupt_flag=0;
            checkNetworkConnection();
            transmitData(connection_interrupt_msg,strlen(connection_interrupt_msg));
            //send some string "connection interrupt"
        }
    }   
}

void timer1_init(void)
{
    overflow_counter=0;

    // set up timer with prescaler = 1024
    TCCR1B |= (1 << CS12) |(1 << CS10) | (1 << WGM12);

    // initialize counter
    TCNT1 = 0;
    OCR1A = 39062;
    TIMSK |= (1 << OCIE1A);
}

void timer0_init(void)
{
    overflow_counter_3=0;

    TCCR0 |= (1 << CS02) |(1 << CS01);        //prescalar 256
    TCNT0 = 0;
    TIMSK |= (1 << TOIE0);
}

ISR(TIMER1_COMPA_vect)
{
    overflow_counter++;

    if(overflow_counter>=12)
    {
        meter_interrupt_flag=1;
        overflow_counter=0;
    }
}

ISR(TIMER0_OVF_vect)
{
    overflow_counter_3++;
    if(overflow_counter_3>=4000)
    {
        connection_interrupt_flag=1;
        overflow_counter_3=0;
    }
}
Gaurav Pathak
  • 1,065
  • 11
  • 28
Prasanna
  • 47
  • 2

0 Answers0