多路开关状态指示

多路开关状态指示,Switch indicator

关键字:AT89S51,开关状态指示

 
1.实验任务
如图所示,AT89S51单片机的P1.0-P1.3接四个发光二极管L1-L4,P1.4-P1.7接了四个开关K1-K4,编程将开关的状态反映到发光二极管上。(开关闭合,对应的灯亮,开关断开,对应的灯灭)。
2.电路原理图
3.系统板上硬件连线
(1.       把“单片机系统”区域中的P1.0-P1.3用导线连接到“八路发光二极管指示模块”区域中的L1-L4端口上;
(2.       把“单片机系统”区域中的P1.4-P1.7用导线连接到“四路拨动开关”区域中的K1-K4端口上;
4.  程序设计内容
(1.       开关状态检测
对于开关状态检测,相对单片机来说,是输入关系,我们可轮流检测每个开关状态,根据每个开关的状态让相应的发光二极管指示,可以采用JB P1.X,REL或JNB P1.X,REL指令来完成;也可以一次性检测四路开关状态,然后让其指示,可以采用MOVA,P1指令一次把P1端口的状态全部读入,然后取高4位的状态来指示。
(2.       输出控制
根据开关的状态,由发光二极管L1-L4来指示,我们可以用SETB P1.X和CLR P1.X指令来完成,也可以采用MOV P1,#1111XXXXB方法一次指示。
5.程序框图 (略)
6.方法一(汇编源程序)
                                      ORG 00H
START:                        MOV A,P1
                                      ANL A,#0F0H
                                      RR A
                                      RR A
                                      RR A
                                      RR A
                                      XOR A,#0F0H
                                      MOV P1,A
                                      SJMP START
                                      END
7.方法一(C语言源程序)
#include
unsigned char temp;
void main(void)
{
  while(1)
    {
      temp=P1>>4;
      temp=temp | 0xf0;
      P1=temp;
    }
}
8.方法二(汇编源程序)
                                      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
9.方法二(C语言源程序)
#include
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;
          }
    }
}
(0)

相关推荐