原创 9.一键多功能按键识别技术

2008-10-18 16:36 1562 4 4 分类: MCU/ 嵌入式

1.实验任务<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />


如图4.9.1所示,开关SP1接在P3.7/RD管脚上,在AT89S51单片机的P1端口接有四个发光二极管,上电的时候,L1接在P1.0管脚上的发光二极管在闪烁,当每一次按下开关SP1的时候,L2接在P1.1管脚上的发光二极管在闪烁,再按下开关SP1的时候,L3接在P1.2管脚上的发光二极管在闪烁,再按下开关SP1的时候,L4接在P1.3管脚上的发光二极管在闪烁,再按下开关SP1的时候,又轮到L1在闪烁了,如此轮流下去。


2.电路原理图


点击看大图


 

3、实现程序


1.汇编源程序


ID                                 EQU 30H


SP1                               BIT P3.7


L1                                 BIT P1.0


L2                                 BIT P1.1


L3                                 BIT P1.2


L4                                 BIT P1.3


                                     ORG 0


                                     MOV ID,#00H


START:                        JB K1,REL


                                     LCALL DELAY10MS


                                     JB K1,REL


                                     INC ID


                                     MOV A,ID


                                     CJNE A,#04,REL


                                     MOV ID,#00H


REL:                    JNB K1,$


                                     MOV A,ID


                                     CJNE A,#00H,IS0


                                     CPL L1


                                     LCALL DELAY


                                     SJMP START


IS0:                               CJNE A,#01H,IS1


                                     CPL L2


                                     LCALL DELAY


                                     SJMP START


IS1:                               CJNE A,#02H,IS2


                                     CPL L3


                                     LCALL DELAY


                                     SJMP START


IS2:                               CJNE A,#03H,IS3


                                     CPL L4


                                     LCALL DELAY


                                     SJMP START


IS3:                               LJMP START


DELAY10MS:    MOV R6,#20


LOOP1:                        MOV R7,#248


                                     DJNZ R7,$


                                     DJNZ R6,LOOP1


                                     RET


DELAY:                        MOV R5,#20


LOOP2:                        LCALL DELAY10MS


                                     DJNZ R5,LOOP2


                                     RET


                                     END


2.C语言源程序


#include <AT89X51.H>


unsigned char ID;


void delay10ms(void)


{


  unsigned char i,j;


  for(i=20;i>0;i--)


  for(j=248;j>0;j--);


}


 


void delay02s(void)


{


  unsigned char i;


  for(i=20;i>0;i--)


    {delay10ms();


    }


}


 


void main(void)


{ while(1)


    { if(P3_7==0)


        {delay10ms();


          if(P3_7==0)


            {


              <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />ID++;


              if(ID==4)


                {


                  ID=0;


                }


              while(P3_7==0);


            }


        }


      switch(ID)


        { case 0:


            P1_0=~P1_0;


            delay02s();


            break;            //break直接跳出switch


          case 1:


            P1_1=~P1_1;


            delay02s();


            break;


          case 2:


            P1_2=~P1_2;


            delay02s();


            break;


          case 3:


            P1_3=~P1_3;


            delay02s();


            break;


        }


    }


}


点击看大图

 


注:程序开始按键没有按下,ID=0,if判断中的语句都不执行,直接到switch()函数,对应ID=0,P1.0取反然后跳出switch(),由于while(1),则继续到if()函数检测有无按键按下,仍然没有的话,再到switch()中P1.0取反,即开始是P1.0每间隔0.2S左右闪烁一次。有按键按下时则对应的LED会闪烁。


     程序不足是当程序不能及时反应键盘的按下,如果快速按下按键,LED会不响应,因为有可能在程序执行延迟函数期间就快速按下和弹起按键了,主程序没有检测到按键


 


 

 

 
PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
4
关闭 站长推荐上一条 /3 下一条