21ic问答首页 - STM32F103RE的闹钟中断问题求教
STM32F103RE的闹钟中断问题求教
NIKEjian2022-01-09
使用的是正点原子的例子,但是用的是LSI最为时钟源,但是一直复位,不懂为什么,有没有老哥解答一下的,main函数里面有一个打印,上电之后就是一直刷那个打印static void RTC_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; //RTCÈ«¾ÖÖжÏ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÏÈÕ¼ÓÅÏȼ¶1λ,´ÓÓÅÏȼ¶3λ
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //ÏÈÕ¼ÓÅÏȼ¶0λ,´ÓÓÅÏȼ¶4λ
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄܸÃͨµÀÖжÏ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝNVIC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèNVIC¼Ä´æÆ÷
NVIC_InitStructure.NVIC_IRQChannel =RTCAlarm_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;
NVIC_Init(&NVIC_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line17);
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
//ʵʱʱÖÓÅäÖÃ
//³õʼ»¯RTCʱÖÓ,ͬʱ¼ì²âʱÖÓÊÇ·ñ¹¤×÷Õý³£
//BKP->DR1ÓÃÓÚ±£´æÊÇ·ñµÚÒ»´ÎÅäÖõÄÉèÖÃ
//·µ»Ø0:Õý³£
//ÆäËû:´íÎó´úÂë
u8 RTC_Init(void)
{
//¼ì²éÊDz»ÊǵÚÒ»´ÎÅäÖÃʱÖÓ
u8 temp=0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //ʹÄÜPWRºÍBKPÍâÉèʱÖÓ
PWR_BackupAccessCmd(ENABLE); //ʹÄܺ󱸼ĴæÆ÷·ÃÎÊ
BKP_DeInit(); //¸´Î»±¸·ÝÇøÓò
RCC_LSICmd(ENABLE); //ÉèÖÃÍⲿµÍËÙ¾§Õñ(LSE),ʹÓÃÍâÉèµÍËÙ¾§Õñ
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) //¼ì²éÖ¸¶¨µÄRCC±ê־λÉèÖÃÓë·ñ,µÈ´ýµÍËÙ¾§Õñ¾ÍÐ÷
{
temp++;
delay_ms(10);
}
if(temp>=250)return 1;//³õʼ»¯Ê±ÖÓʧ°Ü,¾§ÕñÓÐÎÊÌâ
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); //ÉèÖÃRTCʱÖÓ(RTCCLK),Ñ¡ÔñLSE×÷ΪRTCʱÖÓ
RCC_RTCCLKCmd(ENABLE); //ʹÄÜRTCʱÖÓ
RTC_WaitForLastTask(); //µÈ´ý×î½üÒ»´Î¶ÔRTC¼Ä´æÆ÷µÄд²Ù×÷Íê³É
RTC_WaitForSynchro(); //µÈ´ýRTC¼Ä´æÆ÷ͬ²½
RTC_ITConfig(RTC_IT_SEC|RTC_IT_ALR, ENABLE); //ʹÄÜRTCÃëÖжϺÍÄÖÖÓÖжÏ
RTC_WaitForLastTask(); //µÈ´ý×î½üÒ»´Î¶ÔRTC¼Ä´æÆ÷µÄд²Ù×÷Íê³É
RTC_EnterConfigMode();/// ÔÊÐíÅäÖÃ
RTC_SetPrescaler(39999); //ÉèÖÃRTCÔ¤·ÖƵµÄÖµ
RTC_WaitForLastTask(); //µÈ´ý×î½üÒ»´Î¶ÔRTC¼Ä´æÆ÷µÄд²Ù×÷Íê³É
RTC_SetCounter(0); //??????0s
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
//????WORK_TIMES
RTC_SetAlarm(10);
RTC_WaitForLastTask();
RTC_WaitForSynchro();
RTC_ExitConfigMode(); //Í˳öÅäÖÃģʽ
RTC_NVIC_Config();//RCTÖжϷÖ×éÉèÖÃ
printf("Time is =%d \r\n",RTC_GetCounter()); //???????
return 0; //ok
}
//RTCʱÖÓÖжÏ
//ÿÃë´¥·¢Ò»´Î
extern int count;
void RTC_IRQHandler(void)
{
if(RTC_GetITStatus(RTC_IT_SEC)!=RESET)
{
printf("Time is =%d \r\n",RTC_GetCounter()); //???????
}
RTC_ClearITPendingBit(RTC_IT_OW|RTC_IT_SEC);
RTC_WaitForLastTask();
}
//ÄÖÖÓÖжÏ
void RTCAlarm_IRQHandler(void)
{
if(RTC_GetITStatus(RTC_IT_ALR) != RESET)
{
printf("THE ALARM READY =%d \r\n",RTC_GetCounter());//???????
RTC_ClearITPendingBit(RTC_IT_ALR);
PWR_BackupAccessCmd(ENABLE);
RTC_EnterConfigMode();
RTC_WaitForLastTask();
RTC_SetAlarm(10+RTC_GetCounter()); //???????40s?
RTC_WaitForLastTask();
RTC_ExitConfigMode();
PWR_BackupAccessCmd(DISABLE);
}
EXTI_ClearITPendingBit(EXTI_Line17);
RTC_WaitForLastTask();
RTC_ClearITPendingBit(RTC_IT_ALR);
RTC_WaitForLastTask();
}
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; //RTCÈ«¾ÖÖжÏ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÏÈÕ¼ÓÅÏȼ¶1λ,´ÓÓÅÏȼ¶3λ
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //ÏÈÕ¼ÓÅÏȼ¶0λ,´ÓÓÅÏȼ¶4λ
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄܸÃͨµÀÖжÏ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝNVIC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèNVIC¼Ä´æÆ÷
NVIC_InitStructure.NVIC_IRQChannel =RTCAlarm_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;
NVIC_Init(&NVIC_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line17);
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
//ʵʱʱÖÓÅäÖÃ
//³õʼ»¯RTCʱÖÓ,ͬʱ¼ì²âʱÖÓÊÇ·ñ¹¤×÷Õý³£
//BKP->DR1ÓÃÓÚ±£´æÊÇ·ñµÚÒ»´ÎÅäÖõÄÉèÖÃ
//·µ»Ø0:Õý³£
//ÆäËû:´íÎó´úÂë
u8 RTC_Init(void)
{
//¼ì²éÊDz»ÊǵÚÒ»´ÎÅäÖÃʱÖÓ
u8 temp=0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //ʹÄÜPWRºÍBKPÍâÉèʱÖÓ
PWR_BackupAccessCmd(ENABLE); //ʹÄܺ󱸼ĴæÆ÷·ÃÎÊ
BKP_DeInit(); //¸´Î»±¸·ÝÇøÓò
RCC_LSICmd(ENABLE); //ÉèÖÃÍⲿµÍËÙ¾§Õñ(LSE),ʹÓÃÍâÉèµÍËÙ¾§Õñ
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) //¼ì²éÖ¸¶¨µÄRCC±ê־λÉèÖÃÓë·ñ,µÈ´ýµÍËÙ¾§Õñ¾ÍÐ÷
{
temp++;
delay_ms(10);
}
if(temp>=250)return 1;//³õʼ»¯Ê±ÖÓʧ°Ü,¾§ÕñÓÐÎÊÌâ
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); //ÉèÖÃRTCʱÖÓ(RTCCLK),Ñ¡ÔñLSE×÷ΪRTCʱÖÓ
RCC_RTCCLKCmd(ENABLE); //ʹÄÜRTCʱÖÓ
RTC_WaitForLastTask(); //µÈ´ý×î½üÒ»´Î¶ÔRTC¼Ä´æÆ÷µÄд²Ù×÷Íê³É
RTC_WaitForSynchro(); //µÈ´ýRTC¼Ä´æÆ÷ͬ²½
RTC_ITConfig(RTC_IT_SEC|RTC_IT_ALR, ENABLE); //ʹÄÜRTCÃëÖжϺÍÄÖÖÓÖжÏ
RTC_WaitForLastTask(); //µÈ´ý×î½üÒ»´Î¶ÔRTC¼Ä´æÆ÷µÄд²Ù×÷Íê³É
RTC_EnterConfigMode();/// ÔÊÐíÅäÖÃ
RTC_SetPrescaler(39999); //ÉèÖÃRTCÔ¤·ÖƵµÄÖµ
RTC_WaitForLastTask(); //µÈ´ý×î½üÒ»´Î¶ÔRTC¼Ä´æÆ÷µÄд²Ù×÷Íê³É
RTC_SetCounter(0); //??????0s
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
//????WORK_TIMES
RTC_SetAlarm(10);
RTC_WaitForLastTask();
RTC_WaitForSynchro();
RTC_ExitConfigMode(); //Í˳öÅäÖÃģʽ
RTC_NVIC_Config();//RCTÖжϷÖ×éÉèÖÃ
printf("Time is =%d \r\n",RTC_GetCounter()); //???????
return 0; //ok
}
//RTCʱÖÓÖжÏ
//ÿÃë´¥·¢Ò»´Î
extern int count;
void RTC_IRQHandler(void)
{
if(RTC_GetITStatus(RTC_IT_SEC)!=RESET)
{
printf("Time is =%d \r\n",RTC_GetCounter()); //???????
}
RTC_ClearITPendingBit(RTC_IT_OW|RTC_IT_SEC);
RTC_WaitForLastTask();
}
//ÄÖÖÓÖжÏ
void RTCAlarm_IRQHandler(void)
{
if(RTC_GetITStatus(RTC_IT_ALR) != RESET)
{
printf("THE ALARM READY =%d \r\n",RTC_GetCounter());//???????
RTC_ClearITPendingBit(RTC_IT_ALR);
PWR_BackupAccessCmd(ENABLE);
RTC_EnterConfigMode();
RTC_WaitForLastTask();
RTC_SetAlarm(10+RTC_GetCounter()); //???????40s?
RTC_WaitForLastTask();
RTC_ExitConfigMode();
PWR_BackupAccessCmd(DISABLE);
}
EXTI_ClearITPendingBit(EXTI_Line17);
RTC_WaitForLastTask();
RTC_ClearITPendingBit(RTC_IT_ALR);
RTC_WaitForLastTask();
}
赞0
打印内容,好几个地方都打印同样内容,咋区分?
评论
2022-02-10
您需要登录后才可以回复 登录 | 注册