STM32使用RTC(hal)

本文介绍了STM32的RTC模块,详细阐述了RTC的配置过程,包括使能RTC时钟源、日历初始化以及中断设置。通过HAL库实现了一秒钟输出一次的计时功能,总结了利用中断进行计时的基本原理。

一,RTC介绍

实时时钟的缩写是RTC(Real_Time Clock)。RTC 是集成电路,通常称为时钟芯片。
-实时时钟是一个独立的定时器。RTC模块拥有一组连续计数的计数器,在相应软件配置下,可提供时钟日历的功能。修改计数器的值可以重新设置系统当前的时间和日期。
RTC模块和时钟配置系统(RCC_BDCR寄存器)处于后备区域,即在系统复位或从待机模式唤醒后, RTC的设置和时间维持不变。
系统复位后,对后备寄存器和RTC的访问被禁止,这是为了防止对后备区域(BKP)的意外写操作。执行以下操作将使能对后备寄存器和RTC的访问:
● 设置寄存器RCC_APB1ENR的PWREN和BKPEN位,使能电源和后备接口时钟
● 设置寄存器PWR_CR的DBP位,使能对后备寄存器和RTC的访问。

二,HAL选项

RCC配置,这个要使能外部低速时钟,SYS配置可以不用变
在这里插入图片描述
RTC模块使能时钟源和使能日历,同时将RTC_OUT选择为no rtc output,以及把下面的中断加上
RTC_OUT是是否使能tamper(PC13)引脚上输出校正的秒脉冲时钟。
在这里插入图片描述在parameter setting模块可以初始化时间,也可以选择在代码里初始化时间
要用串口发送信息,所以使能一个usart1
在这里插入图片描述
然后就是和以前一样的生成文件就OK了

三,代码

main

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license
### STM32H7 RTC HAL Library Usage For the STM32H7 microcontroller series, configuring and using the Real-Time Clock (RTC) module with the Hardware Abstraction Layer (HAL) library involves several key steps to ensure proper initialization and operation of this peripheral. To manually add the RTC functionality without utilizing STM32CubeMX: - Copy `rtc.c` and `rtc.h` files generated by a method such as STM32CubeMX into your project. - Enable the macro definition `HAL_RTC_MODULE_ENABLED` within `stm32h7xx_hal_conf.h`. - Adjust configurations according to specific requirements of the current engineering environment[^1]. Regarding Systick functionalities which are initialized after calling `HAL_Init()`, these include but not limited to incrementing ticks, getting tick count or priority level, setting/getting tick frequency, delaying execution based on millisecond counts, suspending/resuming ticking behavior. However, direct correlation between SysTick operations and RTC is minimal unless specifically programmed otherwise[^2]. When dealing specifically with UART communication alongside RTC setup, an example function demonstrates how characters can be transmitted over USART2 interface through HAL functions like so: ```c int fputc(int ch, FILE *f) { HAL_UART_Transmit(&huart2, (uint8_t*)&ch, 1, 0xFFFF); return ch; } ``` This snippet shows integration points where serial communications might interact indirectly when debugging or logging from within applications that utilize both peripherals simultaneously[^3]. In terms of enabling external low-speed clock sources necessary for accurate timekeeping provided by RTC hardware, along with other settings related to calendar features and interrupt handling mechanisms, one should also consider initializing parameters either via graphical tools offered by development environments or directly coding them at startup sequences before entering main application logic loops[^4]: ```c // Example configuration excerpt RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; /* Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSE; ... PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK || \ HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK){ Error_Handler(); } __HAL_RCC_RTC_ENABLE(); // Initialize RTC structure here... HAL_RTC_Init(&hrtc); // Optionally set initial date/time values if needed ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值