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

📄 rtc.c

📁 嵌入式系统
💻 C
📖 第 1 页 / 共 2 页
字号:
/*-----------------------------------------------------------------------------
@@
@@ Copyright (c) 2000 Sharp Corporation All rights reserved.
@@
@@ (Summary)	: LH79531 Real Time Clock Device Driver Source File
@@
@@ (Comment)	: Source codes of routines available for Real Time Clock
@@
@@ (Author)	: Teo Li Li
@@
@@ (History)	: 21/11/2000	Teo Li Li	Version 1.0
@@		  28/03/2001	Tan Wee Kah	Version 1.3 Modified the functions
@@						apd_RTCSet(Get)Timer
@@
@@ (RCS ID)	:
@@
-----------------------------------------------------------------------------*/

#define APD_RTC_C

/*+include files*************************************************************/
/*                                                                          */
/*                                                                          */
/****************************************************************************/
#include "rtc.h"

/******************************************************************************
@@
@@ [Name]	: apd_RTCSetCalendar
@@
@@ [Summary]	: This function is used to set the calendar
@@
@@ [Argument]	: cal: specify the information to set the calendar
@@		  APD_RTC_CENTURY for century;
@@		  APD_RTC_YEAR for year;
@@		  APD_RTC_MONTH for month;
@@		  APD_RTC_DAY for day;
@@		  APD_RTC_WEEK for week;
@@
@@ [Return]	: None
@@
@@ [Desc]	: Set the century, year, month, day and day of the week according
@@		  to the argument.
@@
@@ [Note]	:
@@
@@ [History]	: 21/11/2000	Teo Li Li		Version 1.0
@@		  1/2/2001	Teo Li Li 	Version 1.1
@@						No Checking, assume the user input the correct data
@@		  16/02/2001	Leow Yee Ling	Version 1.2
@@						Storage is in BCD format
@@ [END]
******************************************************************************/

APD_VOID apd_RTCSetCalendar(APD_RTC_CALENDAR_TYPE cal)
{
	apd_RTCEnterModifyMode ( );

	*APD_RTCYEAR = ((cal.year  / 10)<<4) + (cal.year  % 10);
	*APD_RTCMONTH = ((cal.month / 10)<<4) + (cal.month % 10);
	*APD_RTCDAY_M = ((cal.day   / 10)<<4) + (cal.day   % 10);
	*APD_RTCDAY_W = cal.week;
	*APD_RTCCENTURY = cal.century;

	apd_RTCExitModifyMode ( );
}

/******************************************************************************
@@
@@ [Name]	: apd_RTCGetCalendar
@@
@@ [Summary]	: This function is used to return the readings of calendar.
@@
@@ [Argument]	: None
@@
@@ [Return]	: Structure of RTC_CALENDAR_TYPE
@@
@@ [Desc]	: Return Century, Year, Month, Day and Day of the Week in
@@		  binary format.
@@
@@ [Note]	:
@@
@@ [History]	: 21/11/2000	Teo Li Li		Version 1.0
@@		  16/02/2001	Leow Yee Ling	Version 1.2
@@
@@ [END]
******************************************************************************/

APD_RTC_CALENDAR_TYPE apd_RTCGetCalendar(APD_VOID)
{
	APD_RTC_CALENDAR_TYPE cal;

	cal.century = *APD_RTCCENTURY;
	cal.year = ((*APD_RTCYEAR >>4)*10) + (*APD_RTCYEAR  & 0x0F);
	cal.month = ((*APD_RTCMONTH>>4)*10) + (*APD_RTCMONTH & 0x0F);
	cal.day = ((*APD_RTCDAY_M>>4)*10) + (*APD_RTCDAY_M & 0x0F);
	cal.week = *APD_RTCDAY_W;

	return cal;
}

/******************************************************************************
@@
@@ [Name]	: apd_RTCSetTime
@@
@@ [Summary]	: This function is used to set the RTC time
@@
@@ [Argument]   : settime:	specify the clock setting to the RTC (in BCD format)
@@                APD_RTC_HOUR for hour;
@@                APD_RTC_MINUTE for minute;
@@                APD_RTC_SECOND for second;
@@                APD_RTC_CLK_FORMAT for format;
@@
@@ [Return]	: None
@@
@@ [Desc]	: Use to set Hour, Minute and Second for Timer Register according
@@		  to the above argument.
@@
@@ [Note]	:  Information of APD_RTC_CLK_FORMAT inside the RTC_TIME_TYPE
@@		  structure will indicate the type. APD_RTC_NULL indicates that it is in 24hr
@@		  format whereas APD_RTC_AM or APD_RTC_PM indicates that it is in
@@		  12hr format.
@@
@@ [Note]       :
@@
@@ [History]	: 21/11/2000	Teo Li Li		Version 1.0
@@		  1/02/2001	Teo Li Li		Version 1.1
@@						No Checking, assume the user input the correct data
@@		  16/02/2001	Leow Yee Ling	Version 1.2
@@						Storage is in BCD format
@@
@@ [END]
******************************************************************************/

