运行的结果是,按键只能按一下检测到后中,LED不停地闪烁,给人的感觉是按键没有弹起一直有测到低电平。
出现这种现象是为哪里设置引起的?
请各位大师帮助 看一下哪里的问题?
#include "stc8h.h"
sbit Led_Red = P1^7;
sbit Led_Green = P3^4;
sbit Led_Blue = P5^5;
sbit Key1 = P3^6; // 00模式
sbit Key2 = P3^5; // 00模式
sbit Key3 = P5^1; // 00模式
bit Key_flag=0; //按键松开按下标识
u8 Counter,Key_Modle;
extent void delay_ms(u16 mS);
void IO_Int(void)
{
P_SW2 |= 0x80; //扩展寄存器(XFR)访问使能
P0M1 = 0x00; P0M0 = 0x00; //设置P0.4、P0.5为漏极开路
P1M1 = 0x00; P1M0 = 0x00; //设置P1.4、P1.5为漏极开路
P2M1 = 0x00; P2M0 = 0xff; //设置P2.2~P2.5为漏极开路
P3M1 = 0x00; P3M0 = 0x80; //设置P3.3、P3.5、P3.6为漏极开路 01101000 10000000
P4M1 = 0x00; P4M0 = 0x0f; //设置P4.2~P4.5为漏极开路
P5M1 = 0x00; P5M0 = 0x01; //设置P5.2、P5.3为漏极开路00001010 00000011
P6M1 = 0xff; P6M0 = 0xff; //设置为漏极开路(实验箱加了上拉电阻到3.3V)
P7M1 = 0x00; P7M0 = 0x00; //设置为准双向口
P3PU=0x68;
P5PU=0x02;
P0=0X00;
P1=0X00;
P2=0XFF;
//P3=0X68; // 35 36
P4=0X0F;
Led_Red =1;
Led_Green=1;
Led_Blue =1;
}
void Key_Check(void)
{
if((Key1==0)&&(Key_flag))
{delay_ms(80);
if(Key1==0)
{
Key_Modle=1;
Led_Red =1;
Led_Blue =1;
Led_Green =~Led_Green;
Key_flag = 0;
Counter = 0;
}
}
if((Key2==0)&&(Key_flag))
{delay_ms(80);
if(Key2==0)
{
Key_Modle=2;
Led_Green =1;
Led_Red =1;
Led_Blue =~Led_Blue;
Key_flag = 0;
}
}
if(Key3==0)&&(Key_flag)
{delay_ms(80);
if(Key3==0)
{
Key_Modle=3;
DIR = 1;
EN = 0;
Led_Green =1;
Led_Blue =1;
Led_Red=~Led_Red;
Counter = 0;
Key_flag = 0;
}
}
if((Key==0)&&(Key_flag))
{delay_ms(40);
if(Key==0)
{
Key_Modle=4;
Counter = 0;
Key_flag = 0;
}
}
if((Key1)&&(Key2)&&(Key3)&&(Key))
{
Key_flag=1;
}
}
void main(void)
{
IO_Int();
while(1)
{
delay_ms(100);
Key_Check();
}
}
|