51单片机外部中断问题
我设置了外部中断0,但是主程序中调用的时候,通过外部按键给单片机引脚下降沿电平,为什么没有跳转到外部中断函数中呢?代码如下:#include<reg52.h>sbitLE...
我设置了外部中断0,但是主程序中调用的时候,通过外部按键给单片机引脚下降沿电平,为什么没有跳转到外部中断函数中呢?
代码如下:
#include <reg52.h>
sbit LE=P0^1;
unsigned char mode;
unsigned int YSJS=0;
//定义按键
sbit key_LOW=P1^0;
sbit key_HIGH=P1^1;
sbit key_TURN=P1^2;
sbit key_POSITION=P1^3;
sbit key_DRL=P1^4;
sbit key_X1=P1^5;
sbit key_X2=P1^6;
sbit key_X3=P1^7;
//定义灯
sbit led_LOW=P2^0;
sbit led_HIGH=P2^1;
sbit led_TURN=P2^2;
sbit led_POSITION=P2^3;
sbit led_DRL=P2^4;
sbit led_X1=P2^5;
sbit led_X2=P2^6;
sbit led_X3=P2^7;
//延时子程序
void delayms(unsigned int ms)
{
unsigned char i;
while(ms--)for(i=0;i<120;i++);
}
//灯熄灭
void off()
{
led_LOW=0;
led_HIGH=0;
led_TURN=0;
led_POSITION=0;
led_DRL=0;
led_X1=0;
led_X2=0;
led_X3=0;
}
//外部中断初始化
void Init_INT()
{
EA=1; //开启总中断
EX0=1; //开启外部中断0
IT0=1; //1为下降沿触发
EX1=1;
IT1=1;
}
//外部中断0,模式切换
void mode1() interrupt 0
{
if(mode=1) mode=0;
//else mode=1;
}
//外部中断1,按键弹起
void button() interrupt 1
{
LE=1;
delayms(10);
LE=0;
}
//主程序
void main()
{
mode=1;
Init_INT();
while(1)
{
if (mode==1) automode();
if (mode==0) handmode();
}
} 展开
代码如下:
#include <reg52.h>
sbit LE=P0^1;
unsigned char mode;
unsigned int YSJS=0;
//定义按键
sbit key_LOW=P1^0;
sbit key_HIGH=P1^1;
sbit key_TURN=P1^2;
sbit key_POSITION=P1^3;
sbit key_DRL=P1^4;
sbit key_X1=P1^5;
sbit key_X2=P1^6;
sbit key_X3=P1^7;
//定义灯
sbit led_LOW=P2^0;
sbit led_HIGH=P2^1;
sbit led_TURN=P2^2;
sbit led_POSITION=P2^3;
sbit led_DRL=P2^4;
sbit led_X1=P2^5;
sbit led_X2=P2^6;
sbit led_X3=P2^7;
//延时子程序
void delayms(unsigned int ms)
{
unsigned char i;
while(ms--)for(i=0;i<120;i++);
}
//灯熄灭
void off()
{
led_LOW=0;
led_HIGH=0;
led_TURN=0;
led_POSITION=0;
led_DRL=0;
led_X1=0;
led_X2=0;
led_X3=0;
}
//外部中断初始化
void Init_INT()
{
EA=1; //开启总中断
EX0=1; //开启外部中断0
IT0=1; //1为下降沿触发
EX1=1;
IT1=1;
}
//外部中断0,模式切换
void mode1() interrupt 0
{
if(mode=1) mode=0;
//else mode=1;
}
//外部中断1,按键弹起
void button() interrupt 1
{
LE=1;
delayms(10);
LE=0;
}
//主程序
void main()
{
mode=1;
Init_INT();
while(1)
{
if (mode==1) automode();
if (mode==0) handmode();
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询