首 页文档资料下载资料维修视频包年699元
请登录  |  免费注册
当前位置:精通维修下载 > 文档资料 > 家电技术 > 单元电路介绍 > 其它电路
51单片机T0定时器应用1
来源:本站整理  作者:佚名  2011-12-16 06:33:27



1.实验任务
  用AT89S51单片机的定时/计数器T0产生一秒的定时时间,作为秒计数时间,当一秒产生时,秒计数加1,秒计数到60时,自动从0开始。硬件电路如下图所示
2.电路原理
定时计数器T0作定时应用技术(一)
                        图4.15.1
3.系统板上硬件连线
(1).把“单片机系统”区域中的P0.0/AD0-P0.7/AD7端口用8芯排线连接到“四路静态数码显示模块”区域中的任一个a-h端口上;要求:P0.0/AD0对应着a,P0.1/AD1对应着b,……,P0.7/AD7对应着h。
(2).把“单片机系统”区域中的P2.0/A8-P2.7/A15端口用8芯排线连接到“四路静态数码显示模块”区域中的任一个a-h端口上;要求:P2.0/A8对应着a,P2.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次我们就可以采用软件的方法来统计了。
  因此,我们设定TMOD=00000001B,即TMOD=01H
  下面我们要给T0定时/计数器的TH0,TL0装入预置初值,通过下面的公式可以计算出
TH0=(216-50000) / 256
TL0=(216-50000) MOD 256
  当T0在工作的时候,我们如何得知50ms的定时时间已到,这回我们通过检测TCON特殊功能寄存器中的TF0标志位,如果TF0=1表示定时时间已到。
5.程序蛲?br />定时计数器T0作定时应用技术(一)
             图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)%6;
  TR0=1;
  tcount=0;
  second=0;
  P0=dispcode[second/10];
  P2=dispcode[second];
  while(1)
    {
      if(TF0==1)
        {
          tcount++;
          if(tcount==20)
            {
              tcount=0;
              second++;
              if(second==60)
                {
                  second=0;
                }
              P0=dispcode[second/10];
              P2=dispcode[second];             
            }
          TF0=0;
          TH0=(65536-50000)/256;
          TL0=(65536-50000)%6;
        }
    }
}
8.汇编源程序(中断法)
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
9.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)%6;
  TR0=1;
  ET0=1;
  EA=1;
  tcount=0;
  second=0;
  P0=dispcode[second/10];
  P2=dispcode[second];
  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];             
    }
  TH0=(65536-50000)/256;
  TL0=(65536-50000)%6;
}

关键词:

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分)
Copyright © 2007-2017 down.gzweix.Com. All Rights Reserved .
页面执行时间:70,121.09000 毫秒