返回主页  单片机教程  XL2000开发板  单片机学习  自制编程器  单片机资料  电子技术 产品介绍  如何购买  进入论坛

第一章:硬件资源模块第二章:keilc软件使用
at89s51单片机实验及实践课题     at89s51单片机实验及实践课题
1. 闪烁灯2.模拟开关灯
3.多路开关状态指示4.广告灯的左移右移
5.广告灯(利用取表方式)6.报警产生器
7.I/O并行口直接驱动LED显示8.按键识别方法之一
9. 一键多功能按键识别技术10. 00-99计数器
11.00-59秒计时器(软件延时)12.可预置可逆4位计数器
13.动态数码显示技术14.4×4矩阵式键盘识别技术
15.定时计数器T0作定时(一)16.定时计数器T0作定时应用技术(二)
17.99秒马表设计18.“嘀、嘀、……”报警声
19.“叮咚”门铃20.数字钟(★)
21.拉幕式数码显示技术22.电子琴
23.模拟计算器数字输入及显示24.8×8LED点阵显示技术
25.点阵LED“0-9”数字显示技术26.点阵式LED简单图形显示技术
27.ADC0809A/D转换器基本应用技术28.数字电压表
29.两点间温度控制30.四位数数字温度计
31.6位数显频率计数器32.电子密码锁设计
33.4×4键盘的电子密码锁34.带有存储器功能的数字温度计-DS1624技术应用
35.DS18B20数字温度计使用

单片机c语言<>欢迎进入51单片机学习网论坛讨论

本站新域名www.8951.com开通WWW.51C51.COM 中国单片机编程技术普及推广第一站!

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

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 <AT89X51.H>

 

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 <AT89X51.H>

 

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;

}