定时计数器T0作定时应用技术(一)

发布者:hylh2008最新更新时间:2011-02-27 手机看文章 扫描二维码
随时随地手机看文章

1.实验任务

AT89S51单片机的定时/计数器T0产生一秒的定时时间,作为秒计数时间,当一秒产生时,秒计数加1,秒计数到60时,自动从0开始。硬件电路如下图所示

2.电路原理图

4.15.1

3.系统板上硬件连线

(1.       把“单片机系统”区域中的P0.0/AD0P0.7/AD7端口用8芯排线连接到“四路静态数码显示模块”区域中的任一个ah端口上;要求:P0.0/AD0对应着aP0.1/AD1对应着b……P0.7/AD7对应着h

(2.       把“单片机系统”区域中的P2.0/A8P2.7/A15端口用8芯排线连接到“四路静态数码显示模块”区域中的任一个ah端口上;要求:P2.0/A8对应着aP2.1/A9对应着b……P2.7/A15对应着h

4.程序设计内容

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=(21650000MOD 256

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

5.程序框图

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4.15.2

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

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

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

#include

 

unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,

                               0x66,0x6d,0x7d,0x07,

                               0x7f,0x6f,0x77,0x7c,

                               0x39,0x5e,0x79,0x71,0x00};

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

 

unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,

                               0x66,0x6d,0x7d,0x07,

                               0x7f,0x6f,0x77,0x7c,

                               0x39,0x5e,0x79,0x71,0x00};

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

{

  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;

}

引用地址:定时计数器T0作定时应用技术(一)

上一篇:可预置可逆4位计数器
下一篇: 4×4矩阵式键盘识别技术

小广播
添点儿料...
无论热点新闻、行业分析、技术干货……
设计资源 培训 开发板 精华推荐

最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

换一换 更多 相关热搜器件
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved