運行的結果是,按鍵只能按一下檢測到后中,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();
}
}
|