STM32G0单片机自带RTC

STM32有个自带RTC外设,外接32.768KHz的晶振后可得到相对精确的计时功能。
实测了一个一小时快个1秒多。

1 cubeMX设置了RTC后自动生成的初始化代码如下

static void MX_RTC_Init(void)
{

  /* USER CODE BEGIN RTC_Init 0 */

  /* USER CODE END RTC_Init 0 */

  RTC_TimeTypeDef sTime = {0};
  RTC_DateTypeDef sDate = {0};

  /* USER CODE BEGIN RTC_Init 1 */

  /* USER CODE END RTC_Init 1 */

  /** Initialize RTC Only
  */
  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.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }

  /* USER CODE BEGIN Check_RTC_BKUP */

  /* USER CODE END Check_RTC_BKUP */

  /** Initialize RTC and set the Time and Date
  */
  sTime.Hours = 9;
  sTime.Minutes = 0;
  sTime.Seconds = 0;
  sTime.SubSeconds = 0;
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  sDate.Month = RTC_MONTH_JANUARY;
  sDate.Date = 1;
  sDate.Year = 25;

  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN RTC_Init 2 */

  /* USER CODE END RTC_Init 2 */

}

2 修改RTC时间设置

HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN)HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN)

3 读取当前日期时间

HAL_RTC_GetTime(&RTC_Handler,&RTC_TimeStruct,RTC_FORMAT_BIN);
HAL_RTC_GetDate(&RTC_Handler,&RTC_DateStruct,RTC_FORMAT_BIN);

注意先读时间,再读日期。如果顺序调过来,会很耗时(像被卡住好几秒)。

### STM32G Series MCU RTC Wakeup Configuration For configuring the Real-Time Clock (RTC) to perform a wake-up operation on STM32G series microcontrollers, several steps are necessary. The process involves initializing the RTC peripheral, setting up the wake-up timer period, enabling interrupts or events associated with this feature, and ensuring that low-power modes like Stop mode can be exited using these configurations. The following sections detail how one might configure such functionality along with an example of implementing it through software means: #### Initialization of RTC Peripheral To start utilizing the RTC's capabilities including its ability to generate periodic wake-ups from low power states, initialization is required first. This includes unlocking write protection for BKP domain registers which contain settings related to backup SRAM as well as RTC itself; then proceeding by configuring prescalers according to desired time base frequency before finally locking back again after setup completion[^1]. ```c RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); PWR_BackupAccessCmd(ENABLE); // Unlock Backup Domain Control Register access. BKP_DeInit(); // Enable LSE Oscillator used as RTC clock source. RCC_LSEConfig(RCC_LSE_ON); 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 until last write operation on RTC registers has finished. RTC_WaitForLastTask(); ``` #### Setting Up Wake-Up Timer Period Once initialized properly, next comes defining what duration should elapse between each successive interrupt/event generated due to expiration of set intervals within configured parameters inside `RTC_InitTypeDef` structure where members like `RTCWakeUpCounter` define count value while others specify whether counter will increment continuously during sleep/stop modes etc.[^2] ```c RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 0xFF; RTC_Init(&RTC_InitStructure); RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits); RTC_SetWakeUpCounter(0xFFFF); // Set maximum interval initially // Enable Wakeup Interrupts RTC_ITConfig(RTC_IT_WUT, ENABLE); ``` #### Enabling Low Power Mode Exit via RTC Alarm/Wakeup Events Finally, once everything above has been correctly established, entering into lower consumption states becomes possible knowing there exists mechanisms allowing exit based upon predefined conditions met when certain hardware timers reach their programmed values causing system-wide resets or simply waking CPU core without affecting other peripherals' operational status quo ante bellum[^3]. ```c // Enter STOP mode PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFE); // After exiting stop mode... if (__HAL_RCC_GET_FLAG(__HAL_RCC_FLAG_PIN_RESET)) { // Handle reset condition here if needed } else if (__HAL_RCC_GET_FLAG(__HAL_RCC_FLAG_WWDGRST)) { // Window Watchdog Reset occurred } /* Add more checks as per application requirements */ ``` --related questions-- 1. How does changing the RTC clock source affect accuracy? 2. What precautions must be taken when writing to RTC registers directly? 3. Can you explain different types of low-power modes available in STM32 devices? 4. Is it possible to use both alarms and wakeup timers simultaneously? If yes, how would conflicts be managed? 5. Are there any specific considerations regarding battery-backed memory usage alongside RTC features? [^1]: Describes initial configuration steps involving unlock/write-protection removal followed by oscillator enablement and selection processes essential prior to employing RTC functionalities effectively. [^2]: Explains parameterization aspects concerning definition of timing intervals utilized specifically around wake-up generation logic implementation details. [^3]: Discusses integration points linking previously mentioned setups towards achieving efficient entry/exits out/from reduced electrical activity periods leveraging built-in real-time monitoring components found onboard selected ARM Cortex-M family processors.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code .

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值