ORG 0000H
START: MOV A,P0
ORL A,#0F0H
CJNE A,#0FFH,PL0
SJMP START
PL0: LCALL DELAY
MOV A,P0
ORL A,#0F0H
CJNE A,#0FFH,PL1
SJMP START
PL1:JNB ACC.0,K1
JNB ACC.1,K2
JNB ACC.2,K3
JNB ACC.3,K4
LJMP START
K1:MOV P1,#0F9H
SJMP $
K2:MOV P1,#0A4H
SJMP $
K3:MOV P1,#0B0H
SJMP $
K4:MOV P1,#99H
SJMP $
DELAY:MOV R6,#15
DEL2: MOV R7,#200
DJNZ R7,$
DJNZ R6,DEL2
RET
END
能把问题描述清楚看来程序实现对你来说不成问题,我用伪代码给你写出来你自己补充完整即可。void main()
{
unsigned char key,cont
bit flag = 0
初始化IO口;\\关闭红灯点亮绿灯,数码管显示0
while(1)
{
key = P1&0xF;
if(key!=0xF)
{
/*按下的按键相应绿灯关闭*/
LED_G1 = (bit)(key&0x1)
LED_G2 = (bit)(key&0x2)
LED_G3 = (bit)(key&0x4)
LED_G4 = (bit)(key&0x8)
/*按下的按键相应红色LED闪烁*/
for(cont=0cont<5cont++)
{
LED_R1 = (~LED_G1)|flag
LED_R2 = (~LED_G2)|flag
LED_R3= (~LED_G3)|flag
LED_R4 = (~LED_G4)|flag
Delay()//延时
flag = ~flag
}
关闭所有红色LED并开启所有绿色LED;
}
}
}
注意:代码是允许有同时按下的情况,当然也可以改为不允许同时按下的情况发生,即如果发生同时按下则随便选择一个按键,这样觉得有点不公平。
#include<reg51.h>#defineuint unsigned int
#defineuchar unsigned char
sbitspeaker = P2^7
sbit key0 =P1^0
sbit key1 =P1^1
sbit key2 =P1^2
sbit key3 =P1^3
sbit key4 =P1^4
sbit key5 =P1^5
sbit key6 =P1^6
sbit key7 =P1^7
ucharled[8]={0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80}
voiddelay_1ms(uint t)
{
uchar m,n
for(m=tm>0m--)
for(n=330n>0n--)
}
bitiskeyinput()//判断是否有按键闭合
{
if((P1 &0xff) ==0xff ) //屏蔽高4位(列线),只检测低4位(行线)
return 0 //没有键闭合,返回0
else
return 1 //有键闭合,返回1
}
ucharkey_identify()//识别键号
{
uchar key
if( key0==0 )key=0
if(key1==0 )key=1
if( key2==0 )key=2
if( key3==0 )key=3
if( key4==0 )key=4
if( key5==0 )key=5
if( key6==0 )key=6
if( key7==0 )key=7
return key//输出键号
}
voidkeyprocess(uchar keynum)
{
uint i
P0 = led[keynum]
i=500
while(i--)
{
speaker = 0
delay_1ms(10)
speaker = 1
}
}
void main()
{
uchar keynum
P0 = 0xFF
while(1)
{
while (!iskeyinput() )//如果没有键,则等待
delay_1ms(10) //去除键抖动
if(iskeyinput() ) //当前有按键,需要识别按键并且等待键盘松开
{
keynum= key_identify()//识别键号
keyprocess(keynum)
while(1)//等待复位
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)