ATmega16 有3個外部中斷(External Interrupts),此例中 iammic 將使用按鈕透過INT 0進行外部中斷,去開關 LED 。

 

電路圖:(PD2 接腳中的按鈕使用Atmega 16 內部 pull up resistor,可以參考 ATmega16 以按鈕控制 LED(pull up resistor) )

 

以下介紹外部中斷會用到的 Register:

MCUCR:

以INT 0 為例,可以使用 MCUCR Bit 0 與 Bit 1,來設定 INT 0 的觸發條件,此例中使用的觸發條件為 The falling edge of INT0 generates an interrupt request,當按下按鈕後,產生一個 falling edge 後觸發。

 

 

 

當要使用到 INT 0 時需要將 GICR Bit 6 設為 1,SREG 中的 Bit 7 設為 1。

GICR:

 

程式:

#include <avr/io.h>

#include <avr/interrupt.h>

 

int main(void){

   

DDRA=0x1; //設定 PA0 為 OUTPUT

    DDRD=0x00;//設定 PD2 為 INPUT 此行程式碼可以省略

    PORTD=0x4; //設定 PD2 為 pull up resistor

MCUCR=0x02; //設定 falling edge-triggered

GICR=0x40;  //設定 INT0 Enable

SREG=0x80; //設定 Interrupt Enable

while(1);

return 0;

}

 

ISR(INT0_vect){ //INT0 中斷函式

PORTA^=1 << PA0;

}

 

arrow
arrow
    全站熱搜

    iammic 發表在 痞客邦 留言(0) 人氣()