大家好
我在调试的板有3个按键,分别是P00,P02,P10,外部电路是上拉10K到5V,下接1K到按键再接地,按键按下的时候是0.7V左右,不按是5V,但是只有P10能正常都到高低电平,P00,P02一直读到低电平,P00,P02的第二功能是AD和外部中断,但是我都关闭了这些功能。
void MCUinit(void)
{
EA=0;
FLASHCON = 0;
CLKCON=0x20;
IPL0 = 2;
IPH0 = 0;
TCON=0;
TR0=0; //停止T0定时器
ET0=0; //禁止T0定时器
TMOD=0x01;
TH0= HIBYTE(T0_125US);
TL0= LOBYTE(T0_125US);
ET0=1;
TR0=1;
///***************************************
//AD初始化设置:
// ADC时钟周期=8Tsys
// 通道设置AN1、AN2、AN3为ADC口
//****************************************/
ADT=0x6E;
ADCH=0x00; //作为普通IO口
///***************************************
//外部中断初始化设置:
// INT2、INT3下降沿触发
//****************************************/
EXF0=0x10;
IEN1=0x04;
/*************************************************************************8
ADC初始化
***************************************************************************/
ADCH=0x00; //作为普通IO口
ADT=0x6E;
EA = 1; //允许中断
}
void PortDir(void)
{
P0 = Bin(11110111);//P0_2为按键输入,设置初始状态为1
P0CR = Bin(01111000);//P0_2为按键输入,设置为输入
P0OS |= 0X30;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=
P1 = Bin(00000001);//P1_6直流电机12V电源
P1CR = Bin(01111110);//P1_6直流电机12V电源
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=
P2 = Bin(11110000);//P2_4为PWM0输出,设置初始状态为1
P2CR = Bin(00011111);//P2_4为PWM0输出,设置为输出
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++=
P3 = Bin(00000000);
P3CR = Bin(00000011);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++==
//上拉设置
P0PCR = Bin(00110000);
P1PCR = Bin(00000000);
P2PCR = Bin(11100000);
P3PCR = Bin(00000000);
}
main(void)
{
Delay(100); //系统启动延时
MCUinit(); //MCU初始化
Delay(100);
PortDir();
Delay(100);
while(1)
{
if(Flag2ms)
{
Flag2ms=0;
RSTSTAT = 0x00; //2730.6ms 清看门狗
KeyScan();
if(Key1)
{
LD2=0;
}
else
{
LD2=1;
}
if(Key2)
{
LD2=0;
}
else
{
LD2=1;
}
if(Key3)
{
LD2=0;
}
else
{
LD2=1;
}
}
}
}
LD2是外接的一个LED,=0就亮,=1就灭,我分别测试了Key1,Key2,Key3三部分按键程序,只有Key3不按的时侯LED会亮,按下时,LED灭了,Key1,Key2一直都是灭的 |