2015-09-25 73 views
1

這是定時器的代碼,它將在按下按鈕(INT0)後啓動,並在Atmega128按下按鈕一段時間後復位。但是按了按鍵之後,Timer自動啓動。在我看來,釋放按鈕後,外部中斷以某種方式激活。但是我知道爲什麼?我會從你的幫助會非常感激AVR外部中斷錯誤

#include <avr/io.h> 
#include <avr/interrupt.h> 
#include <util/delay.h> 
int a,b,c; 
int main(void) 
{ DDRA = 0xff; 
    DDRB = 0xff; 
    DDRC = 0xff; 
    DDRD = 0x00; 
    sei(); 
    OCR1A = 0xFFFF; 
    TIMSK |= (1 << OCIE1A); 
    TCCR1A |= (1 << COM1A0); 
    TCCR1B |= (1<<WGM12); 

    EIMSK = (1 << INT0); 

    while (1) 
    { 
     int iggy; 
     iggy=iggy+1; 
    } 
} 
ISR (TIMER1_COMPA_vect) //When Timer interrupt activates, These actions will be taken 
{ 
    if(a==9) 
    { a=0; 
     if(b==9) 
     {b=0; 
      if(c==9) 
      { c=0;} 
      else 
      { c=c+1;} 
     } 
     else 
     {b=b+1;} 
    } 
    else 
    { a=a+1;} 
    PORTA = a; 
    PORTB = b; 
    PORTC = c; 
} 

ISR(INT0_vect)   //When button is pressed it will start timer clock and if it pressed too long it will stop the clock and reset values 
{ 
    TCCR1B |= (1 << CS10); 
    _delay_ms(800); 
    if(PIND==0b00000000) 
    { 
     TCCR1B |= (0 << CS10); 
     a=0; 
     b=0; 
     c=0; 
     PORTA = a; 
     PORTB = b; 
     PORTC = c; 
     _delay_ms(1000); 
    } 
} 
+0

你的去勢在哪裏? –

+0

你不想在任何ISR中故意拖延 - 永遠不要!應始終儘快處理ISR。將ISR中的信息(按下按鈕)存儲在某個「全局」變量中,然後在main() - 循環中進行處理。 –

回答