新闻中心

中断学习

作者: 时间:2016-11-27 来源:网络 收藏
#include

#include//头文件

本文引用地址://m.amcfsurvey.com/article/201611/322386.htm

#define GPIO_LEDP2

//外部中断的IO

sbit K3=P3^2;

sbit K4=P3^3;//定义k3,k4的io口值为p3.2p3.3

void IntConfiguration();

void Delay(unsigned int n);

unsigned char KeyValue=0;

void main(void)

{

GPIO_LED=0X01;//led=0000 - 0001

IntConfiguration();

while(1)

{

if(KeyValue)//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()interrupt0 //外部中断0的中断函数

{

Delay(1);

if(K3==0) //按下P3^2键,触发外部中断0

KeyValue=1;//中断处理程序0就会将KeyValue赋1

}

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

{

Delay(1);

if(K4==0)//当按下P3^3,就会触发外部中断1

KeyValue=0; //中断断处理程序0就会将KeyValue赋0

}

//两个外部中断硬件上就是与P3^3和P3^2连接的
//,比如说按下P3^2键,触发外部中断0,中断处理程序0就会将KeyValue赋1
//,这时主循环就会执行GPIO_LED=_crol_(GPIO_LED,1

//);同理,当按下P3^3,就会触发外部中断1
//,中断处理程序0就会将KeyValue赋0;这时主循环就会执行GPIO_LED=_cror_(GPIO_LED,1)。



关键词:中断学

评论


技术专区

关闭