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

📄 lpc_rtc.c

📁 给大家提供一个在inram/exram中调试的示例,在周公的lpc2200上调试过.
💻 C
📖 第 1 页 / 共 2 页
字号:
/*************************************************************************
 *         BU MMS China, Philips Semiconductor Software Support
 *         Embest info&Tech Co. Software Support
 *---------------------------------------------------------------------------
 * The software is delivered "AS IS" without warranty or condition of any
 * kind, either express, implied or statutory.  Everybody can use it as 
 * it is opened and without copyright. We will not take any law responsibility
 * for any problem produced by using this software.
 *---------------------------------------------------------------------------                                                    
 *    File name:	 LPC_Rtc.c                                                            
 *    Description:	 Define API for RTC
 *       
 *    History:      
 *    1. Date:		 Aug 10, 2004                                              
 *       Author:		 Shawn Zhang                                                   
 *       Description:	 Create 
 *
 **************************************************************************/

#include "LPC_Rtc.h"

static LPC_Rtc_DateTime_t RTC_InitDateTime = {2004, 3, 15, 0, 0, 0};
static char *RTC_DOWTbl[] = {
	"Sunday ",
	"Monday ",
	"Tuesday ",
	"Wednesday ",
	"Thursday ",
	"Friday ",
	"Saturday "
};
static char *RTC_MonthTbl[] = {
	"",
	"January ",
	"February ",
	"Match ",
	"April ",
	"May ",
	"June ",
	"July ",
	"August ",
	"September ",
	"October ",
	"November ",
	"December "
};
static int RTC_MonthVal[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};


int RTC_SetDateTime (LPC_Rtc_DateTime_t *pDateTime);
/*************************************************************************
 * Function Name: IsLeapYear
 * Parameters: lpc_int16 Year
 *
 * Return: bool 
 *
 * Description: Judge whether the specifying year is leap year. 
 *  
 *************************************************************************/
static bool IsLeapYear (lpc_int16 year)
{
	if (!(year%4) && (year%100) || !(year%400))
		return true;
	else
		return false;
}

/*************************************************************************
 * Function Name: GetDOY
 * Parameters: lpc_int16 Year
 *			lpc_int8 month
 *			lpc_int8 day
 *
 * Return: int 
 *
 * Description: Get the day of year according to the date
 *  
 *************************************************************************/
static int GetDOY (lpc_int16 year, lpc_int8 month, lpc_int8 day)
{
	int DOY=0, i;

	for(i=1; i<month; i++)
		DOY+=RTC_MonthVal[i];
	if (month>2)
		if (IsLeapYear(year))
			DOY++;

	return (DOY+day);	
}

/*************************************************************************
 * Function Name: GetDOW
 * Parameters: lpc_int16 Year
 *			lpc_int8 month
 *			lpc_int8 day
 *
 * Return: int -- (0~6)
 *
 * Description: Get the day of week according to the date.
 *
 * NOTE: Year is not smaller than RTC_YEARMIN (1901).
 *  
 *************************************************************************/
static int GetDOW (lpc_int16 year, lpc_int8 month, lpc_int8 day)
{
	int i = RTC_BASEYEAR, DOW = 0;

	for (i = RTC_BASEYEAR, DOW = 0; i < year; i++)
	{
		DOW +=365;
		if  (IsLeapYear(i))
			DOW++;
	}

	DOW +=  GetDOY (year, month, day) - 1;
	DOW = (DOW + RTC_BASEDOW) % 7;

	return DOW;	
}

/*************************************************************************
 * Function Name: IsValidDay
 * Parameters: lpc_int16 Year
 *			lpc_int8 month
 *			lpc_int8 day
 *
 * Return: 
 *		false -- not valid day
 *		true -- valid day
 *
 * Description: Check if the specify day is valid
 *
 * NOTE: Year is not smaller than RTC_YEARMIN (1901).
 *  
 *************************************************************************/
bool IsValidDay (lpc_int16 year, lpc_int8 month, lpc_int8 day)
{
	/* Valid Judge */
	if (year < RTC_YEARMIN || year > RTC_YEARMAX ||day <1)
		return false;

	switch(month)
	{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if (day>31)
				return false;
			else
				break;
		case 4:
		case 6:
		case 9:
		case 11:
			if (day>30)
				return false;
			else
				break;
		case 2:
			if (IsLeapYear(year))
				if (day>29)
					return false;
				else
					break;
			else
				if (day>28)
					return false;
				else
					break;
		default:
			return false;
	}
	
	return true;
}

/*************************************************************************
 * Function Name: RTC_Enable
 * Parameters: void
 * Return: void 
 *
 * Description: Enable RTC, let RTC run.
 *  
 *************************************************************************/
void RTC_Enable(void)
{
	RTC_CCR = 1;
}

