#include "reg51.h" // 试试
sbit k1 = P1^0;//按键1
sbit k2 = P1^1;//按键2
sbit k3 = P1^2;//按键3
sbit k4 = P1^3;//按键4
sbit p5 = 誉银P1^5; //输出1
sbit p6 = P1^6; //输出2
bit t1, t2;
void main(void)
{
while(1)
{
if(k1==0 && t1==0) {t1=1; p5=1;}
if(k2==0 && t1==0) {t1=1; 纯虚此p5=0;}
if(k1==1 && k2==1) {t1=0;}
if(k3==0 && t2==0) {t2=1; p6=1;}
if(k4==0 && t2==0) {t2=1; p6=0;}
if(k3==1 && 做迅k4==1) {t2=0;}
}}
看你连分都没有花五分钟给你写一个程序代码如下:
/瞎陪************************************************************************************************************
#include
sbit Key_01 = P1^1;
sbit Key_02 = P1^2;
sbit Key_03 = P1^3;
sbit Key_04 = P1^4;
sbit MAIN_A = P1^5;sbit MAIN_B = P1^6;
/*
* 延时
*/
void Delay_1ms(unsigned int time)
{
int x, y;
for(x = time; x > 0; x--)
for(y = 110; y > 0; y--);
}
/*
* 按键扫描子函数
*/
void Key_ScangCmd(void)
{
//=====按键1======//
if(Key_01 == 0) //判铅漏断按键1是否等于0
{
Delay_1ms(10); //延时去抖动
if(Key_01 == 0) //判断按键1是否等于0
{
MAIN_A = 0; //P1.5 = 0低电平
}
while(Key01 == 0); //等待松手检测
}
//=====按键3======//
if(Key_03 == 0)
{
Delay_1ms(10);
if(Key_03 == 0)
{
MAIN_A = 1; //P1.5 = 1高电平
}
while(Key03 == 0);
}
//=====按键2======//
if(Key_02 == 0)
{
Delay_1ms(10);
if(Key_02 == 0)
{
MAIN_B = 0; //P1.6 = 0低电平
}
while(Key02 == 0);
}
//=====按键4======//
if(Key_04 == 0)
{
Delay_1ms(10);
if(Key_04 == 0)
{
MAIN_B = 1; //P1.6 = 1高电磨激蠢平
}
while(Key04 == 0);
}
}
/*
* 主函数
*/
int main(void)
{
while(1)
{
Key_ScangCmd(); //进入主程序一直扫描是否有按键按下
}
}
*****************************************************************************************************************/
;
ORG 0000H
;-----------------------------------------
MOV C, P1.1 ;当k1=0时
MOV P5, C ;p5输出低电平
;-----------------------------------------
MOV C, P1.2 ;当k2=0时
MOV P6, C ;p6输出低电平雀派
;-----------------------------------------
MOV C, P1.3 ;当k3=0时乱岁核
CPL C
MOV P5, C ;p5回到高电平
;-----------------------------------------
MOV C, P1.4 ;当k4=0时
CPL C
MOV P6, C ;p6回到哗掘高电平
;-----------------------------------------
SJMP 0000H
END
#include
sbit k1=P1^0;
sbit k2=P1^1;
sbit k3=P1^2;
sbit k4=P1^3;
sbit door1=P1^5;
sbit door2=P1^6;
void delay()
{
unsigned char i,j;
for(i=0;i<20;i++)
for(j=0;j<120;j++);
}
}
main()
{
unsigned char key;
while(1)
{
key=~(P1&0x0f);
if(key!=0)
{
delay();
if(key==~(P1&0x0f))
switch(key)
{
case 0x01:door1=0;break;
case 0x02:door2=0;break;
case 0x04:door1=1;break;
case 0x08:door2=1;break;
default:break;
}
}
}
}