Teensy USB Interrupts

avrcinterruptsteensy

I'm trying to learn about interrupts using one of the Teensy USB boards. It's got an AVR AT90USB1286 chip on it. I'm using the code below, and I'm expecting my ISR block to get called and periodically flash the LED on pin 6. But nothing is happening. Can anyone see what I'm doing wrong?

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

#define PIN_ON(n) (PORTD |= (1<<(n)))
#define PIN_OFF(n) (PORTD &= ~(1<<(n)))

#define LED_CONFIG  (DDRD |= (1<<6))
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))

#define OVERFLOW_INTERRUPT_ENABLE (TIMSK0 |= (1<<TOIE0))


volatile unsigned int overflow_count = 0; //Count of overflows

ISR(TIMER0_OVF_vect) {
  cli(); //Disable Global Interupt
  if (overflow_count < 0xFFFF) {
    overflow_count++;
  }
  else {
    PIN_ON(6);
    _delay_ms(500);
    PIN_OFF(6);
    overflow_count = 0;
  }
  sei(); //enable global interupt

}
int main(void)
{

    // set for 16 MHz clock, and make sure the LED is off
    CPU_PRESCALE(0);
    LED_CONFIG;
    OVERFLOW_INTERRUPT_ENABLE;
    sei(); //enable global interupts


    while (1) {
      _delay_ms(1000);
      //analogWrite(6, 10);
    }
}

Best Answer

Your ISR should not be disabling and re-enabling interrupts and it should not be delaying.

Try:

ISR(TIMER0_OVF_vect)
{
  PORTD ^= 1<<6;
}

Or even:

ISR(TIMER0_OVF_vect)
{
    if (overflow_count < 0xFFFF)
        overflow_count++;
    else
    {
        overflow_count = 0;
        PORTD ^= 1<<6;
    }
}

Then configure the timer interrupt frequency by changing TCCR0B.

To begin the timer, you may also need to clear the counter:

TCNT0 = 0