⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer_module.c

📁 一个电表的程序
💻 C
字号:
/*===========================================================================
* 			 2006年。三星仪表SOC芯片开发项目
*
*
* 文件名称:Timer_Module.c
* 文件标识:
* 摘    要:    产生系统所有时钟
*
*
* 当前版本:Ver 1.0
* 作    者:罗冬生
* 修改内容:
* 修改日期:
* 完成日期:

===============================================================================================*/
#ifndef  _Timer_Module_
#define  _Timer_Module_
//===============================================================================================

#include "includes.h"

//===============================================================================================

//      定时器0初始化

/*
 unsigned char TMOD;
  struct
  {
    unsigned char M00 : 1;
    unsigned char M10 : 1;
    unsigned char C_T0 : 1;
    unsigned char Gate0 : 1;
    unsigned char M01 : 1;
    unsigned char M11 : 1;
    unsigned char C_T1 : 1;
    unsigned char Gate1 : 1;
  } TMOD_bit;

  {
    unsigned char IT0 : 1;
    unsigned char IE0 : 1;
    unsigned char IT1 : 1;
    unsigned char IE1 : 1;
    unsigned char TR0 : 1;
    unsigned char TF0 : 1;
    unsigned char TR1 : 1;
    unsigned char TF1 : 1;
  } TCON_bit;

 struct
  {
    unsigned char EX0 : 1;
    unsigned char ET0 : 1;
    unsigned char EX1 : 1;
    unsigned char ET1 : 1;
    unsigned char ES : 1;
    unsigned char ET2 : 1;
    unsigned char ETEMP : 1;
    unsigned char EA : 1;
  } IE_bit;

#define Timer_Len  5

 */





//------------------------------------------------------------------------------------------

//    RTC计时缓冲区
/*
__idata union
{
  unsigned char TimeBuffer[7];
  struct
  {
   unsigned char   Second;     //秒
   unsigned char   Minute;     //分
   unsigned char   Hour;       //时
   unsigned char   Week;       //周
   unsigned char   Date;       //日
   unsigned char   Month;      //月
   unsigned char   Year;       //年
  };
};
*/

//------------------------------------------------------------------------------------------

//    计时器-缓冲区
/*
__idata union
{
  unsigned char Timers[6];
  struct
  {
   unsigned char   Comm_Time;         // 通信接收与发送之间延时(MS)
   unsigned char   CommByte_Time;     // 通信字节之间延时(ms)
   unsigned char   CommHWDelyTime;    // 红外通信时间(ms)
   unsigned char   Disp_Time;         // LCD显示计时(s)
   unsigned char   Key_Time;          // 编程按键有效时间(min)
   unsigned char   MinCount;          // 分计时(s)

  };
};

 */
//=========================================================================================================================

void Timer0_Init(void)
{
  TMOD =  (TMOD & 0xF0) | 0x01;     //M1、M0=01(定时器0设置16位定时模式),C_T0=0(定时模式),Gate0=0(不需要引脚控制)
  TH0 = 0x60;                       // Fosc=4.096MHz,产生10ms中断,计数值为:40960个脉冲,TH,TL值为6000H
  TL0 = 0x00;
  TCON_bit.TR0 = 1;                 //定时计数器0控制(启动计时)

  IP_bit.PT0 = 0;                   //定时器0中断优先级别为"低级"
  IE_bit.ET0 = 1;                   //定时器0中断控制(中断使能)
}

//-------------------------------------------------------------------------------------------------------------------------

//      定时器0中断程序 ,10ms产生一次中断

#pragma vector=0x0b
__interrupt void TIMER0_ISR(void)
{
  TCON_bit.TF0 = 0;
  Timer0_Init();
  WatchDog_Flg = 1;                 //置喂狗标志


  if (CommByte_Time)                // 通信字节之间延时
  {
    CommByte_Time--;
  }




  if (CommHWDelyTime);    // 红外通信时间(ms)
  {
     CommHWDelyTime--;
     if (!CommHWDelyTime)
     {
       EPCFG_bit.MODE38_TxD = 0;    // 关闭发送到端口的38KHz输出。
       IR_CTRL =0;                  // 红外通信使能控制
       RS485_CTRL = 1;              // RS585通信使能控制
       TCON_bit.IE0 = 1;            // 外部输入0中断使能
     }
  }
}



//===============================================================================================

//      RTC初始化

void RTC_Init(void)
{
    INTVAL= 1;
    TIMECON = (TIMECON & 0x84) | 0x53;     // 24小时,

    IEIP2_bit.PTI = 0;              //RTC中断优先级别为"高"
    IEIP2_bit.ETI = 1;              //Enable RTC interrupt (RTC中断使能)

}

//------------------------------------------------------------------------------------------------

//          RTC中断程序:(1)24小时过"00:00:00" ,(2)秒中断

 #pragma vector = 0x53
__interrupt void RTC_ISR(void)
{
    if(TIMECON_bit.ALARM)        //RTC 间接报警标志(设置为1秒)
    {
        TIMECON_bit.ALARM = 0;
        RTC_Sec_Flg = 1;

        if  (Disp_Time)       // LCD显示计时-1
           Disp_Time--;

        if (SecCount)
           SecCount--;
        else
        {
          SecCount = 60;
          if (Key_Time);
          {
             Key_Time--;          // 编程按键有效时间-1
             if (!Key_Time)
               Key_Flg = 0;
          }

        }



    }
    if(TIMECON_bit.MIDNIGHT)     // 过零点钟中断标志
    {
        TIMECON_bit.MIDNIGHT = 0;
        RTC_Date_Flg   = 1;
    }
}
//===============================================================================================

__code const unsigned char TabMonth[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

//      设置时钟

void SetTime(void)
{
  SEC = BCD2Hex(Second);
  MIN = BCD2Hex(Minute);
  HOUR = BCD2Hex(Hour);
}


//      从RTC读取时间,更新时钟数据区

void TimeUpdate(void)
{
    Second=Hex2BCD(SEC);
    Minute=Hex2BCD(MIN);
    Hour =Hex2BCD(HOUR);
 }

//===============================================================================================

//        更新日期数据区

//===============================================================================================


void DateUpdate(void)
{
  Date  = BCD2Hex(Date);
  Month = BCD2Hex(Month);
  Year  = BCD2Hex(Year);
  Date++;
  if(Date > TabMonth[Month])
    {
      if(((Year & 0x03)==0x00)&&(Month == 2)&&(Date == 29))
        {
            ;//if it is a leap year and today is Feb.29,do nothing.
        }
      else
        {
            Date = 1;
            Month++;
            if(Month > 12)
            {
                Month = 1;
                Year++;
                if(Year > 99)
                {
                   Year = 0;
                }
            }
        }
    }
  Date  = Hex2BCD(Date);
  Month = Hex2BCD(Month);
  Year  = Hex2BCD(Year);
}


//==============================================================================

void TimeModule(void)
{
   if (RTC_Date_Flg )
   {
     RTC_Date_Flg = 0;    //清过零标志
     DateUpdate();        //更新年月日

     TimeSum =DataSum(rSysTmDt_Dtr,7);
     MoveData_Api((unsigned int)(&rSysTmDt_Dtr[0])+iRAM_Adr, eSysTmDt_Dtr+e2RAM_Adr, 8);
   }
}


//===============================================================================================
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -