?? timer_module.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 + -