原创 12.可预置可逆4位计数器

2008-10-18 20:08 2322 3 3 分类: MCU/ 嵌入式

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


利用AT89S51单片机的P1.0P1.3接四个发光二极管L1L4,用来指示当前计数的数据;用P1.4P1.7作为预置数据的输入端,接四个拨动开关K1K4,用P3.6/WRP3.7/RD端口接两个轻触开关,用来作加计数和减计数开关。具体的电路原理图如下图所示


2.  电路原理图


点击看大图


 


3、实现程序


1.  汇编源程序


COUNT              EQU 30H


                            ORG 00H


START:               MOV A,P3


                           ANL A,#0FH


                            MOV COUNT,A


                            MOV P1,A


SK2:           JB P3.6,SK1


                           LCALL DELY10MS


                            JB P3.6,SK1


                            INC COUNT


                           MOV A,COUNT


                            CJNE A,#16,NEXT


                           MOV A,P3


                            ANL A,#0FH


                            MOV COUNT,A


NEXT:                MOV P1,A


WAIT:                 JNB P3.6,WAIT


                           LJMP SK2


SK1:          JB P3.7,SK2


                            LCALL DELY10MS


                            JB P3.7,SK2


                           DEC COUNT


                            MOV A,COUNT


                            CJNE A,#0FFH,NEX


                           MOV A,P3


                            ANL A,#0FH


                            MOV COUNT,A


NEX:                   MOV P1,A


WAIT2:               JNB P3.7,WAIT2


                           LJMP SK2


DELY10MS:       MOV R6,#20


                            MOV R7,#248


D1:                      DJNZ R7,$


                            DJNZ R6,D1


                           RET


                            END


2.  C语言源程序


#include <AT89X51.H>


 


unsigned char curcount;


 


void delay10ms(void)


{


  unsigned char i,j;


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


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


}


 


 


void main(void)


{


  curcount=P3 & 0x0f;       //读入P3口开关状态,即读入预置数


  P1=~curcount;                //按位取非


  while(1)


    {


      if(P3_6==0)


        {


          delay10ms();


          if(P3_6==0)


            {


              if(curcount>=15)


                {


                  curcount=0;


                }


                else


                  {


                    curcount++;


                  }


               P1=~curcount;


               while(P3_6==0);


            }


        }


      if(P3_7==0)


        {


          delay10ms();


          if(P3_7==0)


            {


              if(curcount<=0)


                {


                  curcount=15;


                }


                else


                  {


                    curcount--;


                  }


               P1=~curcount;


               while(P3_7==0);


            }


        }


    }


}


点击看大图

 

 

 

 

 

 

 
PARTNER CONTENT

文章评论0条评论)

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