
今天终于实现了L053的RTC时钟 下面是代码 #define __RTC_WRITEPROTECTION_DISABLE() \ do \ { \ RTC->WPR = 0xCA; \ RTC->WPR = 0x53; \ } while (0) /** * @brief Enable the write protection for RTC registers. * @param __HANDLE__: specifies the RTC handle. * @retval None */ #define __RTC_WRITEPROTECTION_ENABLE() \ do \ { \ RTC->WPR = 0xFF; \ } while (0) #define ASYN_PREDIVE_SHIFT 16 #define YEAR_SHIFT 16 #define MONTH_SHIFT 8 #define WEEKDAY_SHIFT 13 #define HOUR_SHIFT 16 #define MINUTE_SHIFT 8 #define TIMEFORMAT_SHIFT 13 #define PM_SHIFT 22 void RTC_Init(void) { RCC->CSR |= RCC_CSR_LSION; while ((RCC->CSR & RCC_CSR_LSIRDY) != RCC_CSR_LSIRDY) { } RCC->APB1ENR |= RCC_APB1ENR_PWREN; PWR->CR |= PWR_CR_DBP; RCC->CSR = (RCC->CSR & ~RCC_CSR_RTCSEL) | RCC_CSR_RTCEN | RCC_CSR_RTCSEL_1; RCC->APB1ENR &= ~RCC_APB1ENR_PWREN; /* Configure RTC */ __RTC_WRITEPROTECTION_DISABLE(); /* Clear RTC_CR FMT, OSEL and POL Bits */ RTC->CR &= ((uint32_t) ~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL)); /* Configure the RTC PRER */ RTC->PRER = (uint32_t)(RTC_SYNCH_PREDIV); RTC->PRER |= (uint32_t)(RTC_ASYNCH_PREDIV << ASYN_PREDIVE_SHIFT); /* Exit Initialization mode */ RTC->ISR &= (uint32_t) ~RTC_ISR_INIT; RTC->OR &= (uint32_t) ~(RTC_OR_ALARMOUTTYPE | RTC_OR_RTC_OUT_RMP); __RTC_WRITEPROTECTION_ENABLE(); } |
void
RTC_SetDate(RTC_DateTypeDef * st_Date)
{
uint32_t u32_TateTmp = 0;
u32_TateTmp = ((((uint32_t)st_Date->Year) << YEAR_SHIFT) | \
(((uint32_t)st_Date->Month) << MONTH_SHIFT) | \
((uint32_t)st_Date->Date) | \
(((uint32_t)st_Date->WeekDay) << WEEKDAY_SHIFT));
__RTC_WRITEPROTECTION_DISABLE();
RTC->ISR |= RTC_ISR_INIT;
while ((RTC->ISR & RTC_ISR_INITF) != RTC_ISR_INITF)
{
}
RTC->DR = (uint32_t)(u32_TateTmp & RTC_DR_RESERVED_MASK);
RTC->ISR &= (uint32_t) ~RTC_ISR_INIT;
__RTC_WRITEPROTECTION_ENABLE();
}
void
RTC_SetTime(RTC_TimeTypeDef * st_Time)
{
uint32_t u32_TimeTmp = 0;
st_Time->TimeFormat = RTC_HOURFORMAT_24;
u32_TimeTmp = (((uint32_t)(st_Time->Hours) << HOUR_SHIFT) | \
((uint32_t)(st_Time->Minutes) << MINUTE_SHIFT) | \
((uint32_t)st_Time->Seconds) | \
((uint32_t)(st_Time->TimeFormat) << TIMEFORMAT_SHIFT));
__RTC_WRITEPROTECTION_DISABLE();
RTC->ISR = RTC_INIT_MASK;
while ((RTC->ISR & RTC_ISR_INITF) != RTC_ISR_INITF)
{
}
/* Set the RTC_TR register */
RTC->TR = (uint32_t)(u32_TimeTmp & RTC_TR_RESERVED_MASK);
/* Clear the bits to be configured */
RTC->CR &= (uint32_t) ~RTC_CR_BCK;
/* Configure the RTC_CR register */
RTC->CR |= (uint32_t)(st_Time->DayLightSaving | st_Time->StoreOperation);
/* Exit Initialization mode */
RTC->ISR &= (uint32_t) ~RTC_ISR_INIT;
__RTC_WRITEPROTECTION_ENABLE();
}
void
RTC_GetTime(RTC_TimeTypeDef * st_Time)
{
uint32_t u32_TimeTmp = 0;
/* Get subseconds values from the correspondent registers*/
st_Time->SubSeconds = (uint32_t)(RTC->SSR);
/* Get the TR register */
u32_TimeTmp = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK);
/* Fill the structure fields with the read parameters */
st_Time->Hours = (uint8_t)((u32_TimeTmp & (RTC_TR_HT | RTC_TR_HU)) >> HOUR_SHIFT);
st_Time->Minutes = (uint8_t)((u32_TimeTmp & (RTC_TR_MNT | RTC_TR_MNU)) >> MINUTE_SHIFT);
st_Time->Seconds = (uint8_t)(u32_TimeTmp & (RTC_TR_ST | RTC_TR_SU));
st_Time->TimeFormat = (uint8_t)((u32_TimeTmp & (RTC_TR_PM)) >> PM_SHIFT);
}