stm32之22.RTC时钟

cs 复制代码
void rtc_init(void)
{
	 RTC_DateTypeDef  RTC_DateStructure;
	 RTC_TimeTypeDef  RTC_TimeStructure;
	 RTC_InitTypeDef  RTC_InitStructure;


	 GPIO_InitTypeDef  GPIO_InitStructure;
	 USART_InitTypeDef USART_InitStructure;
	 NVIC_InitTypeDef  NVIC_InitStructure;
	 EXTI_InitTypeDef  EXTI_InitStructure;
	/* Enable the PWR clock ,使能电源控制时钟*/
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
	
	/* Allow access to RTC,允许访问备份电路 */
	PWR_BackupAccessCmd(ENABLE);


#if 0
	/* 使能LSE*/
	RCC_LSEConfig(RCC_LSE_ON);
	
	/* 检查该LSE是否有效*/  
	while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);

	/* 选择LSE作为RTC的硬件时钟源*/
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
	

#else  //若LSE无法工作,可用内部LSI
	/* 使能LSI*/
	RCC_LSICmd(ENABLE);
	
	/* 检查该LSI是否有效*/  
	while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);

	/* 选择LSI作为RTC的硬件时钟源*/
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);

#endif

	/* Enable the RTC Clock,使能RTC时钟 */
	RCC_RTCCLKCmd(ENABLE);
	
	/* Wait for RTC APB registers synchronisation,等待RTC相关寄存器就绪 */
	RTC_WaitForSynchro();
	
 #if 0 //LSE
	/* Configure the RTC data register and RTC prescaler,配置RTC数据寄存器与RTC的分频值 */
	RTC_InitStructure.RTC_AsynchPrediv = 127;				//异步分频系数
	RTC_InitStructure.RTC_SynchPrediv = 255;				//同步分频系数
	RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;	//24小时格式
	RTC_Init(&RTC_InitStructure);
#else //LSI
	/* Configure the RTC data register and RTC prescaler,配置RTC数据寄存器与RTC的分频值 */
	RTC_InitStructure.RTC_AsynchPrediv = 127;				//异步分频系数
	RTC_InitStructure.RTC_SynchPrediv = 249;				//同步分频系数
	RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;	//24小时格式
	RTC_Init(&RTC_InitStructure);

#endif

	/* Set the date: Sunday August 1th 2021 */
	RTC_DateStructure.RTC_Year = 0x21;
	RTC_DateStructure.RTC_Month = RTC_Month_August;
	RTC_DateStructure.RTC_Date = 0x01;
	RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Sunday;
	RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure);

	/* Set the time to 14h 59mn 55s PM */
	RTC_TimeStructure.RTC_H12     = RTC_H12_PM;
	RTC_TimeStructure.RTC_Hours   = 0x14;
	RTC_TimeStructure.RTC_Minutes = 0x59;
	RTC_TimeStructure.RTC_Seconds = 0x55; 
	RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);

	//关闭唤醒功能
	RTC_WakeUpCmd(DISABLE);
	
	//为唤醒功能选择RTC配置好的时钟源
	RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits);
	
	//设置唤醒计数值为自动重载,写入值默认是0
	RTC_SetWakeUpCounter(1-1);
	
	//清除RTC唤醒中断标志
	RTC_ClearITPendingBit(RTC_IT_WUT);
	
	//使能RTC唤醒中断
	RTC_ITConfig(RTC_IT_WUT, ENABLE);

	//使能唤醒功能
	RTC_WakeUpCmd(ENABLE);

	/* Configure EXTI Line22,配置外部中断控制线22 */
	EXTI_InitStructure.EXTI_Line = EXTI_Line22;			//当前使用外部中断控制线22
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;		//中断模式
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;		//上升沿触发中断 
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;			//使能外部中断控制线22
	EXTI_Init(&EXTI_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;		//允许RTC唤醒中断触发
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;	//抢占优先级为0x3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03;		//响应优先级为0x3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//使能
	NVIC_Init(&NVIC_InitStructure);
}