/*************************************************************************
 * Function Name: RTC_Disable
 * Parameters: void
 * Return: void 
 *
 * Description: Disable RTC, let RTC stop.
 *  
 *************************************************************************/
void RTC_Disable(void)
{
	RTC_CCR = 0;
}

/*************************************************************************
 * Function Name: RTC_Init
 * Parameters: void
 *
 * Return: int 
 *             	0: sucess
 *			1: fail
 *
 * Description: Initialize RTC, configure prescaler, CIIR and AMR register
 *  
 *************************************************************************/
int RTC_Init(void)
{
	long PreInt;

	RTC_Disable();
	
	/* initialize prescaler of RTC */
	PreInt = (int)( SYS_GetFpclk() / RTC_CountPerSec ) - 1;
	if (PreInt <= 0)
		return 1;
	
	RTC_PREINT = PreInt;
	RTC_PREFRAC = SYS_GetFpclk() - (PreInt + 1) * RTC_CountPerSec;
	
	/* initialize interrupt mask register of RTC */
	RTC_AMR=0xFF;		
	RTC_CIIR=0x00;
	
	/* clear all interrupt of RTC */
	RTC_ILR=0x3;
	
	/* initialize system default date and time */
	if (RTC_SetDateTime(&RTC_InitDateTime))
		return 1;

//	RTC_Enable();
	return 0;
}

/*************************************************************************
 * Function Name: RTC_SetDate
 * Parameters: LPC_Rtc_Date_t *pDate
 * Return: int 
 *             	0: sucess
 *			1: fail
 * Description: Set your specifying date
 *  
 *************************************************************************/
int RTC_SetDate (LPC_Rtc_Date_t *pDate)
{
	/* Valid Judge */
	if (!IsValidDay(pDate->year, pDate->month, pDate->day))
		return 1;

	/* Calulate DOW, DOY */
	pDate->DOY = GetDOY(pDate->year, pDate->month, pDate->day);
	pDate->DOW = GetDOW(pDate->year, pDate->month, pDate->day);

	RTC_DOM=pDate->day;
	RTC_MONTH=pDate->month;
	RTC_YEAR=pDate->year;
	RTC_DOW=pDate->DOW;
	RTC_DOY=pDate->DOY;

	return 0;
}


/*************************************************************************
 * Function Name: RTC_SetTime
 * Parameters: LPC_Rtc_Time_t *pTime
 * Return: int 
 *             	0: sucess
 *			1: fail
 * Description: Set your specifying time
 *  
 *************************************************************************/
int RTC_SetTime (LPC_Rtc_Time_t *pTime)
{
	/* Valid Judge */
	if ( pTime->hour > 23 || pTime->minute > 59 || pTime->second > 59)
		return 1;

	RTC_HOUR = pTime->hour;
	RTC_MIN = pTime->minute;
	RTC_SEC = pTime->second;

	return 0;
}


/*************************************************************************
 * Function Name: RTC_SetDateTime
 * Parameters: LPC_Rtc_DateTime_t *pDateTime
 * Return: int 
 *             	0: sucess
 *			1: fail
 * Description: Set your specifying date and time
 *  
 *************************************************************************/
int RTC_SetDateTime (LPC_Rtc_DateTime_t *pDateTime)
{
	/* Valid Judge */
	if (!IsValidDay(pDateTime->year, pDateTime->month, pDateTime->day))
		return 1;

	if ( pDateTime->hour > 23 || pDateTime->minute > 59 ||pDateTime->second > 59)
		return 1;

	/* Calulate DOW, DOY */
	pDateTime->DOY = GetDOY(pDateTime->year, pDateTime->month, pDateTime->day);
	pDateTime->DOW = GetDOW(pDateTime->year, pDateTime->month, pDateTime->day);
	
	RTC_DOM=pDateTime->day;
	RTC_MONTH=pDateTime->month;
	RTC_YEAR=pDateTime->year;
	RTC_DOW=pDateTime->DOW;
	RTC_DOY=pDateTime->DOY;

	RTC_HOUR = pDateTime->hour;
	RTC_MIN = pDateTime->minute;
	RTC_SEC = pDateTime->second;

	return 0;
}


/*************************************************************************
 * Function Name: RTC_GetDate
 * Parameters: LPC_Rtc_Date_t *pDate
 * Return: void 
 *
 * Description: Get the current date
 *  
 *************************************************************************/
void RTC_GetDate (LPC_Rtc_Date_t *pDate)
{
	pDate->day = RTC_DOM;
	pDate->month = RTC_MONTH;
	pDate->year = RTC_YEAR;

	pDate->DOW = RTC_DOW;
	pDate->DOY = RTC_DOY;
}


/*************************************************************************

⌨️ 快捷键说明

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