久久ER99热精品一区二区-久久精品99国产精品日本-久久精品免费一区二区三区-久久综合九色综合欧美狠狠

新聞中心

EEPW首頁 > 嵌入式系統 > 設計應用 > AVR 外部中斷INT0的簡單操作

AVR 外部中斷INT0的簡單操作

作者: 時間:2016-11-22 來源:網絡 收藏
#include <avr/io.h>
#include
#include interrupt.h> //調用WINAVR的中斷庫函數。

volatile unsigned char count = 0; //定義循環變量,大家要注意我們這里要加上volatile.
//因為count函數在中斷函數中會變化。
ISR(INT0_vect) //中斷函數,注意我們寫中斷函數用的到ISR(中斷名)
{
_delay_ms(10); //按鍵延時
if((PIND&(1 << PD0)) == 0) //重復檢測防抖動,只有按鍵按下時先執行if里面的語句
{
count++;
PORTB = 0xff;
if(count > 7)
{
count = 0;
}
}
while(!(PIND&(1 << PD0))); //等持釋放按鍵
_delay_ms(10); //這里也是防抖動
}

void Interrupt_Init(void) //中斷初始化函數
{
EICRA |= _BV(1); //INT0為下降沿產生異部中斷請求
EIMSK |= _BV(INT0); //始能外部中斷0
sei(); //置位全局中斷位
}
int main(void)
{
DDRB = 0xff; //PB口為輸出模式
PORTB = 0xff; //初始化為1

DDRD = 0x00; //PD口為輸入模式
PORTD = 0xff; //有上拉
Interrupt_Init();
while(1)
{
PORTB |= _BV(count);
_delay_ms(500);
PORTB &= ~_BV(count);
_delay_ms(500);
}
}


關鍵詞: AVR外部中斷INT

評論


技術專區

關閉