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

2008-10-27 21:31 2100 2 2 分类: MCU/ 嵌入式

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


AT89S51单片机的定时/计数器T0产生一秒的定时时间,作为秒计数时间,当一秒产生时,秒计数加1,秒计数到60时,自动从0开始。


2. 程序设计内容


AT89S51单片机的内部16位定时/计数器是一个可编程定时/计数器,它既可以工作在13位定时方式,也可以工作在16位定时方式和8位定时方式。只要通过设置特殊功能寄存器TMOD,即可完成。定时/计数器何时工作也是通过软件来设定TCON特殊功能寄存器来完成的。


现在我们选择16位定时工作方式,对于T0来说,最大定时也只有65536us,即65.536ms,无法达到我们所需要的1秒的定时,因此,我们必须通过软件来处理这个问题,假设我们取T0的最大定时为50ms,即要定时1秒需要经过20次的50ms的定时。对于这20次我们就可以采用软件的方法来统计了。


因此,我们设定TMOD00000001B,即TMOD01H


下面我们要给T0定时/计数器的TH0TL0装入预置初值,通过下面的公式可以计算出


TH0=(21650000) / 256


TL0=(21650000) MOD 256


T0在工作的时候,我们如何得知50ms的定时时间已到,这回我们通过检测TCON特殊功能寄存器中的TF0标志位,如果TF01表示定时时间已到。


 

3、实现程序


1. 汇编源程序(查询法)


SECOND            EQU 30H


TCOUNT           EQU 31H


                            ORG 00H


START:               MOV SECOND,#00H


                           MOV TCOUNT,#00H


                            MOV TMOD,#01H


                            MOV TH0,#(65536-50000) / 256


                           MOV TL0,#(65536-50000) MOD 256


                            SETB TR0


DISP:                  MOV A,SECOND


                            MOV B,#10


                           DIV AB


                            MOV DPTR,#TABLE


                            MOVC A,@A+DPTR


                           MOV P0,A


                            MOV A,B


                            MOVC A,@A+DPTR


                           MOV P2,A


WAIT:                 JNB TF0,WAIT


                            CLR TF0


                            MOV TH0,#(65536-50000) / 256


                           MOV TL0,#(65536-50000) MOD 256


                            INC TCOUNT


                            MOV A,TCOUNT


                           CJNE A,#20,NEXT


                            MOV TCOUNT,#00H


                            INC SECOND


                           MOV A,SECOND


                            CJNE A,#60,NEX


                           MOV SECOND,#00H


NEX:                   LJMP DISP


NEXT:                LJMP WAIT


TABLE:              DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH


                            END


2. C语言源程序(查询法)


#include <AT89X51.H>


 


unsigned char code dispcode[]={={0x3f,0x06,0x05b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};


unsigned char second;


unsigned char tcount;


 


void main(void)


{


  TMOD=0x01;


  TH0=(65536-50000)/256;


  TL0=(65536-50000)%256;


  TR0=1;


  tcount=0;


  second=0;


  P0=dispcode[second/10];


  P2=dispcode[second%10];


  while(1)


    {


      if(TF0==1)


        {


          tcount++;


          if(tcount==20)


            {


              tcount=0;


              second++;


              if(second==60)


                {


                  second=0;


                }


              P0=dispcode[second/10];


              P2=dispcode[second%10];             


            }


          TF0=0;


          TH0=(65536-50000)/256;


          TL0=(65536-50000)%256;


        }


    }


}


1.      汇编源程序(中断法)


SECOND                     EQU 30H


TCOUNT                    EQU 31H


                                     ORG 00H


                                     LJMP START


                                     ORG 0BH


                                     LJMP INT0X


START:                        MOV SECOND,#00H


                                     MOV A,SECOND


                                     MOV B,#10


                                     DIV AB


                                     MOV DPTR,#TABLE


                                     MOVC A,@A+DPTR


                                     MOV P0,A


                                     MOV A,B


                                     MOVC A,@A+DPTR


                                     MOV P2,A


                                     MOV TCOUNT,#00H


                                     MOV TMOD,#01H


                                     MOV TH0,#(65536-50000) / 256


                                     MOV TL0,#(65536-50000) MOD 256


                                     SETB TR0


                                     SETB ET0


                                     SETB EA


                                     SJMP $


INT0X:


                                     MOV TH0,#(65536-50000) / 256


                                     MOV TL0,#(65536-50000) MOD 256


                                     INC TCOUNT


                                     MOV A,TCOUNT


                                     CJNE A,#20,NEXT


                                     MOV TCOUNT,#00H


                                     INC SECOND


                                     MOV A,SECOND


                                     CJNE A,#60,NEX


                                     MOV SECOND,#00H


NEX:                                     MOV A,SECOND


                                     MOV B,#10


                                     DIV AB


                                     MOV DPTR,#TABLE


                                     MOVC A,@A+DPTR


                                     MOV P0,A


                                     MOV A,B


                                     MOVC A,@A+DPTR


                                     MOV P2,A


NEXT:                          RETI


                  


TABLE:                        DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH


                                     END


2.      C语言源程序(中断法)


#include <AT89X51.H>


 


unsigned char code dispcode[]={={0x3f,0x06,0x05b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};


unsigned char second;


unsigned char tcount;


 


void main(void)


{


  TMOD=0x01;


  TH0=(65536-50000)/256;


  TL0=(65536-50000)%256;


  TR0=1;


  ET0=1;


  EA=1;


  tcount=0;


  second=0;


  P0=dispcode[second/10];


  P2=dispcode[second%10];


  while(1);


}


 


void t0(void) interrupt 1 using 0    //定时器T0中断程序


{


  tcount++;


  if(tcount==20)


    {


      tcount=0;


      second++;


      if(second==60)


        {


          second=0;


        }


      P0=dispcode[second/10];


      P2=dispcode[second%10];             


    }


  TH0=(65536-50000)/256;


  TL0=(65536-50000)%256;


}


 


点击看大图


 
PARTNER CONTENT

文章评论0条评论)

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