原创 16.定时计数器T0作定时应用技术(二)

2008-10-27 21:35 2516 3 3 分类: MCU/ 嵌入式

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


AT89S51的定时/计数器T0产生2秒钟的定时,每当2秒定时到来时,更换指示灯闪烁,每个指示闪烁的频率为0.2秒,也就是说,开始L1指示灯以0.2秒的速率闪烁,当2秒定时到来之后,L2开始以0.2秒的速率闪烁,如此循环下去。0.2秒的闪烁速率也由定时/计数器T0来完成。


2.      电路原理图


点击看大图


 


3、实现程序


1.      汇编源程序


TCOUNT2S                EQU 30H


TCNT02S                    EQU 31H


ID                                 EQU 32H


                                      ORG 00H


                                      LJMP START


                                      ORG 0BH


                                      LJMP INT_T0


START:                        MOV TCOUNT2S,#00H


                                     MOV TCNT02S,#00H


                                     MOV ID,#00H


                                      MOV TMOD,#01H


                                      MOV TH0,#(65536-50000) / 256


                                      MOV TL0,#(65536-50000) MOD 256


                                      SETB TR0


                                      SETB ET0


                                      SETB EA


                                      SJMP $


INT_T0:                      MOV TH0,#(65536-50000) / 256


                                      MOV TL0,#(65536-50000) MOD 256


                                      INC TCOUNT2S


                                      MOV A,TCOUNT2S


                                      CJNE A,#40,NEXT


                                      MOV TCOUNT2S,#00H


                                      INC ID


                                      MOV A,ID


                                      CJNE A,#04H,NEXT


                                      MOV ID,#00H


NEXT:                          INC TCNT02S


                                      MOV A,TCNT02S


                                      CJNE A,#4,DONE


                                      MOV TCNT02S,#00H


                                      MOV A,ID


                                      CJNE A,#00H,SID1


                                      CPL P1.0


                                      SJMP DONE


SID1:                            CJNE A,#01H,SID2


                                      CPL P1.1


                                      SJMP DONE


SID2:                           CJNE A,#02H,SID3


                                      CPL P1.2


                                      SJMP DONE


SID3:                            CJNE A,#03H,SID4


                                      CPL P1.3


SID4:                            SJMP DONE


DONE:                        RETI


                                      END


2.      C语言源程序


#include <AT89X51.H>


 


unsigned char tcount2s;


unsigned char tcount02s;


unsigned char ID;


 


void main(void)


{


  TMOD=0x01;


  TH0=(65536-50000)/256;


  TL0=(65536-50000)%256;


  TR0=1;


  ET0=1;


  EA=1;


 


  while(1);


}


 


void t0(void) interrupt 1 using 0


{


  tcount2s++;


  if(tcount2s==40)


    {


      tcount2s=0;


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


      if(ID==4)


        {


          ID=0;


        }


    }


  tcount02s++;


  if(tcount02s==4)


    {


      tcount02s=0;  //在开始运行程序的前0.2s内是没有LED闪烁的,在到0.2s是,tcount0.2s置零


      switch(ID)


        {


          case 0:


            P1_0=~P1_0;


            break;


          case 1:


            P1_1=~P1_1;


            break;


          case 2:


            P1_2=~P1_2;


            break;


          case 3:


            P1_3=~P1_3;


            break;


        }


    }


}


 


4、总结


LED亮到不亮转换的时间间隔是0.2s,则灯亮的时间间隔是0.4s,在2s的时间间隔你可以看到LED灯亮5

PARTNER CONTENT

文章评论0条评论)

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