STM32L151 RTC第一次正常唤醒,后续则不能。

2019-07-20 05:22发布




想利用 STM32L151 实现低功耗,采用RTC的wakeup的方式进行唤醒,当单片机进入待机状态后,10秒钟唤醒。每次重新下载程序或者断电重启后,都能间隔10秒唤醒,第二次进入低功耗后会立马唤醒。初步怀疑是写入计数初值不成功。在程序了采用RTC_WakeUpCmd(DISABLE)方式关闭计数后再写入计数初值,发现RTC_WakeUpCmd(DISABLE)返回错误,通过读取 RTC_GetWakeUpCounter()中的值也和写入的不一致。
代码如下,各位大神研究研究。


  /* Reset RTC Domain */
   RCC_RTCResetCmd(ENABLE);
   RCC_RTCResetCmd(DISABLE);
       
   RTC_ITConfig(RTC_IT_WUT, DISABLE);
if(0==RTC_WakeUpCmd(DISABLE))
{
    UART3_SendLR();
    UART3_SendString("RTC_WakeUpCmd(DISABLE)-> fail");

}
       
/* Enable the PWR clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

  /* Allow access to RTC */
  PWR_RTCAccessCmd(ENABLE);

  /* Enable the LSI OSC */
  RCC_LSICmd(ENABLE);

  /* Wait till LSI is ready */  
  while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);

  /* Select the RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);

  /* Enable the RTC Clock */
  RCC_RTCCLKCmd(ENABLE);

  /* Wait for RTC APB registers synchronisation */
  RTC_WaitForSynchro();

  /* Calendar Configuration */
  RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
  RTC_InitStructure.RTC_SynchPrediv        =  0x120; /* (37KHz / 128) - 1 = 0x120*/
  RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
        RTC_STATFACK=0;
if(RTC_STATFACK==RTC_Init(&RTC_InitStructure))
{
    UART3_SendLR();
    UART3_SendString("RTC_Init(&RTC_InitStructure)-> fail");

}         

  /* EXTI configuration *******************************************************/
  EXTI_ClearITPendingBit(EXTI_Line20);
  EXTI_InitStructure.EXTI_Line = EXTI_Line20;
  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 Wakeup Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
       
  ;
        RTC_STATFACK=0;
if(RTC_STATFACK==RTC_WakeUpCmd(DISABLE))
{
    UART3_SendLR();
    UART3_SendString("RTC_WakeUpCmd(DISABLE)-> fail");

}       
  /* Configure the RTC WakeUp Clock source: CK_SPRE (1Hz) */
  RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits);

  wakeupc=RTC_GetWakeUpCounter();
  UART3_SendLR();
  sprintf(stingl, "%d",wakeupc );
  UART3_SendString("RTC_GetWakeUpCounter() 1-> ");
  UART3_SendString(stingl);

  RTC_SetWakeUpCounter(10); //2S

  wakeupc=RTC_GetWakeUpCounter();
  UART3_SendLR();
  sprintf(stingl, "%d",wakeupc );

  UART3_SendString("RTC_GetWakeUpCounter() 2-> ");
  UART3_SendString(stingl);  

  /* Enable the RTC Wakeup Interrupt */
  RTC_ITConfig(RTC_IT_WUT, ENABLE);

  /* Enable Wakeup Counter */

        RTC_STATFACK=0;
if(RTC_STATFACK==RTC_WakeUpCmd(ENABLE))
{
    UART3_SendLR();
    UART3_SendString("RTC_WakeUpCmd(ENABLE)-> fail");

}
  RTC_ClearFlag(RTC_FLAG_WUTF);

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。