首 页文档资料下载资料维修视频包年699元
请登录  |  免费注册
当前位置:精通维修下载 > 文档资料 > 家电技术 > 维修教程知识 > 单片机栏
定时计数器T0作定时应用技术(二)
来源:本站整理  作者:佚名  2010-01-25 18:56:53



1.实验任务
  用AT89S51的定时/计数器T0产生2秒钟的定时,每当2秒定时到来时,更换指示灯闪烁,每个指示闪烁的频率为0.2秒,也就是说,开始L1指示灯以0.2秒的速率闪烁,当2秒定时到来之后,L2开始以0.2秒的速率闪烁,如此循环下去。0.2秒的闪烁速率也由定时/计数器T0来完成。
2.电路原理图
定时计数器T0作定时应用技术(二)
                图4.16.1
3.系统板硬件连线
(1).把“单片机系统”区域中的P1.0-P1.3用导线连接到“八路发光二极管指示模块”区域中的L1-L4上
4.程序设计内容
(1).由于采用中断方式来完成,因此,对于中断源必须它的中断入口地址,对于定时/计数器T0来说,中断入口地址为000BH,因此在中断入口地方加入长跳转指令来执行中断服务程序。书写汇编源程序格式如下所示:
                            ORG 00H
                            LJMP START
                            ORG 0BH        ;定时/计数器T0中断入口地址
                            LJMP INT_T0
START:               NOP          ;主程序开始
                            .
                            .
 
INT_T0:             PUSH ACC                  ;定时/计数器T0中断服务程序
                            PUSH PSW
                            .
                            .
                            POP PSW
                            POP ACC
                            RETI                   ;中断服务程序返回
                            END
(2).定时2秒,采用16位定时50ms,共定时40次才可达到2秒,每50ms产生一中断,定时的40次数在中断服务程序中完成,同样0.2秒的定时,需要4次才可达到0.2秒。对于中断程序,在主程序中要对中断开中断。
(3).由于每次2秒定时到时,L1-L4要交替闪烁。采用ID来号来识别。当ID=0时,L1在闪烁,当ID=1时,L2在闪烁;当ID=2时,L3在闪烁;当ID=3时,L4在闪烁
5.程序框图
T0中断服务程序框图
定时计数器T0作定时应用技术(二)
主程序框图
          定时计数器T0作定时应用技术(二)
定时计数器T0作定时应用技术(二)
      图4.16.2
6.汇编源程序
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
7.C语言源程序
#include
unsigned char tcount2s;
unsigned char tcount02s;
unsigned char ID;
void main(void)
{
  TMOD=0x01;
  TH0=(65536-50000)/256;
  TL0=(65536-50000)%6;
  TR0=1;
  ET0=1;
  EA=1;
  while(1);
}
void t0(void) interrupt 1 using 0
{
  tcount2s++;
  if(tcount2s==40)
    {
      tcount2s=0;
      ID++;
      if(ID==4)
        {
          ID=0;
        }
    }
  tcount02s++;
  if(tcount02s==4)
    {
      tcount02s=0;
      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;
        }
    }

关键词:

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

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