【专题STM32F103】关于STM32Cube_FW_F1_V1.8.0内的example顶层程序设计逻辑 与 RTC_Calendar增补

 Examples内程序结构

STM32Cube_FW_F1_V1.8.0\Projects\STM3210E_EVAL\Examples内程序结构分析如下:

使用外设XXX

向工程添加 stm32f10x_XXX.c

修改 stm32f10x_conf.h 

在stm32f1xx_hal_msp.c中写  外设XXX写初始化程序

在stm32f1xx_it.c中写 中断服务程序

在main.c中写 配置程序、功能程序。

实时时钟RTC_Calendar增补

在STM32Cube_FW_F1_V1.8.0\Projects\STM3210E_EVAL\Examples\RTC\RTC_Calendar

修改 stm32f1xx_hal_msp.c内  void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc) 函数

void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
{
  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();
  
  /* Enable BKP CLK for backup registers */
  __HAL_RCC_BKP_CLK_ENABLE();
     
   __HAL_RCC_RTC_ENABLE();// 补上开RTC工作时钟

修改 main.c文件 main()内代码

  /*##-1- Configure the RTC peripheral #######################################*/
  /* Configure RTC prescaler and RTC data registers */
  /* RTC configured as follow:
      - Asynch Prediv  = Calculated automatically by HAL */
  RtcHandle.Instance = RTC; 
  RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND;

  if (HAL_RTC_Init(&RtcHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /*##-2- Check if Data stored in BackUp register1: No Need to reconfigure RTC#*/
  /* Read the Back Up Register 1 Data */
    RTC_CalendarConfig();                                     //增加此处,用于调试时修改时间。
  if (HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR1) != 0x32F2)//RTC有电池时,将不能第二次修改时间
  {
    /* Configure RTC Calendar */
    RTC_CalendarConfig();
  }
  else
  {
    /* Check if the Power On Reset flag is set */
    if (__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET)
    {
      /* Turn on LED2: Power on reset occurred */
      BSP_LED_On(LED2);
    }
    /* Check if Pin Reset flag is set */
    if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET)
    {
      /* Turn on LED4: External reset occurred */
      BSP_LED_On(LED4);
    }
    /* Clear source Reset Flag */
    __HAL_RCC_CLEAR_RESET_FLAGS();
  }

 

posted @ 2023-05-19 18:22  辛河  阅读(224)  评论(0编辑  收藏  举报