lpc_rtc.c

来自「深圳优龙公司LPC2148开发板(与iar公司开发板基本相同)的原理图和配套样例」· C语言 代码 · 共 827 行 · 第 1/2 页

C
827
字号
	CIIR = 0;
	return ;
}

/*************************************************************************
 * Function Name: RTC_SetAlarmtInt
 * Parameters: unsigned char AlarmIntType
 * Return: void
 *
 * Description: Set alarm interrupt type
 *
 *************************************************************************/
void RTC_SetAlarmInt( unsigned char AlarmIntType )
{
	AMR = ~AlarmIntType & 0xFF;
	return;
}

/*************************************************************************
 * Function Name: RTC_DisableAlarmInt
 * Parameters: void
 * Return: void
 *
 * Description: Disable RTC alarm interrupt.
 *
 *************************************************************************/
void RTC_DisableAlarmInt( void )
{
	AMR = 0xFF;
}

/*************************************************************************
 * Function Name: RTC_SetAlarmDateTime
 * Parameters: LPC_Rtc_DateTime_t *pDateTime
 * Return: int
 *  		   	0: sucess
 *		1: fail
 * Description: Set your specifying alarm date and time
 *
 *************************************************************************/
int RTC_SetAlarmDateTime( 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 );

	ALDOM = pDateTime->day;
	ALMON = pDateTime->month;
	ALYEAR = pDateTime->year;
	ALDOW = pDateTime->DOW;
	ALDOY = pDateTime->DOY;

	ALHOUR = pDateTime->hour;
	ALMIN = pDateTime->minute;
	ALSEC = pDateTime->second;

	return 0;
}

/*************************************************************************
 * Function Name: RTC_ClearInt
 * Parameters: unsigned long IntType
 * Return: int
 *  		   	0: sucess
 *		1: fail
 *
 * Description: Clear RTC interrupt.
 *
 *************************************************************************/
int RTC_ClearInt( unsigned long IntType )
{
	if ( IntType<1 || IntType>3 )
		return 1;

	ILR = ( IntType & 0x3 );
	return 0;
}

/*************************************************************************
 * Function Name: RTC_CheckIntType
 * Parameters: void
 * Return:  unsigned long
 *		RTCIncrementInt(1) | RTCAlarmInt(2)
 *
 * Description: Get RTC interrupt Type.
 *
 *************************************************************************/
unsigned long RTC_CheckIntType( void )
{
	return ( ILR & 0x3 );
}

/*************************************************************************
 * Function Name: RTC_ISR
 * Parameters:  void
 * Return: void
 *
 * Description: Rtc interrupt subroutine
 *
 *************************************************************************/
void RTC_ISR( void )
{
	int IntStatus;

	IntStatus = RTC_CheckIntType() & 0x3;
	RTC_ClearInt( IntStatus );

	if ( IntStatus & RTCIncrementInt )	// Increment Interrupt
	{
		SysTimeUpdate();
	}

	if ( IntStatus & RTCAlarmInt )			// Alarm Interrupt
	{
		Alarm();
	}
	VICVectAddr = 0;
}


/*************************************************************************
 * Function Name: FormatDate
 * Parameters: int Type
 *			LPC_Rtc_Date_t *pDate
 *			char *s
 *
 * Return: void
 *
 * Description: Format the current date into an ASCII string according to the Type.
 *		Type = 1, "YYYY-MM-DD" (11 chars)
 *		Type = 2, "DOW Month DD, YYYY" (30 chars)
 *
 *************************************************************************/