主函数代码

cs 复制代码
//做个标志位不然速度会太快
static volatile uint32_t g_rtc_wakeup_event=0;

int main(void)
{

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
	
	Led_init();
	key_init();
	
	/* 初始化串口1波特率为115200bps,若发送/接收数据有乱码,请检查PLL */
	usart1_init(9600);
	
	
	rtc_init();
	printf("this is rtc test\r\n");

	while(1)
	{
		//检测RTC唤醒中断是否触发
		if(g_rtc_wakeup_event)
		{
			//获取时间
			RTC_GetTime(RTC_Format_BCD,&RTC_TimeStructure);
			printf("Time %02X:%02X:%02X\r\n",RTC_TimeStructure.RTC_Hours,RTC_TimeStructure.RTC_Minutes,RTC_TimeStructure.RTC_Seconds);
			
			
			//获取日期
			RTC_GetDate(RTC_Format_BCD,&RTC_DateStructure);
			printf("Date 20%02X-%02X-%02X Week:%X\r\n",RTC_DateStructure.RTC_Year,RTC_DateStructure.RTC_Month,RTC_DateStructure.RTC_Date,RTC_DateStructure.RTC_WeekDay);
			
			
			g_rtc_wakeup_event=0;
		
		}
	}
}



void RTC_WKUP_IRQHandler(void)
{

	if(RTC_GetITStatus(RTC_IT_WUT) == SET)
	{
		printf("RTC_WKUP_IRQHandler\r\n");
		
		g_rtc_wakeup_event=1;
		
		RTC_ClearITPendingBit(RTC_IT_WUT);
		EXTI_ClearITPendingBit(EXTI_Line22);
	}
}

锁定时间不然复位键重置时间

cs 复制代码
static volatile uint32_t g_rtc_wakeup_event=0;

