سلام بر اساتید گرامی
من میخواستم ساعت و تاریخ رو از واحد RTC بگیرم اما نشد لطفا راهنمایی بفرمایید.
ممنون
من میخواستم ساعت و تاریخ رو از واحد RTC بگیرم اما نشد لطفا راهنمایی بفرمایید.
ممنون

#include "RTC_Time.h"
/* Private define ------------------------------------------------------------*/
//#define RTCClockOutput_Enable /* RTC Clock/64 is output on tamper pin(PC.13) */
/* Private function prototypes -----------------------------------------------*/
void Time_Set(u32 t);
/*******************************************************************************
* Function Name : Time_ConvUnixToCalendar
* Description : ת»»UNIXʱ¼ä´ÁΪÈÕÀúʱ¼ä
* Input : - t: µ±Ç°Ê±¼äµÄUNIXʱ¼ä´Á
* Output : None
* Return : struct tm
* Attention : None
*******************************************************************************/
struct tm Time_ConvUnixToCalendar(time_t t)
{
struct tm *t_tm;
t_tm = localtime(&t);
t_tm->tm_year += 1900; /* localtimeת»»½á¹ûµÄtm_yearÊÇÏà¶ÔÖµ£¬ÐèҪת³É¾ø¶ÔÖµ */
return *t_tm;
}
/*******************************************************************************
* Function Name : Time_ConvCalendarToUnix
* Description : дÈëRTCʱÖÓµ±Ç°Ê±¼ä
* Input : - t: struct tm
* Output : None
* Return : time_t
* Attention : None
*******************************************************************************/
time_t Time_ConvCalendarToUnix(struct tm t)
{
t.tm_year -= 1900; /* Íⲿtm½á¹¹Ìå´æ´¢µÄÄê·ÝΪ2008¸ñʽ */
/* ¶øtime.hÖж¨ÒåµÄÄê·Ý¸ñʽΪ1900Ä꿪ʼµÄÄê·Ý */
/* ËùÒÔ£¬ÔÚÈÕÆÚת»»Ê±Òª¿¼Âǵ½Õâ¸öÒòËØ¡£*/
return mktime(&t);
}
/*******************************************************************************
* Function Name : Time_GetUnixTime
* Description : ´ÓRTCÈ¡µ±Ç°Ê±¼äµÄUnixʱ¼ä´ÁÖµ
* Input : None
* Output : None
* Return : time_t
* Attention : None
*******************************************************************************/
time_t Time_GetUnixTime(void)
{
return (time_t)RTC_GetCounter();
}
/*******************************************************************************
* Function Name : Time_GetCalendarTime
* Description : ´ÓRTCÈ¡µ±Ç°Ê±¼äµÄÈÕÀúʱ¼ä£¨struct tm£©
* Input : None
* Output : None
* Return : struct tm
* Attention : None
*******************************************************************************/
struct tm Time_GetCalendarTime(void)
{
time_t t_t;
struct tm t_tm;
t_t = (time_t)RTC_GetCounter();
t_tm = Time_ConvUnixToCalendar(t_t);
return t_tm;
}
/*******************************************************************************
* Function Name : Time_SetUnixTime
* Description : ½«¸ø¶¨µÄUnixʱ¼ä´ÁдÈëRTC
* Input : - t: time_t
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void Time_SetUnixTime(time_t t)
{
RTC_WaitForLastTask();
RTC_SetCounter((u32)t);
RTC_WaitForLastTask();
return;
}
/*******************************************************************************
* Function Name : Time_SetCalendarTime
* Description : ½«¸ø¶¨µÄCalendar¸ñʽʱ¼äת»»³ÉUNIXʱ¼ä´ÁдÈëRTC
* Input : - t: struct tm
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void Time_SetCalendarTime(struct tm t)
{
Time_SetUnixTime(Time_ConvCalendarToUnix(t));
return;
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures the nested vectored interrupt controller.
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the RTC Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures the RTC.
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void RTC_Configuration(void)
{
/* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Reset Backup Domain */
BKP_DeInit();
/* Enable LSE */
RCC_LSEConfig(RCC_LSE_ON);
/* Wait till LSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{}
/* Select LSE as RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
/* Enable RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Enable the RTC Second */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
/*******************************************************************************
* Function Name : USART_Scanf
* Description : USART Receive
* Input : - min_value:
* - max_value:
* - lenght:
* Output : None
* Return : uint8_t
* Attention : None
*******************************************************************************/
static uint16_t USART_Scanf(uint32_t min_value,uint32_t max_value,uint8_t lenght)
{
uint16_t index = 0;
uint32_t tmp[4] = {0, 0, 0, 0};
while (index < lenght)
{
/* Loop until RXNE = 1 */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{}
tmp[index++] = (USART_ReceiveData(USART1));
if( tmp[index - 1] == 0x0D ) { index--; continue; }
if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39))
{
printf("Please enter valid number between 0 and 9\r\n");
index--;
}
}
/* Calculate the Corresponding value */
if( lenght ==2 )
index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10 );
else /* lenght ==4 */
index = (tmp[3] - 0x30) + ((tmp[2] - 0x30) * 10 ) + ((tmp[1] - 0x30) * 100 ) + ((tmp[0] - 0x30) * 1000 );
/* Checks */
if (index > max_value || index < min_value)
{
printf("Please enter valid number between %d and %d\r\n", min_value, max_value);
return 0;
}
return index;
}
/*******************************************************************************
* Function Name : Time_Regulate
* Description : None
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void Time_Regulate(void)
{
struct tm time;
memset(&time, 0 , sizeof(time) ); /* Çå¿Õ½á¹¹Ìå */
printf("=======================Time Settings==========================\r\n");
printf("Please Set Years between 1970 to 2037\r\n");
while ( time.tm_year>2037 || time.tm_year<1970)
{
time.tm_year = USART_Scanf(1970,2037,4);
}
printf("Set Years: %d\r\n", time.tm_year);
printf("Please Set Months between 01 to 12\r\n");
while (time.tm_mon >12 || time.tm_mon < 1 )
{
time.tm_mon= USART_Scanf(1,12,2)-1;
}
printf("Set Months: %d\r\n", time.tm_mon);
printf("Please Set Days between 01 to 31\r\n");
while (time.tm_mday >31 ||time.tm_mday <1 )
{
time.tm_mday = USART_Scanf(1,31,2);
}
printf("Set Days: %d\r\n", time.tm_mday);
printf("Please Set Hours between 01 to 23\r\n");
while (time.tm_hour >23 ||time.tm_hour <1 )
{
time.tm_hour = USART_Scanf(1,23,2);
}
printf("Set Hours: %d\r\n", time.tm_hour);
printf("Please Set Minutes between 01 to 59\r\n");
while (time.tm_min >59 || time.tm_min <1 )
{
time.tm_min = USART_Scanf(1,59,2);
}
printf("Set Minutes: %d\r\n", time.tm_min);
printf("Please Set Seconds between 01 to 59\r\n");
while (time.tm_sec >59 || time.tm_sec <1 )
{
time.tm_sec = USART_Scanf(1,59,2);
}
printf("Set Seconds: %d\r\n", time.tm_sec);
/* Return the value to store in RTC counter register */
Time_SetCalendarTime(time);
}
/*******************************************************************************
* Function Name : RTC_Init
* Description : RTC Initialization
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void RTC_Init(void)
{
if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{
/* Backup data register value is not correct or not yet programmed (when
the first time the program is executed) */
printf("RTC not yet configured....\r\n");
/* RTC Configuration */
RTC_Configuration();
Time_Regulate();
/* Adjust time by values entred by the user on the hyperterminal */
printf("RTC configured....\r\n");
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
}
else
{
/* Check if the Power On Reset flag is set */
if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
printf("Power On Reset occurred....\r\n");
}
/* Check if the Pin Reset flag is set */
else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
printf("External Reset occurred....\r\n");
}
printf("No need to configure RTC....\r\n");
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Enable the RTC Second */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
/* NVIC configuration */
NVIC_Configuration();
#ifdef RTCClockOutput_Enable
/* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Disable the Tamper Pin */
BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper
functionality must be disabled */
/* Enable RTC Clock Output on Tamper Pin */
BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
#endif
/* Clear reset flags */
RCC_ClearFlag();
return;
}
/*******************************************************************************
* Function Name : Time_Display
* Description : Printf Time
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void Time_Display(void)
{
struct tm time;
time = Time_GetCalendarTime();
printf("Time: %d-%d-%d %02d:%02d:%02d \r\n", time.tm_year, \
time.tm_mon+1, time.tm_mday,\
time.tm_hour, time.tm_min, time.tm_sec);
}
دیدگاه