[SOLVED] Timer interrupts with ATtiny

Hello
I am trying to program an ATtiny microcontroller, but it crashes when I add any interrupt routine; for instance, I compiled with avr-gcc the following code

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

volatile uint8_t counterINT = 0;

ISR( TIM0_OVF_vect )
{
    if (++counterINT>50)
    {
        PORTA ^= 0x01;
        counterINT = 0;
    }
}

int main(void)
{
    // Timer0 1kHz (64 prescaled)
    TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00);
    TCCR0B = _BV(CS01) | _BV(CS00);
    TCNT0 = -125;
    // Timer0 overflow interrupt active
    TIMSK0 |= 1 << TOIE0;
    
    // PORTA and PORTB PIN0 output
    DDRA =  _BV(0);
    DDRB =  _BV(0);

   // Set interrupt enabled
    sei();
    
    while(1) {
		PORTB ^= 0x01;
                _delay_ms(500);
	}
    
}

If I connect two LED to PORTA.0 and PORTB.0 I should see them blinking, but all is silent. I tried to give the hex file to simavr, and I have a crash output. If i comment the ISR routine the program works (i have the PORTB LED blinking), and obviously the simulator doesn't give me any error.
Is the code wrong, or needs some special parameters to avr-gcc/avrdude to be compiled/loaded?

Why use a timer? Use millis() for that. If you do need a timer, try MsTimer2.h
It works with ATtiny, but just in milliseconds.

giuliof:

    TCCR0A = _BV(COM0A1) | _BV(COM0B1) | ...;

If I connect two LED to PORTA.0 and PORTB.0 I should see them blinking

Nope. You told it to set the outputs LOW when the timer gets a compare match.

Maybe you want to toggle them instead (or something...)

steinie44:
Why use a timer? Use millis() for that. If you do need a timer, try MsTimer2.h
It works with ATtiny, but just in milliseconds.

Nope, for my project I need the micro to call a function asynchronously than the main code.

fungus:

giuliof:

    TCCR0A = _BV(COM0A1) | _BV(COM0B1) | ...;

If I connect two LED to PORTA.0 and PORTB.0 I should see them blinking

Nope. You told it to set the outputs LOW when the timer gets a compare match.

Maybe you want to toggle them instead (or something...)

Sorry, that was a residual of a previous PWM test that worked fine. OCR0A and OCR0B should not influence the rest of the code (i think). Anyway in this code TCCR0A can be left zero, the ISR function must be called when timer register goes overflow.

Try calling it "TIMER0_OVF_vect" instead of "TIM0_OVF_vect"

fungus:
Try calling it "TIMER0_OVF_vect" instead of "TIM0_OVF_vect"

Nothing, I get this warning from avr-gcc, and when i load it I have not changes.

warning: ‘TIMER0_OVF_vect’ appears to be a misspelled signal handler

I noticed that interrupt functions makes the micro crazy: if I comment them, leaving registers inizialised, the main program is executed.

@giuliof:

Maybe delete the WDT and insert your timer/ISR in this working t85 project:
http://www.hackster.io/rayburne/hot-yet

Ray

This code works on my attiny85 with internal 8Mhz, for blinking a led once per second.

//Config Timer1 en attiny85

//constantes y vars

const int led=3;

//config inicial

  void setup() {
     pinMode(led,OUTPUT);
    
     cli();
     TCCR1=0;
     TCNT1 = 0;                  //zero the timer
     GTCCR = _BV(PSR1);          //reset the prescaler
     OCR1A=243;           //contador CTC, tiene un tamaño de 2^8-1=255
     OCR1C=243;               
     TCCR1 |= (1<<CTC1);  //el analogo a WGM12 en los atmega
     TCCR1 |= (1<<CS10);//
     TCCR1 |= (1<<CS11);//
     TCCR1 |= (1<<CS12);
     TCCR1 |= (1<<CS13);//   CS10=1, CS11=1, CS12=1, CS13=1   ==> prescaler=16384 (ver datasheet attiny85)
                        // luego T=1/(f/prescaler)==> 16384/8MHz = 2048us
                        //2048us*CTC=2048us*244= 500 ms           
     TIMSK=(1<<OCIE1A); //para habilitar la comparacion Output Compare A Match (vector interrupcion)
     sei();
    }
    
    void loop(){
    }
    
//rutina interrupcion
  ISR(TIMER1_COMPA_vect){
  digitalWrite(led,!digitalRead(led));
   
  }

giuliof:

fungus:
Try calling it "TIMER0_OVF_vect" instead of "TIM0_OVF_vect"

Nothing, I get this warning from avr-gcc, and when i load it I have not changes.

warning: ‘TIMER0_OVF_vect’ appears to be a misspelled signal handler

I noticed that interrupt functions makes the micro crazy: if I comment them, leaving registers inizialised, the main program is executed.

OK. The name changes depending on which "ATtiny" you're using (which you're keeping secret). I'm guessing it's not a Tiny85 because you use PORTA. If you're getting an error then you probably picked the right name.

I haven't tried running it but I can't see anything obviously wrong in the code.

Is that the exact same code you're using?

giuliof:
If I connect two LED to PORTA.0 and PORTB.0 I should see them blinking, but all is silent. I tried to give the hex file to simavr, and I have a crash output. If i comment the ISR routine the program works (i have the PORTB LED blinking), and obviously the simulator doesn't give me any error.
Is the code wrong, or needs some special parameters to avr-gcc/avrdude to be compiled/loaded?

I compiled your code in Atmel Studio for an attiny84A and it runs fine. I tried different CPU clock rates, 16MHz, 8MHz, and 1MHz. They all ran fine. The ISR LED blinks about half the speed of the loop LED @1MHz.

I copied the avr-gcc options from Arduino's config files, and now all works. I think the problem is solved, for the moment.