APD_VOID apd_RTCSetTime(APD_RTC_TIME_TYPE settime)
{
	apd_RTCEnterModifyMode ( );

	#if 0
	settime.hour = ((settime.hour / 10)<<4) + (settime.hour % 10);
	#endif
	if (settime.format != APD_RTC_NULL)
	{
		// 12 hr format
		if (settime.format == APD_RTC_PM)
		{
			settime.hour = settime.hour | APD_RTCCLK_FORMAT_SET;
		}
	}
	else
	{
		// 24 hr format
		settime.hour = settime.hour | APD_RTCTIME_FORMAT_SEL;
	}

	*APD_RTCHOUR   = settime.hour;
	#if 0
	*APD_RTCMINUTE = ((settime.minute / 10)<<4) + (settime.minute % 10);
	*APD_RTCSECOND = ((settime.second / 10)<<4) + (settime.second % 10);
	#else
	*APD_RTCMINUTE = settime.minute;
	*APD_RTCSECOND = settime.second;
	#endif

	apd_RTCExitModifyMode ( );
}

/******************************************************************************
@@
@@ [Name]	: apd_RTCGetTime
@@
@@ [Summary]: This function is used to return readings of the RTC time.
@@
@@ [Argument]: format: APD_RTC_NULL if to return time in 24hr format
@@                     APD_RTC_AM or APD_RTC_PM if to return time in 12 hr format
@@
@@ [Return]	: Structure of RTC_TIME_TYPE
@@
@@ [Desc]	: Get the RTC time (BCD format) in 12 hour format or 24 hour format.
@@
@@ [Note]	:  Information of APD_RTC_CLK_FORMAT inside the RTC_TIME_TYPE
@@		  structure will indicate the type. APD_RTC_NULL indicates that it is in 24hr
@@		  format whereas APD_RTC_AM or APD_RTC_PM indicates that it is in
@@		  12hr format.
@@
@@ [Note]	:
@@
@@ [History]	: 21/11/2000	Teo Li Li		Version 1.0
@@		  16/02/2001	Leow Yee Ling	Version 1.2
@@
@@ [END]
******************************************************************************/

APD_RTC_TIME_TYPE apd_RTCGetTime(APD_RTC_CLK_FORMAT format)
{
	APD_RTC_TIME_TYPE gettime;

	/* Check if current storage is in 12 or 24 hr format */
	if ((*APD_RTCHOUR & APD_RTCTIME_FORMAT_SEL) != 0)
	{
		/* currently in 24 hour format */
		gettime.hour = *APD_RTCHOUR & APD_RTC24HOUR_MASK;
		gettime.format = APD_RTC_NULL;
		if (format != APD_RTC_NULL)
		{
			/* return in 12 hr format */
			gettime.format = APD_RTC_AM;
			if (gettime.hour > 0x11)
			{
				gettime.hour = gettime.hour - 0x12;
				gettime.format = APD_RTC_PM;
			}
			if (gettime.hour == 0)
			{
				gettime.hour = 0x12;
			}
		}
	}
	else
	{
		/* currently in 12 hour format */
		gettime.hour = *APD_RTCHOUR & APD_RTC12HOUR_MASK;
		gettime.format = ((*APD_RTCHOUR & APD_RTCCLK_FORMAT_SET) != 0) ? APD_RTC_PM : APD_RTC_AM;
		if (format == APD_RTC_NULL)
		{
			if (gettime.format == APD_RTC_PM)
			{
				gettime.hour = gettime.hour + 0x12;
			}
			if (gettime.hour > 0x23)
			{
				gettime.hour = gettime.hour - 0x24;
			}
			gettime.format = APD_RTC_NULL;
		}
	}

	gettime.second = *APD_RTCSECOND;
	gettime.minute = *APD_RTCMINUTE;

	/* return in BCD format, no conversion needed */
	#if 0
	gettime.second = ((*APD_RTCSECOND>>4)*10) + (*APD_RTCSECOND & 0x0F);
	gettime.minute = ((*APD_RTCMINUTE>>4)*10) + (*APD_RTCMINUTE & 0x0F);
	gettime.hour = ((gettime.hour>>4)*10) + (gettime.hour & 0x0F);
	#endif

	return gettime;
}