void FormatDate( int Type , LPC_Rtc_Date_t* pDate , char* s )
{
	unsigned short year;
	unsigned char month, day, DOW;
	char str[5];

	year = pDate->year;
	month = pDate->month;
	day = pDate->day;
	DOW = pDate->DOW;
	//  DOY = pDate->DOY;

	switch ( Type )
	{
		case 1:
			strcpy( s , "YYYY-MM-DD" );
			s[0] = year / 1000 + '0';
			year = year % 1000;
			s[1] = year / 100 + '0';
			year = year % 100;
			s[2] = year / 10 + '0';
			s[3] = year % 10 + '0';
			s[5] = month / 10 + '0';
			s[6] = month % 10 + '0';
			s[8] = day / 10 + '0';
			s[9] = day % 10 + '0';
			break;
		case 2:
			strcpy( s , RTC_DOWTbl[DOW] );
			strcat( s , RTC_MonthTbl[month] );
			if ( day < 10 )
			{
				str[0] = day + '0';
				str[1] = 0;
			}
			else
			{
				str[0] = day / 10 + '0';
				str[1] = day % 10 + '0';
				str[2] = 0;
			}
			strcat( s , str );
			strcat( s , ", " );
			sprintf( str , "%d" , year );
			strcat( s , str );
			break;		
		default:
			strcpy( s , "?" );
			break;
	}
}

/*************************************************************************
 * Function Name: FormatTime
 * Parameters: int Type
 *			LPC_Rtc_Time_t *pTime
 *			char *s
 *
 * Return: void
 *
 * Description: Format the current time into an ASCII string according to the Type.
 *		Type = 1, "HH:MM:SS" (9 chars)
 *		Type = 2, "HH:MM:SS AM" (13 chars)
 *
 *************************************************************************/
void FormatTime( int Type , LPC_Rtc_Time_t* pTime , char* s )
{
	unsigned char hour, minute, second;

	hour = pTime->hour;
	minute = pTime->minute;
	second = pTime->second;

	switch ( Type )
	{
		case 1:
			strcpy( s , "HH:MM:SS" );
			s[0] = hour / 10 + '0';
			s[1] = hour % 10 + '0';
			s[3] = minute / 10 + '0';
			s[4] = minute % 10 + '0';
			s[6] = second / 10 + '0';
			s[7] = second % 10 + '0';
			break;
		case 2:
			strcpy( s , "HH:MM:SS AM" );
			s[9] = ( hour >= 12 ) ? 'P' : 'A';
			if ( hour > 12 )
				hour = hour - 12;	

			s[0] = hour / 10 + '0';
			s[1] = hour % 10 + '0';
			s[3] = minute / 10 + '0';
			s[4] = minute % 10 + '0';
			s[6] = second / 10 + '0';
			s[7] = second % 10 + '0';
			break;
		default:
			strcpy( s , "?" );
			break;
	}
}

/*************************************************************************
 * Function Name: FormatDateTime
 * Parameters:  int Type
 *			LPC_Rtc_DateTime_t *pDateTime
 *		char *s
 *
 * Return: void
 *
 * Description: Format the current date and time into an ASCII string according to the Type.
 *		Type = 1, "YYYY-MM-DD HH:MM:SS" (18 chars)
 *
 *************************************************************************/
void FormatDateTime( int Type , LPC_Rtc_DateTime_t* pDateTime , char* s )
{
	unsigned short year;
	unsigned char month, day;
	unsigned char hour, minute, second;

	year = pDateTime->year;
	month = pDateTime->month;
	day = pDateTime->day;
	//  DOW = pDateTime->DOW;
	//  DOY = pDateTime->DOY;

	hour = pDateTime->hour;
	minute = pDateTime->minute;
	second = pDateTime->second;

	switch ( Type )
	{
		case 1:
			strcpy( s , "YYYY-MM-DD HH:MM:SS" );
			s[0] = year / 1000 + '0';
			year = year % 1000;
			s[1] = year / 100 + '0';
			year = year % 100;
			s[2] = year / 10 + '0';
			s[3] = year % 10 + '0';
			s[5] = month / 10 + '0';
			s[6] = month % 10 + '0';
			s[8] = day / 10 + '0';
			s[9] = day % 10 + '0';

			s[11] = hour / 10 + '0';
			s[12] = hour % 10 + '0';
			s[14] = minute / 10 + '0';
			s[15] = minute % 10 + '0';
			s[17] = second / 10 + '0';
			s[18] = second % 10 + '0';
			break;
		default:
			strcpy( s , "?" );
			break;
	}
}

const char *WeekDay[7] = { "MON", "TUES", "WED", "THURS","FRI", "SAT", "SUN" };

