搜索
bottom↓
回复: 2

STM32L476 RTC不走呢???!!

[复制链接]

出0入0汤圆

发表于 2018-9-30 15:14:03 | 显示全部楼层 |阅读模式
本人刚接触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周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

 楼主| 发表于 2018-9-30 15:15:25 | 显示全部楼层
使用了LSI时钟

出0入8汤圆

发表于 2019-6-23 14:45:01 | 显示全部楼层

为啥我的 用LSI 仿真的时候能走,自己跑就不能走了
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-3-29 06:32

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表