你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32G431RBTx 基本模块 RTC

[复制链接]
STMCU小助手 发布时间:2023-3-1 20:42
前言
由于第八届赛题中出现了要求使用单片机的RTC功能。

3157b8e6bd02447c9d3d6cf3124558ef.png

所以本节我们学习RTC的使用。


1.CubeMx的配置步骤
修改之前的UART的ioc文件来生成工程。

c2a2c77b01a04368a514ce14dd939dac.png

8d23fe7214084b268bdb57fc70403110.png

2.生成工程
点击GENERATER CODE生成工程Project即可。

f8512451c5d946ff9a001cae802bbdb2.png


3.测试代码
main.c
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>© Copyright (c) 2023 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "rtc.h"
  23. #include "usart.h"
  24. #include "gpio.h"

  25. /* Private includes ----------------------------------------------------------*/
  26. /* USER CODE BEGIN Includes */
  27. #include "stdlib.h"
  28. #include "stdio.h"
  29. /* USER CODE END Includes */

  30. /* Private typedef -----------------------------------------------------------*/
  31. /* USER CODE BEGIN PTD */

  32. /* USER CODE END PTD */

  33. /* Private define ------------------------------------------------------------*/
  34. /* USER CODE BEGIN PD */
  35. /* USER CODE END PD */

  36. /* Private macro -------------------------------------------------------------*/
  37. /* USER CODE BEGIN PM */

  38. /* USER CODE END PM */

  39. /* Private variables ---------------------------------------------------------*/

  40. /* USER CODE BEGIN PV */

  41. /* USER CODE END PV */

  42. /* Private function prototypes -----------------------------------------------*/
  43. void SystemClock_Config(void);
  44. /* USER CODE BEGIN PFP */

  45. /* USER CODE END PFP */

  46. /* Private user code ---------------------------------------------------------*/
  47. /* USER CODE BEGIN 0 */
  48. RTC_TimeTypeDef T; //储存时间用的两个结构体
  49. RTC_DateTypeDef D;
  50. unsigned char second;
  51. /* USER CODE END 0 */

  52. /**
  53.   * @brief  The application entry point.
  54.   * @retval int
  55.   */
  56. int main(void)
  57. {
  58.   /* USER CODE BEGIN 1 */

  59.   /* USER CODE END 1 */

  60.   /* MCU Configuration--------------------------------------------------------*/

  61.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  62.   HAL_Init();

  63.   /* USER CODE BEGIN Init */

  64.   /* USER CODE END Init */

  65.   /* Configure the system clock */
  66.   SystemClock_Config();

  67.   /* USER CODE BEGIN SysInit */

  68.   /* USER CODE END SysInit */

  69.   /* Initialize all configured peripherals */
  70.   MX_GPIO_Init();
  71.   MX_RTC_Init();
  72.   MX_USART1_UART_Init();
  73.   /* USER CODE BEGIN 2 */

  74.   /* USER CODE END 2 */

  75.   /* Infinite loop */
  76.   /* USER CODE BEGIN WHILE */
  77.   while (1)
  78.   {
  79.     /* USER CODE END WHILE */

  80.     /* USER CODE BEGIN 3 */
  81.                 HAL_RTC_GetTime(&hrtc, &T, RTC_FORMAT_BIN); //采集时间
  82.                 HAL_RTC_GetDate(&hrtc, &D, RTC_FORMAT_BIN);
  83.                 if(second != T.Seconds) //打印到串口
  84.                 {
  85.                         second = T.Seconds;
  86.                         printf("%02d-%02d-%02d-%02d-%02d-%02d\r\n",D.Year,D.Month,D.Date,T.Hours,T.Minutes,T.Seconds);
  87.                 }

  88.   }
  89.   /* USER CODE END 3 */
  90. }

  91. /**
  92.   * @brief System Clock Configuration
  93.   * @retval None
  94.   */
  95. void SystemClock_Config(void)
  96. {
  97.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  98.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  99.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  100.   /** Configure the main internal regulator output voltage
  101.   */
  102.   HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  103.   /** Initializes the RCC Oscillators according to the specified parameters
  104.   * in the RCC_OscInitTypeDef structure.
  105.   */
  106.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  107.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  108.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  109.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  110.   RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
  111.   RCC_OscInitStruct.PLL.PLLN = 20;
  112.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  113.   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  114.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  115.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  116.   {
  117.     Error_Handler();
  118.   }
  119.   /** Initializes the CPU, AHB and APB buses clocks
  120.   */
  121.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  122.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  123.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  124.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  125.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  126.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  127.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  128.   {
  129.     Error_Handler();
  130.   }
  131.   /** Initializes the peripherals clocks
  132.   */
  133.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART1;
  134.   PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  135.   PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV32;

  136.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  137.   {
  138.     Error_Handler();
  139.   }
  140. }

  141. /* USER CODE BEGIN 4 */
  142. int fputc(int ch, FILE *f)
  143. {
  144.         HAL_UART_Transmit(&huart1, (unsigned char *)&ch, 1, HAL_MAX_DELAY);
  145.         return ch;
  146. }
  147. /* USER CODE END 4 */

  148. /**
  149.   * @brief  This function is executed in case of error occurrence.
  150.   * @retval None
  151.   */
  152. void Error_Handler(void)
  153. {
  154.   /* USER CODE BEGIN Error_Handler_Debug */
  155.   /* User can add his own implementation to report the HAL error return state */
  156.   __disable_irq();
  157.   while (1)
  158.   {
  159.   }
  160.   /* USER CODE END Error_Handler_Debug */
  161. }

  162. #ifdef  USE_FULL_ASSERT
  163. /**
  164.   * @brief  Reports the name of the source file and the source line number
  165.   *         where the assert_param error has occurred.
  166.   * @param  file: pointer to the source file name
  167.   * @param  line: assert_param error line source number
  168.   * @retval None
  169.   */
  170. void assert_failed(uint8_t *file, uint32_t line)
  171. {
  172.   /* USER CODE BEGIN 6 */
  173.   /* User can add his own implementation to report the file name and line number,
  174.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  175.   /* USER CODE END 6 */
  176. }
  177. #endif /* USE_FULL_ASSERT */

  178. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

复制代码

4.演示效果

3a8764baa28f4c74830ffb102a9d76a4.gif

————————————————
版权声明:火花页.

收藏 评论0 发布时间:2023-3-1 20:42

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版