×
单片机 > 单片机程序设计 > 详情

51外部中断测试

发布时间:2020-05-28 发布时间:
|

#include

#include


#define GPIO_LED P2

//外部中断的IO

sbit K3=P3^2;

sbit K4=P3^3;


void IntConfiguration();

void Delay(unsigned int n);

unsigned char KeyValue=0;

void main(void)

{

GPIO_LED=0Xfe;

IntConfiguration();

while(1)

{

if(KeyValue)

GPIO_LED=_crol_(GPIO_LED,1);

else

GPIO_LED=_cror_(GPIO_LED,1);

Delay(2000);

}

}

void IntConfiguration()

{

//设置INT0

IT0=1;//跳变沿出发方式(下降沿)

EX0=1;//打开INT0的中断允许。

//设置INT1

IT1=1;

EX1=1;

EA=1;//打开总中断

}


void Delay(unsigned int n)   //延时50us误差 0us

{

    unsigned char a,b;

for(;n>0;n--)

{

    for(b=1;b>0;b--)

        for(a=22;a>0;a--);

}

}


void Int0() interrupt 0 //外部中断0的中断函数

{

Delay(1); //延时消抖

if(K3==0)

KeyValue=1;

}


void Int1() interrupt 2 //外部中断1的中断函数

{

Delay(1); //延时消抖

if(K4==0)

KeyValue=0;

}


『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
如何升级STM32单片机的代码