/********************************************************************************************************************
【函数名称】void RTC_Date_Time_Dispaly( void )
【功能描述】RTC时间显示测试
【参数输入】无
【参数返回】无
********************************************************************************************************************/
void RTC_Date_Time_Dispaly( void )
{
	LPC_Rtc_DateTime_t DateTimE ;

	RTC_GetDateTime( &DateTimE ) ;

	Uart_Printf( UART0, "\nCurrent date is %d-%d-%d [%s]\n", DateTimE.year, DateTimE.month, DateTimE.day, WeekDay[(DateTimE.DOW-1)%7]);
	Uart_Printf( UART0, "\nCurrent time is %2d:%2d:%2d\n", DateTimE.hour, DateTimE.minute, DateTimE.second);
}


/****************************************************************************
【功能说明】十进制转换成BCD码
****************************************************************************/
/*int Algorism_BCD(int a)
{
	int x,y,z;
	x = a / 1000;
	a = a % 1000;
	y = a / 100;
	a = a % 100;
	z = a / 10;
	a = a % 10;
	
	a = a | (z << 4) | (y << 8) | (x << 12);
	return a;
}*/
//***************************************************************************

/****************************************************************************
【功能说明】实时时钟时间设定
****************************************************************************/
void Rtc_Time_Set(void)
{
	int m ;
	LPC_Rtc_DateTime_t Rtc_DateTime_SET ;
	
	RTC_Date_Time_Dispaly() ;

    Uart_Printf( UART0, "\nPlease input new year[2000-2099],ENTER' for no modufy:");	
    m = UART_Get_Number( UART0 );		//
    if( m != 0 )
    {
    	//m =Algorism_BCD(m);		//
    	Rtc_DateTime_SET.year = m;		//年
    }

    Uart_Printf( UART0, "\n\nPlease input new month[1-12],ENTER' for no modufy:");	
    m = UART_Get_Number( UART0 );		//
    if(m != 0)
    {
    	//m =Algorism_BCD(m);		//
	    Rtc_DateTime_SET.month = m;		//月
    }

    Uart_Printf( UART0, "\n\nPlease input new day[1-31],ENTER' for no modufy:");	
    m = UART_Get_Number( UART0 );		//
    if(m != 0)
    {
    	//m =Algorism_BCD(m);		//
    	Rtc_DateTime_SET.day = m;		//日
    }

   	Uart_Printf( UART0, "\n\n  ");	
	for( m = 0; m < 7; m++ )
    	Uart_Printf( UART0, "%d=%s  ", (m+1), WeekDay[m]);	
	
    Uart_Printf( UART0, "\nPlease input new week[1-7],ENTER' for no modufy:");	
    m = UART_Get_Number( UART0 );		//
    if(m != 0)
    {
    	//m =Algorism_BCD(m);		//
	    Rtc_DateTime_SET.DOW = (m-1);		//星期
    }

    Uart_Printf( UART0, "\n\nPlease input new hour[0-23],ENTER' for no modufy:");	
    m = UART_Get_Number( UART0 );		//
    if(m != 0)
    {
    	//m =Algorism_BCD(m);		//
	    Rtc_DateTime_SET.hour = m;		//小时
    }

    Uart_Printf( UART0, "\n\nPlease input new minute[0-59],ENTER' for no modufy:");	
    m = UART_Get_Number( UART0 );		//
    if(m != 0)
    {
    	//m =Algorism_BCD(m);		//
	    Rtc_DateTime_SET.minute = m;		//分
    }

    Uart_Printf( UART0, "\n\nPlease input new second[0-59],ENTER' for no modufy:");	
    m = UART_Get_Number( UART0 );		//
    if(m != 0)
    {
    	//m =Algorism_BCD(m);		//
	    Rtc_DateTime_SET.second = m;		//秒
    }

	// initialize Date and Time
	RTC_SetDateTime( &Rtc_DateTime_SET ) ;
	
	RTC_Date_Time_Dispaly() ;
}
//***************************************************************************

⌨️ 快捷键说明

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