初中生
最后登录1970-1-1
在线时间 小时
注册时间2017-9-22
|
楼主 |
发表于 2018-1-8 17:17:43
|
显示全部楼层
#include "bsp_exti.h"
#include "bsp_led.h"
#include "bsp_usart1.h"
/**
* @brief ÅäÖÃǶì×Ïòá¿ÖD¶Ï¿ØÖÆÆ÷NVIC
* @param ÎT
* @retval ÎT
*/
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* ÅäÖÃ[A|B|C|D|E]13ÎaÖD¶ÏÔ′ */
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; //RTC諾ÖÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÏèÕ¼óÅÏ輶1λ,′óóÅÏ輶3λ
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //ÏèÕ¼óÅÏ輶0λ,′óóÅÏ輶4λ
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1Äü¸Ãí¨μàÖD¶Ï
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;//RTCÄÖÖóÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief ÅäÖà PC13 ÎaÏßÖD¶Ï¿ú£¬2¢éèÖÃÖD¶ÏóÅÏ輶
* @param ÎT
* @retval ÎT
*/
void EXTI_Key_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
/* config the extiline(PC13) clock and AFIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC |RCC_APB2Periph_GPIOA| RCC_APB2Periph_AFIO,ENABLE);
/* config the NVIC(PC13) */
NVIC_Configuration();
/* EXTI line gpio config(PC13) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // éÏà-êäèë
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* EXTI line(PC13) mode config */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource13);
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //ϽμÑØÖD¶Ï
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//ía2¿ÖD¶ÏÏß17
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);
}
/*
* oˉêyÃû£oRTC_Configuration
* Ãèêö £oÅäÖÃRTC
* êäèë £oÎT
* êä3ö £oÎT
* μ÷óÃ £oía2¿μ÷óÃ
*/
void RTC_Configuration(void)
{
/* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Reset Backup Domain */
BKP_DeInit();
/* Enable LSE */
RCC_LSEConfig(RCC_LSE_ON);
/* Wait till LSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{}
/* Select LSE as RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
/* Enable RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC registers synchronization
* òòÎaRTCê±ÖóêÇμíËùμÄ£¬Äú»·ê±ÖóêǸßËùμÄ£¬ËùòÔòaí¬2½
*/
RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Enable the RTC Second */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) = 1HZ */
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
/*
* oˉêyÃû£oRTC_CheckAndConfig
* Ãèêö £o¼ì2é2¢ÅäÖÃRTC
* êäèë £oóÃóú¶áè¡RTCê±¼äμĽá11ìåÖ¸Õë
* êä3ö £oÎT
* μ÷óÃ £oía2¿μ÷óÃ
*/
void RTC_CheckAndConfig(void)
{
/*ÔúÆô¶ˉê±¼ì2鱸·Y¼Ä′æÆ÷BKP_DR1£¬èç1ûÄúèY2»êÇ0xA5A5,
ÔòDèÖØDÂÅäÖÃê±¼ä2¢ÑˉÎêóû§μ÷Õûê±¼ä*/
if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{
//printf("\r\n\r\n RTC not yet configured....");
/* RTC Configuration */
RTC_Configuration();
//printf("\r\n\r\n RTC configured....");
/* ½«¼ÆêyÖμD′èëRTC¼ÆêyÆ÷ */
RTC_SetCounter(0); //éèÖÃRTC¼ÆêyÆ÷μÄÖμ
RTC_WaitForLastTask(); //μè′y×î½üò»′ζÔRTC¼Ä′æÆ÷μÄD′2ù×÷íê3é
//éèÖÃÄÖÖóê±¼ä
RTC_SET_ALARM(10);
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
}
else
{
/*Æô¶ˉÎTDèéèÖÃDÂê±Öó*/
/*¼ì2éêÇ·ñμôμçÖØÆô*/
// if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
// {
// printf("\r\n\r\n Power On Reset occurred....");
// }
// /*¼ì2éêÇ·ñReset¸′λ*/
// else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
// {
// printf("\r\n\r\n External Reset occurred....");
// }
//
// printf("\r\n No need to configure RTC....");
/*μè′y¼Ä′æÆ÷í¬2½*/
RTC_WaitForSynchro();
/*ÔêDíRTCÃëÖD¶Ï*/
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/*μè′yéÏ′ÎRTC¼Ä′æÆ÷D′2ù×÷íê3é*/
RTC_WaitForLastTask();
}
/* Clear reset flags */
RCC_ClearFlag();
}
//éèÖÃÄÖÖó
void RTC_SET_ALARM(u32 sec)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //ê1ÄüPWRoíBKPíaéèê±Öó
PWR_BackupAccessCmd(ENABLE); //ê1Äüo󱸼Ä′æÆ÷·ÃÎê
// RTC_SetAlarm(10);
//DEBUG_COM_STREAM("-??-",NULL);
RTC_SetAlarm(RTC_GetCounter()+sec);
//DEBUG_COM_STREAM("-??1-",NULL);
RTC_WaitForLastTask();
//DEBUG_COM_STREAM("-??2-",NULL);
RTC_ITConfig(RTC_FLAG_ALR,ENABLE);
}
中断服务程序:
//RTCÃëÖD¶Ïoˉêy
void RTC_IRQHandler(void)
{
if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
{
/* Clear the RTC Second interrupt */
RTC_ClearITPendingBit(RTC_IT_SEC);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
}
//RTCÄÖÖóÖD¶Ïoˉêy
void RTCAlarm_IRQHandler(void)
{
if(RTC_GetITStatus(RTC_IT_ALR)!= RESET)//ÄÖÖóÖD¶Ï
{
SYSCLKConfig_STOP(); //ÖØDÂÅäÖÃËøÏà»·ê±Öó
//éèÖÃê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, DISABLE); //ê1ÄüPWRoíBKPíaéèê±Öó
RTC_SetCounter(0); //ÖØDÂéèÖÃRTC¼ÆêyÆ÷μÄÖμ
RTC_WaitForLastTask(); //μè′y×î½üò»′ζÔRTC¼Ä′æÆ÷μÄD′2ù×÷íê3é
RTC_SET_ALARM(10); //éèÖÃÄÖÖóê±¼ä
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
RTC_ClearITPendingBit(RTC_IT_ALR); //ÇåÄÖÖóÖD¶Ï
EXTI_ClearITPendingBit(EXTI_Line17);
RTC_WaitForLastTask();
}
}
|
|