您的位置 首页 ADAS

AVR 外部中止INT0的简略操作

includeavrioh>includeutildelayh>includeavrinterrupth>调用WINAVR的中断库函数。volatileunsignedcharcou

#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);
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/adas/262349.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部