中断学习2-九游会j9

嵌入式 > 嵌入式开发 > 详情

中断学习2

发布时间:2025-07-25 发布时间:
|
#include

#include//头文件

#define gpio_led p2

//外部中断的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() interrupt 0 //外部中断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)。



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

热门文章 更多
intel 独立显卡iris xe max亮相.性能超越mx450
网站地图