/******************************************************************************
@@
@@ [Name]	: apd_RTCSetAlarm
@@
@@ [Summary]	: This function is used to set the alarm time.
@@
@@ [Argument]	: settime:	specify the clock setting to alarm time (BCD format)
@@		  APD_RTC_HOUR for hour;
@@		  APD_RTC_MINUTE for minute;
@@		  APD_RTC_SECOND for second;
@@		  APD_RTC_CLK_FORMAT for format;
@@
@@ [Return]	: None
@@
@@ [Desc]	: Sets Hour and Minute for the alarm registers. All data is store as 24HR format.
@@
@@ [Note]	:
@@
@@ [History]	: 21/11/2000	Teo Li Li		Version 1.0
@@		  1/2/2001	Teo Li Li		Version 1.1
@@						No Checking, assume the user input the correct data
@@		  16/02/2001	Leow Yee Ling	Version 1.2
@@						Storage is in BCD format
@@		  28/03/2001	Tan Wee Kah	Version 1.3
@@						Alarm is store as 24HR format only
@@
@@ [END]
******************************************************************************/

APD_VOID apd_RTCSetAlarm(APD_RTC_TIME_TYPE setalm)
{
	apd_RTCEnterModifyMode ( );

	// 12 hr format, convert to 24 hr format

	#if 0

	if (setalm.hour == 12)
	{
		setalm.hour = 0;
	}
	if (setalm.format == APD_RTC_PM)
	{
		setalm.hour = setalm.hour + 12;
	}

	*APD_RTCHOURD = ((setalm.hour / 10)<<4) + (setalm.hour % 10);
	*APD_RTCMIND  = ((setalm.minute / 10)<<4) + (setalm.minute % 10);

	#else

	if (setalm.format != APD_RTC_NULL)
	{
		if (setalm.hour == 0x12)
		{
			setalm.hour = 0;
		}
		if (setalm.format == APD_RTC_PM)
		{
			if (setalm.hour < 8)
				setalm.hour = setalm.hour + 0x12;
			else
				setalm.hour = setalm.hour + 0x18;
		}
	}
	*APD_RTCHOURD = setalm.hour;
	*APD_RTCMIND  = setalm.minute;

	#endif

	apd_RTCExitModifyMode ( );
}

/******************************************************************************
@@
@@ [Name]	: apd_RTCGetAlarm
@@
@@ [Summary]	: This function is used to return readings of the alarm time.
@@
@@ [Argument]: format: APD_RTC_NULL if to return time in 24hr format
@@                     APD_RTC_AM or APD_RTC_PM if to return time in 12 hr format
@@
@@ [Return]	: Structure of RTC_TIME_TYPE
@@
@@ [Desc]	: Get the alarm time.
@@
@@ [Note]	:
@@
@@ [History]	: 21/11/2000	Teo Li Li		Version 1.0
@@		  16/02/2001	Leow Yee Ling	Version 1.2
@@		  28/03/2001	Tan Wee Kah	Version 1.3 Modify the clock
@@						setting checking
@@ [END]
******************************************************************************/

APD_RTC_TIME_TYPE apd_RTCGetAlarm(APD_RTC_CLK_FORMAT format)
{
	APD_RTC_TIME_TYPE getalarm;

	getalarm.second = 0;		/* not in use, set to 0 */
	getalarm.format = APD_RTC_NULL; /* set to 24 hr format  */

	/* return in bcd format, no conversion */
	#if 0
	// convert from bcd to binary format
	getalarm.minute = ((*APD_RTCMIND>>4)*10)+ (*APD_RTCMIND & 0x0F);
	getalarm.hour   = ((*APD_RTCHOURD>>4)*10) + (*APD_RTCHOURD & 0x0F);
	#endif

	getalarm.minute = *APD_RTCMIND;
	getalarm.hour   = *APD_RTCHOURD;
	if (format != APD_RTC_NULL)
	{
		/* return in 12 hr format */
		getalarm.format = APD_RTC_AM;
		if (getalarm.hour > 0x11)
		{
			getalarm.hour = getalarm.hour - 0x12;
			getalarm.format = APD_RTC_PM;
		}
		if (getalarm.hour == 0)
		{
			getalarm.hour = 0x12;
		}
	}

	return getalarm;
}

/******************************************************************************
@@
@@ [Name]	: apd_RTCEnableClkInput
@@
@@ [Summary]	: This function enables the clock input

⌨️ 快捷键说明

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