|
本人刚接触STML4系列MCU。原来使用103和407的RTC都很顺利的。这个的L476就是不运行呢!!??
主函数直接调用:MX_RTC_Init()。打印出来的时间一直是一个时间。
请大家多多指教!谢谢!!
#define RTC_CLOCK_SOURCE_LSI
//#define RTC_CLOCK_SOURCE_LSE
RTC_HandleTypeDef RtcHandle;
RTC_HandleTypeDef hrtc;
/* Buffers used for displaying Time and Date */
uint8_t aShowTime[50] = {0}, aShowTimeStamp[50] = {0};
uint8_t aShowDate[50] = {0}, aShowDateStamp[50] = {0};
/* RTC init function */
void MX_RTC_Init(void)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
/*##-1- Configure the RTC peripheral #######################################*/
/* Configure RTC prescaler and RTC data registers */
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
RT_ASSERT(HAL_RTC_Init(&hrtc) == HAL_OK);
/*##-2- Check if data stored in BackUp register1: Wakeup timer enable #######*/
/* Read the Back Up Register 1 Data */
if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2)
{
sTime.Hours = 22;
sTime.Minutes = 28;
sTime.Seconds = 0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
RT_ASSERT(HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) == HAL_OK);
sDate.WeekDay = RTC_WEEKDAY_THURSDAY;
sDate.Month = RTC_MONTH_OCTOBER;
sDate.Date = 26;
sDate.Year = 17;
RT_ASSERT(HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) == HAL_OK);
}
/*##-2- Check if data stored in BackUp register1: Wakeup timer enable #######*/
/* Read the Back Up Register 1 Data */
if (HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR1) == 0x32F2)
{
/* if the wakeup timer is enabled then deactivate it to disable the wakeup timer interrupt */
RT_ASSERT(HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle) == HAL_OK);
}
/*##-3- Configure the RTC Wakeup peripheral #################################*/
/* Setting the Wakeup time to 1 s
If RTC_WAKEUPCLOCK_CK_SPRE_16BITS is selected, the frequency is 1Hz,
this allows to get a wakeup time equal to 1 s if the counter is 0x0 */
HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, 0x0, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
/*##-4- Write 'wakeup timer enabled' tag in RTC Backup data Register 1 #######*/
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, 0x32F2);
}
void HAL_RTC_MspInit(RTC_HandleTypeDef *rtcHandle)
{
if (rtcHandle->Instance == RTC)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
/*##-1- Enables the PWR Clock and Enables access to the backup domain ###################################*/
/* To change the source clock of the RTC feature (LSE, LSI), You have to:
- Enable the power clock using __HAL_RCC_PWR_CLK_ENABLE()
- Enable write access using HAL_PWR_EnableBkUpAccess() function before to
configure the RTC clock source (to be done once after reset).
- Reset the Back up Domain using __HAL_RCC_BACKUPRESET_FORCE() and
__HAL_RCC_BACKUPRESET_RELEASE().
- Configure the needed RTC clock source */
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWR_EnableBkUpAccess();
/*##-2- Configure LSE/LSI as RTC clock source ###############################*/
#ifdef RTC_CLOCK_SOURCE_LSE
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
RT_ASSERT(HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK);
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
RT_ASSERT(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) == HAL_OK);
#elif defined (RTC_CLOCK_SOURCE_LSI)
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
RT_ASSERT(HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK);
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
RT_ASSERT(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) == HAL_OK);
#else
#error Please select the RTC Clock source inside the main.h file
#endif /*RTC_CLOCK_SOURCE_LSE*/
/*##-3- Enable RTC peripheral Clocks #######################################*/
/* Enable RTC Clock */
__HAL_RCC_RTC_ENABLE();
}
}
void HAL_RTC_MspDeInit(RTC_HandleTypeDef *rtcHandle)
{
if (rtcHandle->Instance == RTC)
{
__HAL_RCC_RTC_DISABLE();
}
}
void RTC_CalendarShow(void)
{
RTC_DateTypeDef sdatestructureget;
RTC_TimeTypeDef stimestructureget;
/* Get the RTC current Time */
HAL_RTC_GetTime(&RtcHandle, &stimestructureget, RTC_FORMAT_BIN);
/* Get the RTC current Date */
HAL_RTC_GetDate(&RtcHandle, &sdatestructureget, RTC_FORMAT_BIN);
/* Display date Format : mm-dd-yy */
rt_kprintf("Date: %.2d-%.2d-%.2d ", 2000 + sdatestructureget.Year,sdatestructureget.Month,sdatestructureget.Date);
/* Display time Format : hh:mm:ss */
rt_kprintf("Time: %.2d:%.2d:%.2d\r\n", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
}
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
{
/* Toggle LED2 */
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}
|
阿莫论坛20周年了!感谢大家的支持与爱护!!
一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
|