so basically I'm trying set up a ISR(Interrupt subroutine) to simply turn a light on, but the ISR doesn't seem to run. I've looked through the microcontroller datasheet a lot to see if I'm doing something wrong but I couldn't find anything.
Heres my code.
#include <asf.h>
int main (void) {
DDRD = 0b10000000;
cli(); // turn off global interrupts
//Timer
TCCR2A |=_BV(WGM21); //CTC mode
TCCR2B |=_BV(CS22)|_BV(CS21); //clk pre-scale 1/256
OCR2A = 0x7d; //125clk cycles = 2ms
TIMSK2 |=_BV(OCIE2A); //enable interrupt on compare matchA
TIFR2 |= _BV(OCF2A); //clear flag
TCNT2 = 0x00; // reset counter
sei(); // enable global interrupts
}
ISR(TIMER2_COMPA_vect) {
PORTD = 0b10000000;
}
答案 0 :(得分:4)
你应该在main()函数的末尾有一个无限循环:
#include <asf.h>
int main (void) {
DDRD = 0b10000000;
cli(); // turn off global interrupts
//Timer
TCCR2A |=_BV(WGM21); //CTC mode
TCCR2B |=_BV(CS22)|_BV(CS21); //clk pre-scale 1/256
OCR2A = 0x7d; //125clk cycles = 2ms
TIMSK2 |=_BV(OCIE2A); //enable interrupt on compare matchA
TIFR2 |= _BV(OCF2A); //clear flag
TCNT2 = 0x00; // reset counter
sei(); // enable global interrupts
while(1);
}
ISR(TIMER2_COMPA_vect) {
PORTD = 0b10000000;
}
如果没有无限循环,AVR会达到未定义的状态!