【蓝桥杯嵌入式】7_RTC
1、RTC的三个可用时钟源
系统时钟框图中的RTC部分:
RTC可选时钟源:
特别说明:
蓝桥杯嵌入式开发板CT117E
只能使用LSI
即内置RC
振荡器。
2、RTC框图
3、写代码前需要知道的一些标志位
RTOFF
:
0:上一次对RTC的写操作仍在进行
1:上一次对RTC的写操作已经结束CNF
:
0:不允许配置
1:允许配置RSF
:
0:寄存器尚未被同步
1:寄存器已经被同步
只有被同步后才能正常读取数据
注:了解标志位只是为了更好的理解工作流程在库函数开发中不涉及具体操作。
因为这些标志位的存在所以在使用库函数的时候有了这两个函数:
RTC_WaitForLastTask();//等待最近一次对 RTC 寄存器的写操作完成
RTC_WaitForSynchro();//等待APB1时钟和RTC时钟同步
4、配置流程
1)使能电源时钟和备份区域时钟。
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
2)取消备份区写保护。
PWR_BackupAccessCmd(ENABLE); //使能 RTC 和后备寄存器访问
3)复位备份区域,开启LSI振荡器。
BKP_DeInit();//复位备份区域
RCC_LSICmd(ENABLE); //开启LSI振荡器。
4)选择 RTC 时钟,并使能。
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); //选择LSI作为时钟源
RCC_RTCCLKCmd(ENABLE); //使能RTC时钟
5)设置 RTC 的分频,以及配置 RTC 时钟。
RTC_EnterConfigMode();/// 允许配置
RTC_ExitConfigMode();//退出配置模式,更新配置
void RTC_SetPrescaler(uint32_t PrescalerValue);//设置 RTC 时钟分频数
void RTC_ITConfig(uint16_t RTC_IT, FunctionalState NewState);//设置秒中断允许
RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能 RTC 秒中断
void RTC_SetCounter(uint32_t CounterValue)//是设置时间
6)更新配置,设置 RTC 中断分组。
RTC_ExitConfigMode();//退出配置模式,更新配置
注:以下函数比赛时可不用
//在退出配置模式更新配置之后我们在备份区域 BKP_DR1 中写入 0X5050 代表我们已经初始化过时钟了,下次开机(或复位)的时候,先读取 BKP_DR1 的值,然后判断是否是 0X5050 来决定是不是要配置。
void BKP_WriteBackupRegister(uint16_t BKP_DR, uint16_t Data);//往备份区域写用户数据的函数
uint16_t BKP_ReadBackupRegister(uint16_t BKP_DR);读备份区域内数据的函数
5、源代码
main.c
:
#include "Headfile.h"
void Delay_Ms(u32 nTime);
u32 TimingDelay = 0;
//Main Body
int main(void)
{
SysTick_Config(SystemCoreClock/1000);
Delay_Ms(200);
STM3210B_LCD_Init();
LCD_Clear(Blue);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
RTCInit(23,59,55);
while(1);
}
//
void Delay_Ms(u32 nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
RTC.c
:
#include "Headfile.h"
void RTCInit(u8 Hour,u8 Min,u8 Sec)
{
//中断配置
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//时钟电源配置
RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP|RCC_APB1Periph_PWR,ENABLE);//使能电源时钟和备份区域时钟
PWR_BackupAccessCmd(ENABLE);//取消备份区写保护
BKP_DeInit();//复位备份区域
RCC_LSICmd(ENABLE);//开启LSI震荡器
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);//等待时钟使能完毕
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); //选择LSI作为时钟源
RCC_RTCCLKCmd(ENABLE);//使能RTC时钟
//RTC配置
RTC_WaitForLastTask();//等待最近一次对 RTC 寄存器的写操作完成
RTC_WaitForSynchro();//等待APB1时钟和RTC时钟同步
RTC_WaitForLastTask();
RTC_EnterConfigMode();
RTC_WaitForLastTask();
RTC_SetPrescaler(40000-1);//设置预分频系数
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_SEC,ENABLE);//使能RTC秒中断
RTC_WaitForLastTask();
RTC_SetCounter(Hour*3600 + Min*60 + Sec);//设置时间
RTC_ExitConfigMode();
}
void RTCtoTFT()
{
u32 Time = 0;
u8 Hour = 0,Min = 0,Sec = 0;
u8 str[20];
Time = RTC_GetCounter();
RTC_WaitForLastTask();
if(Time == (23*3600 + 59*60 + 59))
{
RTC_SetCounter(0);
RTC_WaitForLastTask();
}
Hour = Time / 3600;
Min = (Time % 3600) / 60;
Sec = (Time % 3600) % 60;
sprintf((char *)str,"%.2d:%.2d:%.2d", Hour, Min, Sec);
LCD_DisplayStringLine(Line5,str);
}
stm32f10x_it.c
/**
******************************************************************************
* @file I2S/SPI_I2S_Switch/stm32f10x_it.c
* @author MCD Application Team
* @version V3.5.0
* @date 08-April-2011
* @brief Main Interrupt Service Routines.
* This file provides template for all exceptions handler and peripherals
* interrupt service routine.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "Headfile.h"
extern u32 TimingDelay;
/** @addtogroup STM32F10x_StdPeriph_Examples
* @{
*/
/** @addtogroup I2S_SPI_I2S_Switch
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/******************************************************************************/
/* Cortex-M3 Processor Exceptions Handlers */
/******************************************************************************/
/**
* @brief This function handles NMI exception.
* @param None
* @retval None
*/
void NMI_Handler(void)
{
}
/**
* @brief This function handles Hard Fault exception.
* @param None
* @retval None
*/
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Fault exception occurs */
while (1)
{}
}
/**
* @brief This function handles Memory Manage exception.
* @param None
* @retval None
*/
void MemManage_Handler(void)
{
/* Go to infinite loop when Memory Manage exception occurs */
while (1)
{}
}
/**
* @brief This function handles Bus Fault exception.
* @param None
* @retval None
*/
void BusFault_Handler(void)
{
/* Go to infinite loop when Bus Fault exception occurs */
while (1)
{}
}
/**
* @brief This function handles Usage Fault exception.
* @param None
* @retval None
*/
void UsageFault_Handler(void)
{
/* Go to infinite loop when Usage Fault exception occurs */
while (1)
{}
}
/**
* @brief This function handles Debug Monitor exception.
* @param None
* @retval None
*/
void DebugMon_Handler(void)
{
}
/**
* @brief This function handles SVCall exception.
* @param None
* @retval None
*/
void SVC_Handler(void)
{
}
/**
* @brief This function handles PendSV_Handler exception.
* @param None
* @retval None
*/
void PendSV_Handler(void)
{
}
/**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
TimingDelay--;
}
//
void RTC_IRQHandler(void)
{
if(RTC_GetITStatus(RTC_IT_SEC) == 1)
{
RTC_ClearITPendingBit(RTC_IT_SEC);
RTCtoTFT();
}
}
//
/******************************************************************************/
/* STM32F10x Peripherals Interrupt Handlers */
/* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */
/* available peripheral interrupt handler's name please refer to the startup */
/* file (startup_stm32f10x_xx.s). */
/******************************************************************************/
/**
* @brief This function handles PPP interrupt request.
* @param None
* @retval None
*/
/*void PPP_Switch_IRQHandler(void)
{
}*/
/**
* @}
*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
6、关于设置闹钟这件事
本文地址:https://blog.csdn.net/weixin_43444989/article/details/108183702