f103 hal RTC

    技术2022-07-11  119

    1.初始化RTC

    RTC_handle.Instance = RTC; RTC_handle.Lock = HAL_UNLOCKED; RTC_handle.Init.AsynchPrediv = RTC_AUTO_1_SECOND; RTC_handle.Init.OutPut = RTC_OUTPUTSOURCE_ALARM; if(HAL_RTC_Init(&RTC_handle) != HAL_OK) { HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_SET); }

    2.重写HAL_RTC_MspInit()函数

    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(); /*##-2- Configue LSE/LSI as RTC clock soucre ###############################*/ #ifdef RTC_CLOCK_SOURCE_LSE RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.LSEState = RCC_LSE_ON; RCC_OscInitStruct.LSIState = RCC_LSI_OFF; if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { } PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { } #elif defined (RTC_CLOCK_SOURCE_LSI) RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.LSIState = RCC_LSI_ON; RCC_OscInitStruct.LSEState = RCC_LSE_OFF; if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { } PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { } #else #error Please select the RTC Clock source inside the main.h file #endif /*RTC_CLOCK_SOURCE_LSE*/ /*##-2- Enable RTC peripheral Clocks #######################################*/ /* Enable RTC Clock */ __HAL_RCC_RTC_ENABLE(); /*##-4- Configure the NVIC for RTC Alarm ###################################*/ HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0x0F, 0); HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn); }

    3.定义用哪一个时钟源

    可以定义在main.h文件中

    //#define RTC_CLOCK_SOURCE_LSI #define RTC_CLOCK_SOURCE_LSE

    4.配置时间和闹钟

    date.Year = 0x20; date.Month = RTC_MONTH_JUNE; date.Date = 0x30; date.WeekDay = RTC_WEEKDAY_TUESDAY; if(HAL_RTC_SetDate(&RTC_handle,&date,RTC_FORMAT_BCD)!= HAL_OK) { //HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_SET); } time.Hours = 0x15; time.Minutes = 0x10; ime.Seconds = 0x00; if(HAL_RTC_SetTime(&RTC_handle,&time,RTC_FORMAT_BCD)!= HAL_OK) { HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_SET); } alarm.Alarm = RTC_ALARM_A; //配置闹钟的时间 alarm.AlarmTime.Hours = 0x15; alarm.AlarmTime.Minutes = 0x10; alarm.AlarmTime.Seconds = 0x05; HAL_RTC_SetAlarm_IT(&RTC_handle,&alarm,RTC_FORMAT_BCD);

    5.重写RTC_Alarm_IRQHandler

    让其进去HAL_RTC_AlarmIRQHandler()

    void RTC_Alarm_IRQHandler(void) { HAL_RTC_AlarmIRQHandler(&RTC_handle); }

    6.重写HAL_RTC_AlarmAEventCallback()函数

    进入闹钟中断执行里面的代码段。

    void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) { HAL_RTC_GetTime(&RTC_handle,&time,RTC_FORMAT_BCD); /*if(date.Year == 0x20) {*/ HAL_UART_Transmit(&UART_initStruct, (uint8_t*)&time.Seconds, 1,1000); }

    7.整份代码

    #include "rtc.h" RTC_HandleTypeDef RTC_handle; RTC_DateTypeDef date; RTC_TimeTypeDef time; RTC_AlarmTypeDef alarm; void RTC_Init(void) { RTC_handle.Instance = RTC; RTC_handle.Lock = HAL_UNLOCKED; RTC_handle.Init.AsynchPrediv = RTC_AUTO_1_SECOND; RTC_handle.Init.OutPut = RTC_OUTPUTSOURCE_ALARM; if(HAL_RTC_Init(&RTC_handle) != HAL_OK) { HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_SET); } } void RTC_alarmconfig(void) { date.Year = 0x20; date.Month = RTC_MONTH_JUNE; date.Date = 0x30; date.WeekDay = RTC_WEEKDAY_TUESDAY; if(HAL_RTC_SetDate(&RTC_handle,&date,RTC_FORMAT_BCD)!= HAL_OK) { //HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_SET); } time.Hours = 0x15; time.Minutes = 0x10; time.Seconds = 0x00; if(HAL_RTC_SetTime(&RTC_handle,&time,RTC_FORMAT_BCD)!= HAL_OK) { HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_SET); } alarm.Alarm = RTC_ALARM_A; alarm.AlarmTime.Hours = 0x15; alarm.AlarmTime.Minutes = 0x10; alarm.AlarmTime.Seconds = 0x05; HAL_RTC_SetAlarm_IT(&RTC_handle,&alarm,RTC_FORMAT_BCD); } 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(); /*##-2- Configue LSE/LSI as RTC clock soucre ###############################*/ #ifdef RTC_CLOCK_SOURCE_LSE RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.LSEState = RCC_LSE_ON; RCC_OscInitStruct.LSIState = RCC_LSI_OFF; if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { } PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { } #elif defined (RTC_CLOCK_SOURCE_LSI) RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.LSIState = RCC_LSI_ON; RCC_OscInitStruct.LSEState = RCC_LSE_OFF; if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { } PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { } #else #error Please select the RTC Clock source inside the main.h file #endif /*RTC_CLOCK_SOURCE_LSE*/ /*##-2- Enable RTC peripheral Clocks #######################################*/ /* Enable RTC Clock */ __HAL_RCC_RTC_ENABLE(); /*##-4- Configure the NVIC for RTC Alarm ###################################*/ HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0x0F, 0); HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn); } void RTC_Alarm_IRQHandler(void) { HAL_RTC_AlarmIRQHandler(&RTC_handle); } void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) { HAL_RTC_GetTime(&RTC_handle,&time,RTC_FORMAT_BCD); /*if(date.Year == 0x20) {*/ HAL_UART_Transmit(&UART_initStruct, (uint8_t*)&time.Seconds, 1,1000); }
    Processed: 0.014, SQL: 9