📄 rtc.c
字号:
void RTC_SetOverflowInt(RTC_CALLBACK_T rtc_cb)
{
rtc_CB_Save[RTC_OWEN_INT].callback_para = NULL;
rtc_CB_Save[RTC_OWEN_INT].rtc_callback = rtc_cb;
RTC_WaitForLastWrite(); //wait the last write action complete
RTC_CRL |= 0x01<<4; //set RTC unit to config mode
RTC_WaitForLastWrite();
RTC_CRH |=RTC_RTOWEN_Mask|RTC_RTGEN_Mask; //
RTC_WaitForLastWrite();
RTC_CRL &= ~(0x01<<4); //config is complete ,set RTC uint to free running mode
}
/*********************************************************************************************************
;** 函数名称: RTC_Clr_Overflow_Int
;** 功能描述:清除时钟溢出处理
;**
;** 参 数:无
;**
;** 返 回 值:无
;**
;** 作 者:lhl
;** 日 期: 2006年5月10日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void RTC_ClrOverflowInt()
{
rtc_CB_Save[RTC_OWEN_INT].callback_para = NULL;
rtc_CB_Save[RTC_OWEN_INT].rtc_callback = NULL;
RTC_WaitForLastWrite(); //wait the last write action complete
RTC_CRL |= 0x01<<4; //set RTC unit to config mode
RTC_WaitForLastWrite();
RTC_CRH &= ~(RTC_RTOWEN_Mask|RTC_RTGEN_Mask); //
RTC_WaitForLastWrite();
RTC_CRL &= ~(0x01<<4); //config is complete ,set RTC uint to free running mode
}
/*********************************************************************************************************
;** 函数名称: RTC_Interrupt_Enter
;** 功能描述: RTC模块中断入口函数
;**
;** 参 数:无
;**
;** 返 回 值: 无
;**
;** 作 者:lhl
;** 日 期: 2006年5月10日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void RTC_InterruptEnter()
{
UINT16 ints_reg,inte_reg;
INT8 i;
RTC_WaitForLastWrite(); //wait the last write action complete
inte_reg = RTC_CRH; //Save the int mask
RTC_WaitForLastWrite();
RTC_CRH &= 0xfff0; //disable rtc irq
RTC_WaitForLastWrite();
ints_reg = RTC_CRL;
for(i = 0;(ints_reg&0x01)&&(i<4);ints_reg = ints_reg>>1) //find out occured int
{
if(rtc_CB_Save[i].rtc_callback != NULL) //if cb is exist
rtc_CB_Save[i].rtc_callback(rtc_CB_Save[i].callback_para); //execute the callback programs
}
RTC_WaitForLastWrite();
RTC_CRL = 0x0000; //clr the static of int
RTC_WaitForLastWrite();
RTC_CRH = inte_reg; //restore the int mask
}
/*********************************************************************************************************
;** 函数名称: RTC_Init
;** 功能描述: 初始化RTC模块
;**
;** 参 数:RTC的预分频,目的使RTC时钟的频率为1HZ。
;** 本系统采用32768hz晶振,采用32768作为预分频
;**
;** 返 回 值: 无
;**
;** 作 者:lhl
;** 日 期: 2006年5月10日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void RTC_Init(UINT32 prescale)
{
UINT16 value;
RTC_WaitForLastWrite(); //wait the last write action complete
RTC_CRL |= 0x01<<4; //set RTC unit to config mode
RTC_WaitForLastWrite(); //wait the last write action complete
value = (UINT16)((prescale&0xffff0000)>>16);
RTC_PRLH = value; //set prescale register high bits
RTC_WaitForLastWrite(); //wait the last write action complete
value = (UINT16)(prescale&0x0000FFFF);
RTC_PRLL = value; //set prescale register low bits
RTC_WaitForLastWrite();
RTC_CRL &= 0xfff0; //clear the status of interrupt request signals.
RTC_WaitForLastWrite();
RTC_CRH &= 0xfff0; //clear all interrupt mask .
RTC_WaitForLastWrite();
RTC_CRL &= ~(0x01<<4); //config is complete ,set RTC uint to free running mode
}
/*********************************************************************************************************
;** 函数名称: RTC_String2Time
;** 功能描述: 将YYMMDDHHMMSS时间日期格式转换为UNIX 32bit时间格式
;**
;** 参 数:将要转换的YYMMDDHHMMSS时间
;**
;** 返 回 值: UNIX 32bit时间
;**
;** 作 者:lhl
;** 日 期: 2006年5月10日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
Time_T RTC_String2Time(RTC_Time_T *rtc_time)
{
//INT16 secs, mins, hours, months, years;
INT16 i, nly;
Time_T t;
UINT32 days;
RTC_Time_T *ret = rtc_time;
/* First, change days, hours, minutes and seconds. into seconds*/
t = (((ret->day-1)*24 +ret->hour)*60 +ret->minute)*60 +ret->second;
/*calculate the days from years*/
nly = ((ret->year-1970) + 1) / 4; //find out leap years
days = (ret->year-1970) * 365 +nly; //calculate the days in the pass years
if(((ret->year-1970) +2)%4 == 0)
{
for(i =0;i<(ret->month-1);i++)
days += m4[i];
}
else
{
for(i =0;i<(ret->month-1);i++)
days += m[i];
}
t += (UINT32)days*86400;
return t;
}
/*********************************************************************************************************
;** 函数名称: RTC_Time2String
;** 功能描述: 将UNIX 32bit时间格式转换为YYMMDDHHMMSS时间日期格式
;**
;** 参 数:1、将要转换的UNIX 32bit时间指针2、YYMMDDHHMMSS时间格式指针
;**
;** 返 回 值: 无
;**
;** 作 者:lhl
;** 日 期: 2006年5月10日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void RTC_Time2String(Time_T time,RTC_Time_T *rtc_time)
{
INT16 secs, mins, hours, days, months, years;
INT16 i, nly;
const UINT8 *days_in_month;
Time_T t = time;
RTC_Time_T *ret = rtc_time;
/* First, divide into days, hours, minutes and seconds. */
days = (INT16)(t / 86400);
t = t % 86400;
hours = (INT16)(t / 3600);
t = t % 3600;
mins = (INT16)(t / 60);
secs = (INT16)(t % 60);
/* Now the number of days has to be divided into years and months.
* Start by approximating each year to be 365 days.
* This approximation will be at most one off.
* Compensate if necessary. */
years = (INT16)(days / 365);
days = (INT16)(days % 365);
/* In this interval (1970 - 2037), every fourth year is a leap year,
* without exceptions, starting with 1972. */
nly = (INT16)((years + 1) / 4);
if (days < nly)
{
years--;
days = (INT16)(days + 365 - nly);
}
else
{
days -= nly;
}
/* To determine the month we simply do a linear search through
* an array holding the number of days of each month.
* First we have to select the correct array though,
* there is one for leap years and one for non-leap years. */
if (((years + 2) % 4) == 0)
days_in_month = m4;
else
days_in_month = m;
for (i = 0; i < 12; i++)
{
if (days < days_in_month[i])
break;
days -= days_in_month[i];
}
months = i;
/* We are done. The variable "years" holds the number of complete
* years that have passed since 1970; "months" holds the number
* of complete months that have passed since the beginning of
* the present year; and similarly for the other variables. */
ret->year = years + 1970;
ret->month = months + 1;
ret->day = days + 1;
ret->hour = hours;
ret->minute = mins;
ret->second = secs;
// sprintf (result, "%02d%02d%02d%02d%02d%02d\n",
// (years + 1970) % 100, months + 1, days + 1, hours, mins, secs);
}
void RTC_WaitForLastWrite(void)
{
UINT16 value;
UINT16 cnt = 0;
value = RTC_CRL;
/* Loop until the Last operation Completion */
while (!(value& RTC_RTOFF_Mask))
{
if(cnt++>0x1000)
return ;
value = RTC_CRL;
}
return ;
}
#endif //EN_ARM_RTC > 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -