STM32CubeMx配置HAL库RTC

RTC (Real Time Clock):实时时钟

软件设置

  • SYS->Debug->Serial Wire
  • RCC->HSE->Crystal/Ceramic Resonator
  • RCC->LSE->Crystal/Ceramic Resonator

image-20220721162800394

  • 时钟树将RTC的频率设置为32.768KHz

image-20220721162934893

  • USART1->Mode->Asynchronous

image-20220721165423421

  • RTC激活时钟源,激活日历,RTC OUT选择No RTC Output
  • 设置日历初始时间日期

image-20220721163342315

  • 打开中断

image-20220721164028876

程序修改

main.c中

/* USER CODE BEGIN PV */
RTC_DateTypeDef GetDate;
RTC_TimeTypeDef GetTime;
RTC_AlarmTypeDef sAlarm;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

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

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_RTC_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  __HAL_RTC_SECOND_ENABLE_IT(&hrtc,RTC_IT_SEC);//开启秒中断
  sAlarm.Alarm = RTC_ALARM_A;
  sAlarm.AlarmTime.Hours=17;
  sAlarm.AlarmTime.Minutes=10;
  sAlarm.AlarmTime.Seconds=0;
  HAL_RTC_SetAlarm_IT(&hrtc,&sAlarm,RTC_FORMAT_BIN);//设置闹钟并使能闹钟中断
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
      HAL_Delay(1000);
      HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

rtc.c

定义外部变量

/* USER CODE BEGIN 0 */
#include "stdio.h"
extern RTC_DateTypeDef GetDate;
extern RTC_TimeTypeDef GetTime;
/* USER CODE END 0 */

编写秒中断处理回调函数和闹钟中断处理函数

/* USER CODE BEGIN 1 */
void HAL_RTCEx_RTCEventCallback(RTC_HandleTypeDef *hrtc)
{
    HAL_RTC_GetDate(hrtc,&GetDate,RTC_FORMAT_BIN);
    HAL_RTC_GetTime(hrtc,&GetTime,RTC_FORMAT_BIN);
    printf("Date:%02d-%02d-%02d\r\n",2000+GetDate.Year,GetDate.Month,GetDate.Date);
    printf("Time:%02d:%02d:%02d\r\n",GetTime.Hours,GetTime.Minutes,GetTime.Seconds);
    printf("\r\n");
}
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc){
    printf("Alarm Active...!!!\n");
}
/* USER CODE END 1 */

image-20220721172003463

1. 打开STM32CubeMX,选择对应的MCU型号 2. 在Pinout & Configuration中配置RTC外设的引脚 3. 在Clock Configuration中开启LSE(外部低速晶振),并选择对应的频率 4. 在Project Manager中选择Generate Code,生成代码 5. 打开生成的工程,在main函数中初始化RTC外设,并设置闹钟时间 6. 在RTC中断回调函数中处理闹钟响应事件 以下是一个示例代码,用于设置闹钟时间为2022年1月1日0时0分0秒: ``` #include "stm32f4xx_hal.h" #include "stm32f4xx_hal_rtc.h" RTC_HandleTypeDef hrtc; void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_RTC_Init(void); int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_RTC_Init(); RTC_TimeTypeDef sTime = {0}; RTC_DateTypeDef sDate = {0}; RTC_AlarmTypeDef sAlarm = {0}; sTime.Hours = 0; sTime.Minutes = 0; sTime.Seconds = 0; sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sTime.StoreOperation = RTC_STOREOPERATION_RESET; HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN); sDate.WeekDay = RTC_WEEKDAY_SATURDAY; sDate.Month = RTC_MONTH_JANUARY; sDate.Date = 1; sDate.Year = 22; HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN); sAlarm.AlarmTime.Hours = 0; sAlarm.AlarmTime.Minutes = 0; sAlarm.AlarmTime.Seconds = 0; sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET; sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY; sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE; sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE; sAlarm.AlarmDateWeekDay = 1; sAlarm.Alarm = RTC_ALARM_A; HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN); while (1) { } } void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) { // 处理闹钟事件 } void MX_RTC_Init(void) { 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; if (HAL_RTC_Init(&hrtc) != HAL_OK) { Error_Handler(); } } ``` 在上述代码中,设置了RTC时间为2022年1月1日0时0分0秒,闹钟时间也为2022年1月1日0时0分0秒。在RTC中断回调函数中,可以编写代码处理闹钟响应事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值