原创 3、多路开关状态指示

2008-10-18 09:19 2196 2 2 分类: MCU/ 嵌入式

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


如图4.3.1所示,AT89S51单片机的P1.0P1.3接四个发光二极管L1L4P1.4P1.7接了四个开关K1K4,编程将开关的状态反映到发光二极管上。(开关闭合,对应的灯亮,开关断开,对应的灯灭)。


2. 电路原理图


点击开大图


3、程序实现


1. 方法一(汇编源程序)


                                      ORG 00H


START:                        MOV A,P1         ;


                                      RR A


                                      RR A


                                      RR A


                                      RR A


                                      ORL A,#0F0H


                                      MOV P1,A


                                      SJMP START


                                      END


2. 方法一(C语言源程序)


#include <AT89X51.H>


unsigned char temp;


void main(void)


{


  while(1)


    {


      temp=P1>>4;        //P1.76543210=0000+四个开关状态


      temp=temp | 0xf0; //P1.76543210=1111+四个开关状态


      P1=temp;


    }


}


点击开大图


3. 方法二(汇编源程序)


                                      ORG 00H


START:                         JB P1.4,NEXT1


                                      CLR P1.0


                                      SJMP NEX1


NEXT1:                       SETB P1.0


NEX1:                          JB P1.5,NEXT2


                                      CLR P1.1


                                      SJMP NEX2


NEXT2:                       SETB P1.1


NEX2:                          JB P1.6,NEXT3


                                      CLR P1.2


                                      SJMP NEX3


NEXT3:                       SETB P1.2


NEX3:                          JB P1.7,NEXT4


                                      CLR P1.3


                                      SJMP NEX4


NEXT4:                       SETB P1.3


NEX4:                          SJMP START


                                      END


4. 方法二(C语言源程序)


#include <AT89X51.H>


 


void main(void)


{


  while(1)


    {


      if(P1_4==0)


        {


          P1_0=0;


        }


        else


          {


            P1_0=1;


          }


      if(P1_5==0)


        {


          P1_1=0;


        }


        else


          {


            P1_1=1;


          }


      if(P1_6==0)


        {


          P1_2=0;


        }


        else


          {


            P1_2=1;


          }


      if(P1_7==0)


        {


          P1_3=0;


        }


        else


          {


            P1_3=1;


          }


    }


}


4、总结


  多个LED和多个开关在同一个IO口上,最简便的方法是通过左右移位、与或操作来实现。汇编和C语言编程思想是相同的


 
PARTNER CONTENT

文章评论0条评论)

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