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

📄 lpc_rtc.c

📁 深圳优龙公司LPC2148开发板(与iar公司开发板基本相同)的原理图和配套样例程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*************************************************************************
 *
 *    Used with ICCARM and AARM.
 *
 *    (c) Copyright IAR Systems 2003
 *
 *    File name   : LPC_Rtc.c
 *    Description : Define API for RTI
 *
 *    History :
 *    1. Data   	 : August 10, 2004
 *  	 Author 	 : Shawn Zhang
 *  	 Description : Create
 *
 *    2. Data   	 : Oct 12, 2004
 *  	 Author 	 : Stanimir Bonev
 *  	 Description: Modify the interface of some functions
 *
 *    $Revision: 1.1 $
 **************************************************************************/

#include "LPC_Rtc.h"

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

/*************************************************************************
 * Function Name: GetDOY
 * Parameters:  unsigned short Year
 *	  	unsigned char month
 *		unsigned char day
 *
 * Return: int
 *
 * Description: Get the day of year according to the date
 *
 *************************************************************************/
static int GetDOY( unsigned short year , unsigned char month , unsigned char 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:  unsigned short Year
 *		unsigned char month
 *		unsigned char 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( unsigned short year , unsigned char month , char day )
{
	int i, 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:  unsigned short Year
 *		unsigned char month
 *			unsigned char 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).
 *
 *************************************************************************/
static bool IsValidDay( unsigned short year , unsigned char month , unsigned char 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 )
{
	CCR_bit.CLKEN = true;
	return ;
}

/*************************************************************************
 * Function Name: RTC_Disable
 * Parameters: void
 * Return: void
 *
 * Description: Disable RTC, let RTC stop.
 *
 *************************************************************************/
void RTC_Disable( void )
{
	CCR_bit.CLKEN = false;
	return ;
}

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

	RTC_Disable();
	if ( BackComp )
	{
		// initialize prescaler of RTC
		PreInt = ( int ) ( SYS_GetFpclk() / RTC_CountPerSec ) - 1;
		if ( PreInt <= 0 )
			return 1;

		PREINT = PreInt;
		PREFRAC = SYS_GetFpclk() - ( PreInt + 1 ) * RTC_CountPerSec;
	}
	else
	{
		/* 32kHz watch quarc */
		CCR_bit.CLKSRC = 1;
	}
	/* test disable and reset*/
	CCR_bit.CTCRST = CCR_bit.CTTEST = 0;
	// initialize interrupt mask register of RTC
	AMR = 0;
	CIIR_bit.IMSEC = 1;

	// clear all interrupt of RTC
	ILR = 0x3;

#if( 1 )
	RTC_GetDate( &Date_Read ) ;
	if ( IsValidDay( Date_Read.year , Date_Read.month , Date_Read.day ) == false )
	{
		// initialize Date and Time
		if ( RTC_SetDateTime( &RTC_InitDateTime ) )
			return 1;
	}
#else
	// initialize Date and Time
	if ( RTC_SetDateTime( &RTC_InitDateTime ) )
		return 1;
#endif


	//  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 ) == false )
		return 1;

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

	DOM = pDate->day;
	MONTH = pDate->month;
	YEAR = pDate->year;
	DOW = pDate->DOW;
	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;

	HOUR = pTime->hour;
	MIN = pTime->minute;
	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 ) == false )
		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 );

	DOM = pDateTime->day;
	MONTH = pDateTime->month;
	YEAR = pDateTime->year;
	DOW = pDateTime->DOW;
	DOY = pDateTime->DOY;

	HOUR = pDateTime->hour;
	MIN = pDateTime->minute;
	SEC = pDateTime->second;

	return 0;
}

/*************************************************************************
 * Function Name: RTC_GetDate
 * Parameters: LPC_Rtc_Date_t *pDate
 * Return: int
 *  		   	0: sucess
 *		1: fail
 * Description: Get the current date
 *
 *************************************************************************/
int RTC_GetDate( LPC_Rtc_Date_t* pDate )
{
	pDate->day = DOM;
	pDate->month = MONTH;
	pDate->year = YEAR;

	pDate->DOW = DOW;
	pDate->DOY = DOY;

	return 0;
}

/*************************************************************************
 * Function Name: RTC_GetTime
 * Parameters: LPC_Rtc_Time_t *pTime
 * Return: int
 *  		   	0: sucess
 *		1: fail
 * Description: Set the current time
 *
 *************************************************************************/
int RTC_GetTime( LPC_Rtc_Time_t* pTime )
{
	pTime->hour = HOUR;
	pTime->minute = MIN;
	pTime->second = SEC;

	return 0;
}

/*************************************************************************
 * Function Name: RTC_GetDateTime
 * Parameters: LPC_Rtc_DateTime_t *pDateTime
 * Return: int
 *  		   	0: sucess
 *		1: fail
 * Description: Get the current date and time
 *
 *************************************************************************/
int RTC_GetDateTime( LPC_Rtc_DateTime_t* pDateTime )
{
	pDateTime->day = DOM;
	pDateTime->month = MONTH;
	pDateTime->year = YEAR;

	pDateTime->hour = HOUR;
	pDateTime->minute = MIN;
	pDateTime->second = SEC;

	pDateTime->DOW = DOW;
	pDateTime->DOY = DOY;

	return 0;
}

/*************************************************************************
 * Function Name: RTC_SetInctInt
 * Parameters: unsigned char IncIntType
 * Return: void
 *
 * Description: Set increment interrupt type
 *
 *************************************************************************/
void RTC_SetIncInt( unsigned char IncIntType )
{
	CIIR = IncIntType & 0xFF;
	return;
}

/*************************************************************************
 * Function Name: RTC_DisableIncInt
 * Parameters: void
 * Return: void
 *
 * Description: Disable RTC increment interrupt.
 *
 *************************************************************************/
void RTC_DisableIncInt( void )
{

⌨️ 快捷键说明

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