0

Need some quick help with Interrupts
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Need some quick help with Interrupts

by nemesisrobot on Tue Mar 10, 2009 2:32 am

I'm trying to play around with interrupts, but I'm having trouble getting the following code to work. No matter what value I try pass to OCR1A, LED fires up right away.

Code: Select all | TOGGLE FULL SIZE
#include <avr/io.h>
#include <avr/interrupt.h>

int counter = 0;

ISR(TIMER1_COMPA_vect){
  //Turn on LED on Pin 13
  PORTB = 0xFF;
}

void setup(){
    DDRB = 0xFF;

    //Interrupt when timer matches OCR1A
    TIMSK1 = _BV(OCIE1A);
    //256 prescale, CTC mode TOP
    TCCR1B  = _BV(CS12) | _BV(WGM12);

    OCR1A = 31200;
    sei();

}

void loop(){
//twiddle thumbs
}
nemesisrobot
 
Posts: 25
Joined: Tue Jan 01, 2008 3:27 am

Re: Need some quick help with Interrupts

by mtbf0 on Tue Mar 10, 2009 7:04 am

first of all, the arduino code initializes all of the timers so they can be used for pwm. you'll need to clear TCCR1A. as it is you're actually selecting fast pwm 8-bit mode 5, since WGM10 is set long before your setup code ever runs.

then i'd code it to toggle digital 13, since a blink is infinitely more entertaining than a 1/2 second pause followed by the light coming on.
Code: Select all | TOGGLE FULL SIZE
  PORTB ^= (1 << PB5);
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: Need some quick help with Interrupts

by nemesisrobot on Tue Mar 10, 2009 8:17 pm

Ah okay I didn't know that. Thank you!

And yes, blinking LEDs are much more fun =)
nemesisrobot
 
Posts: 25
Joined: Tue Jan 01, 2008 3:27 am

Please be positive and constructive with your questions and comments.