int main(void)
{

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
	
	led_init();
	key_init();
	
	/* 初始化串口1波特率为115200bps,若发送/接收数据有乱码,请检查PLL */
	usart1_init(115200);
	
	/* 若开机读取备份寄存器的值不等于0x1688,则进行rtc日期和时间的重置 */
	if(RTC_ReadBackupRegister(RTC_BKP_DR0)!=0x1688)
	{
		rtc_init();
		
		/* 对备份寄存器0写入数据0x1688,用于标记已经初始化过 */
		RTC_WriteBackupRegister(RTC_BKP_DR0,0x1688);
	}
	else
	{
	
		/* Enable the PWR clock ,使能电源控制时钟*/
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
		
		/* Allow access to RTC,允许访问备份电路 */
		PWR_BackupAccessCmd(ENABLE);


	#if 0
		/* 使能LSE*/
		RCC_LSEConfig(RCC_LSE_ON);
		
		/* 检查该LSE是否有效*/  
		while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);

		/* 选择LSE作为RTC的硬件时钟源*/
		RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
		

	#else  //若LSE无法工作,可用内部LSI
		/* 使能LSI*/
		RCC_LSICmd(ENABLE);
		
		/* 检查该LSI是否有效*/  
		while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);

		/* 选择LSI作为RTC的硬件时钟源*/
		RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);

	#endif

		/* Enable the RTC Clock,使能RTC时钟 */
		RCC_RTCCLKCmd(ENABLE);
		
		/* Wait for RTC APB registers synchronisation,等待RTC相关寄存器就绪 */
		RTC_WaitForSynchro();
		
	 #if 0 //LSE
		/* Configure the RTC data register and RTC prescaler,配置RTC数据寄存器与RTC的分频值 */
		RTC_InitStructure.RTC_AsynchPrediv = 127;				//异步分频系数
		RTC_InitStructure.RTC_SynchPrediv = 255;				//同步分频系数
		RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;	//24小时格式
		RTC_Init(&RTC_InitStructure);
	#else //LSI
		/* Configure the RTC data register and RTC prescaler,配置RTC数据寄存器与RTC的分频值 */
		RTC_InitStructure.RTC_AsynchPrediv = 127;				//异步分频系数
		RTC_InitStructure.RTC_SynchPrediv = 249;				//同步分频系数
		RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;	//24小时格式
		RTC_Init(&RTC_InitStructure);

	#endif


		//关闭唤醒功能
		RTC_WakeUpCmd(DISABLE);
		
		//为唤醒功能选择RTC配置好的时钟源
		RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits);
		
		//设置唤醒计数值为自动重载,写入值默认是0
		RTC_SetWakeUpCounter(1-1);
		
		//清除RTC唤醒中断标志
		RTC_ClearITPendingBit(RTC_IT_WUT);
		
		//使能RTC唤醒中断
		RTC_ITConfig(RTC_IT_WUT, ENABLE);

		//使能唤醒功能
		RTC_WakeUpCmd(ENABLE);

		/* Configure EXTI Line22,配置外部中断控制线22 */
		EXTI_InitStructure.EXTI_Line = EXTI_Line22;			//当前使用外部中断控制线22
		EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;		//中断模式
		EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;		//上升沿触发中断 
		EXTI_InitStructure.EXTI_LineCmd = ENABLE;			//使能外部中断控制线22
		EXTI_Init(&EXTI_InitStructure);
		
		NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;		//允许RTC唤醒中断触发
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;	//抢占优先级为0x3
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03;		//响应优先级为0x3
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//使能
		NVIC_Init(&NVIC_InitStructure);	
	}
	
	
	
	printf("this is rtc test\r\n");

	while(1)
	{
		//检测RTC唤醒中断是否触发
		if(g_rtc_wakeup_event)
		{
			//获取时间
			RTC_GetTime(RTC_Format_BCD,&RTC_TimeStructure);
			printf("Time %02X:%02X:%02X\r\n",RTC_TimeStructure.RTC_Hours,RTC_TimeStructure.RTC_Minutes,RTC_TimeStructure.RTC_Seconds);
			
			
			//获取日期
			RTC_GetDate(RTC_Format_BCD,&RTC_DateStructure);
			printf("Date 20%02X-%02X-%02X Week:%X\r\n",RTC_DateStructure.RTC_Year,RTC_DateStructure.RTC_Month,RTC_DateStructure.RTC_Date,RTC_DateStructure.RTC_WeekDay);
			
			
			g_rtc_wakeup_event=0;
		
		}
	}
}
相关推荐
代码游侠8 小时前
学习笔记——设备树基础
linux·运维·开发语言·单片机·算法
xuxg200510 小时前
4G 模组 AT 命令解析框架课程正式发布
stm32·嵌入式·at命令解析框架
CODECOLLECT11 小时前
京元 I62D Windows PDA 技术拆解:Windows 10 IoT 兼容 + 硬解码模块,如何降低工业软件迁移成本?
stm32·单片机·嵌入式硬件
BackCatK Chen12 小时前
STM32+FreeRTOS:嵌入式开发的黄金搭档,未来十年就靠它了!
stm32·单片机·嵌入式硬件·freertos·低功耗·rtdbs·工业控制
全栈游侠15 小时前
STM32F103XX 02-电源与备份寄存器
stm32·单片机·嵌入式硬件
深圳市九鼎创展科技17 小时前
瑞芯微 RK3399 开发板 X3399 评测:高性能 ARM 平台的多面手
linux·arm开发·人工智能·单片机·嵌入式硬件·边缘计算
辰哥单片机设计17 小时前
STM32项目分享:车辆防盗报警系统
stm32·单片机·嵌入式硬件
風清掦18 小时前
【江科大STM32学习笔记-05】EXTI外部中断11
笔记·stm32·学习
小龙报18 小时前
【51单片机】从 0 到 1 玩转 51 蜂鸣器:分清有源无源,轻松驱动它奏响新年旋律
c语言·数据结构·c++·stm32·单片机·嵌入式硬件·51单片机
范纹杉想快点毕业18 小时前
嵌入式与单片机开发核心学习指南——从思维转变到第一性原理的深度实践
单片机·嵌入式硬件