Help with simple interrupt

I’m having trouble with the following code. No matter what value I set OCR1A to, the LED always seems to fire up right away. What am I doing wrong?

Thanks

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

ISR(TIMER1_COMPA_vect){
  //turn on LED 13...
  PORTB = 0xFF;
}

void setup(){
  Serial.begin(9600);
    DDRB = 0xFF;
    TIMSK1 = _BV(OCIE1A);
    //256 prescale, CTC mode T0P
    TCCR1B  = _BV(CS12) | _BV(WGM12);
    OCR1A = 32500;
    sei();

}

void loop(){
  //twiddle thumbs
}

At the register level there is no such thing as a simple interrupt.

If you haven't already seen this, have a look at the FrequencyTimer2 library. This enables you to use interrupts without having to read and understand all the low level information in the ATmega168 datasheet.

See: http://www.arduino.cc/playground/Code/FrequencyTimer2

You did not say what you want to do… do you want to dim the LED connected to the OC1A pin?

Then maybe you want to toggle the pin on each OC event:

TCCR1A |= (1 << COM1A0);

without this line of code, the OC1A pin is not connected to the timer outout…

and of course you want to set OCR1A to the desired value in your ISR again…

Mem: "without having to read and understand all the low level information in the ATmega168 datasheet" that woul come handy for me.

However, it seems FrequencyTimer2 lib seems not working with arduino>11 anymore?

I get this errors:

In file included from D:\Programme\arduino-0013\hardware\cores\arduino/WProgram.h:4,
d:/programme/arduino-0013/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected unqualified-id before 'int'
d:/programme/arduino-0013/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected `)' before 'int'
d:/programme/arduino-0013/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected `)' before 'int'

In file included from D:\Programme\arduino-0013\hardware\cores\arduino/WProgram.h:6,

Carsten, who just struggles with a LED Matrix....

See this for a change you need to make to the library:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1237653105/3#3

Hmm.

I can't see a line to change as suggested in the thread.

For now I am using MSTimer Lib which works good so far. Luckiely I dont need a rate below 1ms.

Carsten