本帖最后由 zjqlovelyy 于 2013-10-1 16:00 编辑
wangch_sh 发表于 2013-10-1 09:49 
没接线应该不会改变。接了有电平变化或者上升沿或者下降沿才会改变。
就好比如这个程序,执行结果是使Led闪烁,实际上,没接线,仍然可以闪烁
- /*********************************************************************************
- *实验名 :外部中断实验
- *注意 :由于P3.2口跟红外线共用,所以做按键实验时为了不让红外线影响实验效果,最好把红外线先
- *取下来。
- *实验效果 :K3按下LED左循环,K4按下LED变右循环
- *
- *********************************************************************************/
- #include<reg51.h>
- #include<intrins.h>
- #define GPIO_LED P2
- //外部中断的IO
- sbit K3=P3^2;
- sbit K4=P3^3;
- void IntConfiguration();
- void Delay(unsigned int n);
- unsigned char KeyValue=0;
- /*******************************************************************************
- * 函数名 : main
- * 函数功能 : 主函数
- * 输入 : 无
- * 输出 : 无
- *******************************************************************************/
- 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);
- }
- }
- /*******************************************************************************
- * 函数名 : IntConfiguration()
- * 函数功能 : 设置外部中断
- * 输入 : 无
- * 输出 : 无
- *******************************************************************************/
- void IntConfiguration()
- {
- //设置INT0
- IT0=1;//跳变沿出发方式(下降沿)
- EX0=1;//打开INT0的中断允许。
- //设置INT1
- IT1=1;
- EX1=1;
-
- EA=1;//打开总中断
- }
- /*******************************************************************************
- * 函数名 : Delay(unsigned int n)
- * 函数功能 : 延时
- * 输入 : n
- * 输出 : 无
- *******************************************************************************/
- 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--);
- }
- }
- /*******************************************************************************
- * 函数名 : Int0() interrupt 0
- * 函数功能 : 外部中断0的中断函数
- * 输入 : 无
- * 输出 : 无
- *******************************************************************************/
- void Int0() interrupt 0 //外部中断0的中断函数
- {
- Delay(1); //延时消抖
- if(K3==0)
- KeyValue=1;
- }
- /*******************************************************************************
- * 函数名 : Int1() interrupt 2
- * 函数功能 : 外部中断1的中断函数
- * 输入 : 无
- * 输出 : 无
- *******************************************************************************/
- void Int1() interrupt 2 //外部中断1的中断函数
- {
- Delay(1); //延时消抖
- if(K4==0)
- KeyValue=0;
- }
|