stm32之23.RTC闹钟

​
static volatile uint32_t g_rtc_wakeup_event=0;
static RTC_AlarmTypeDef  RTC_AlarmStructure;
	
void rtc_alarm_a_init(void)
{
	
	 NVIC_InitTypeDef  NVIC_InitStructure;
	 EXTI_InitTypeDef  EXTI_InitStructure;
	RTC_AlarmCmd(RTC_Alarm_A,DISABLE);
	
	RTC_AlarmStructure.RTC_AlarmTime.RTC_H12     = RTC_H12_PM;
	RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours   = 0x20;
	RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = 0x37;
	RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = 0x00;
#if 0
//闹钟时间修改
	//RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31;
	//RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
	RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay;	//屏蔽日期和星期,就是闹钟每天都生效
#endif
	
	
#if 0
	//指定1号生效
	RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x01;
	RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;//指定日期生效
	RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_None;	//不屏蔽日期和星期,开放日期和星期的指定
#endif
	
#if 1
	//指定星期日生效
	RTC_AlarmStructure.RTC_AlarmDateWeekDay = RTC_Weekday_Saturday;
	RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_WeekDay;//指定星期生效
	RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_None;	//不屏蔽日期和星期,开放日期和星期的指定
#endif	

	
	RTC_SetAlarm(RTC_Format_BCD,RTC_Alarm_A,&RTC_AlarmStructure);

	RTC_AlarmCmd(RTC_Alarm_A,ENABLE);
	
	/* Enable RTC Alarm A Interrupt,使能闹钟A的中断 */
	RTC_ITConfig(RTC_IT_ALRA, ENABLE);
	
	RTC_ClearFlag(RTC_FLAG_ALRAF);

	
	EXTI_ClearITPendingBit(EXTI_Line17);
	EXTI_InitStructure.EXTI_Line = EXTI_Line17;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);
	
	/* Enable the RTC Alarm Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
}

​

修改闹钟时间,必须与日期配对否之无效

主函数代码

int main(void)
{
	 RTC_DateTypeDef  RTC_DateStructure;
	 RTC_TimeTypeDef  RTC_TimeStructure;


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

	while(1)
	{
		//检测RTC唤醒中断是否触发
		if(g_rtc_wakeup_event)
		{
			//获取时间
			RTC_GetTime(RTC_Format_BCD,&RTC_TimeStructure);
			printf("Time %02X:%02X:%02Xrn",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:%Xrn",RTC_DateStructure.RTC_Year,RTC_DateStructure.RTC_Month,RTC_DateStructure.RTC_Date,RTC_DateStructure.RTC_WeekDay);
			
			
			g_rtc_wakeup_event=0;
		
		}
	}
}
void USART1_IRQHandler(void)
{
	uint8_t d;
	
	if(USART_GetITStatus(USART1,USART_IT_RXNE) == SET)
	{
		d = USART_ReceiveData(USART1);
		

		
		/* 告诉CPU,已经完成接收中断请求,可以响应新的接收中断请求 */
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);
		
	}

}

void RTC_WKUP_IRQHandler(void)
{

	if(RTC_GetITStatus(RTC_IT_WUT) == SET)
	{
		printf("RTC_WKUP_IRQHandlerrn");
		
		g_rtc_wakeup_event=1;
		
		RTC_ClearITPendingBit(RTC_IT_WUT);
		EXTI_ClearITPendingBit(EXTI_Line22);
	}
}

void RTC_Alarm_IRQHandler(void)
{

	if(RTC_GetITStatus(RTC_IT_ALRA) == SET)
	{
		printf("RTC_Alarm_IRQHandlerrn");
		

		RTC_ClearITPendingBit(RTC_IT_ALRA);
		EXTI_ClearITPendingBit(EXTI_Line17);
	}
}