新手求救。关于中断问题
程序的作用是按下K3,流水灯左移,按下K4,流水灯右移。
一开始是这样写的:
#include<reg52.h>
#include<intrins.h>
sbit K3=P3^2;
sbit K4=P3^3;
#define LED P2
unsigned char Keyvalue=0;
void delay(unsigned int n);
void delay(unsigned int n)
{
unsigned char a,b;
for(;n>0;n--)
{
for(b=1;b>0;b--)
for(a=22;a>0;a--);
}
}
void main()
{
LED=0xfe;
IT0=1;
EX0=1;
IT1=1;
EA=1;
while(1)
{
if(Keyvalue)
LED=_crol_(LED,1);
else
LED=_cror_(LED,1);
delay(2000);
}
}
void IN0() interrupt 0
{
delay(1);
if(K3==0)
Keyvalue=1;
}
void IN1() interrupt 2
{
delay(1);
if(K4==0)
Keyvalue=0;
}
但是这样做的话K4按键按下去的时候无效,就是LED并没有右移。后来这样写:
#include<reg51.h>
#include<intrins.h>
#define GPIO_LED P2
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()
{
IT0=1;
EX0=1;
IT1=1;
EX1=1;
EA=1;
}
void Delay(unsigned int n)
{
unsigned char a,b;
for(;n>0;n--)
{
for(b=1;b>0;b--)
for(a=22;a>0;a--);
}
}
void Int0() interrupt 0
{
Delay(1);
if(K3==0)
KeyValue=1;
}
void Int1() interrupt 2
{
Delay(1);
if(K4==0)
KeyValue=0;
}
这样写的话就能正常工作了,新手一枚,求助各位大神!
大神快来帮忙啊
你貌似没说你的要求偶~~~~~
不知道你到底想说什么呢?
就是开启中断的那几段代码用子函数写的话程序就正常了,不用子函数调用的话就不能用
就是开启中断的那几段代码用子函数写的话程序就正常了,不用子函数调用的话就不能用。
你还能仔细看看啊,明显少了一句话。打赏点积分把
虽然我看不懂 但是还是顶贴一下
其实第一个程序是可以运行的,只是你马虎少些一行而已~~~~~这一行你在第二个程序里竟然写上了,你没发现么?哈哈~~~~~
下面是我帮你改的,你重新编译一下看看是否可以运行。
程序要求:按下P3^2,流水灯左移,按下P3^3,流水灯右移。
/************ 头 文 件 ************/
#include <reg52.h>
#include <intrins.h>
/************ 宏 定 义 ************/
#define LED P2
unsigned char Keyvalue = 0;
/************ 延 时 程 序 ************/
void delay(unsigned int n) {
unsigned char a,b;
for(;n>0;n--) {
for(b=1;b>0;b--)
for(a=22;a>0;a--);
}
}
/************ 主 程 序 ************/
void main() {
LED = 0xfe;
IT0 = 1;
EX0 = 1;
IT1 = 1;
EX1 = 1; //这一行你没写,外部中断 1 肯定不能运行了!
EA = 1;
while(1) {
if(Keyvalue)
LED=_crol_(LED,1);
delay(2000); //这一行你也没写!不延时估计P2口的LED会呈全亮状态吧?
else
LED=_cror_(LED,1);
delay(2000);
}
}
/************ 外部中断 0 ************/
void IN0() interrupt 0 {
Keyvalue =! Keyvalue; //静心体会一下我为什么这样写,和你写的有哪些不同。
}
/************ 外部中断 1 ************/
void IN1() interrupt 2 {
Keyvalue =! Keyvalue; //原理同上
}
/* 这段程序我没上机验证,相信不会有啥问题,你编译验证一下吧。若成功了就来报个信,打赏多少积分你看着办 ^_^ */
