AVR 外部中斷INT0的簡(jiǎn)單操作 作者: 時(shí)間:2016-11-22 來(lái)源:網(wǎng)絡(luò ) 加入技術(shù)交流群 掃碼加入和技術(shù)大咖面對面交流海量資料庫查詢(xún) 收藏 #include <avr/io.h>#include #include interrupt.h> //調用WINAVR的中斷庫函數。volatile unsigned char count = 0; //定義循環(huán)變量,大家要注意我們這里要加上volatile. //因為count函數在中斷函數中會(huì )變化。ISR(INT0_vect) //中斷函數,注意我們寫(xiě)中斷函數用的到ISR(中斷名){ _delay_ms(10); //按鍵延時(shí) if((PIND&(1 << PD0)) == 0) //重復檢測防抖動(dòng),只有按鍵按下時(shí)先執行if里面的語(yǔ)句 { count++; PORTB = 0xff; if(count > 7) { count = 0; } } while(!(PIND&(1 << PD0))); //等持釋放按鍵 _delay_ms(10); //這里也是防抖動(dòng)}void Interrupt_Init(void) //中斷初始化函數{ EICRA |= _BV(1); //INT0為下降沿產(chǎn)生異部中斷請求 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); }}
評論