T2做精确时钟

T2做精确时钟

52定时器/计数器T2一些寄存器的地址

//52,定时器T2

sfr     T2MOD = 0xC9;                            //T2MOD地址

sfr16  RCAP2 = 0xCA;                   //RCAP2地址

 

//sfr              T2CON = 0xC8;          //T2CON地址

//sfr              RCAP2L= 0xCA;

//sfr              RCAP2H= 0xCB;

//sfr              TH2   = 0xCD;

//sfr              TL2   = 0xCC;

//sfr              TR2   = 0xC8^2;

//sfr              TF2   = 0xC8^7;

 

//T2初始化函数

void Timer2_Init(void)

{

TH2   = 0x4c;                     //65536-46080,设置初值46080,晶振22.1184MHz,定时25ms

TL2   = 0x00;                      //65536-46080

RCAP2H = 0x4c;                  //TH2重装值

RCAP2L = 0x00;                   //TL2重装值

 

T2CON = 0x04;                    //T2工作模式,16位自动装载定时器方式

T2MOD = 0x10;                            //自动装载向上计数

IE    |= 0xa0;

TR2   = 1;

}

 

//T2中断处理函数

void Timer2(void) interrupt 5

{

TF2 = 0;                                          // 溢出标志必须由软件清零

//EXF2 = 0;                                      // 捕获标志必须由软件清零       

T2_Cycle++;

if(T2_Cycle == 40)                        //循环40次,每次25ms,定时1s

{

           T2_Cycle = 0 ;            

           T2_S++ ;

 

           if(T2_S == 60)

           {

                    T2_S = 0;  

                    T2_M++;

 

                    if(T2_M == 60)

                    {

                             T2_M = 0;

                             T2_H++;

                                      

                             if(T2_H == 24)

                             {

                                       T2_H = 0; 

                             }

                    }

           }

}

}

以上是T2工作在自重载方式下的定时器,只要晶振很精确,T2做时钟就可以做到很精确,它的初值由硬件自重载,没有延迟,即使中断没有及时响应,计数器也正常计数。

你可能感兴趣的:(c,timer,工作,IE)