stm8 闹钟设定时死机

stm8 闹钟设定时死机,关闭看门狗就不死机。最后发现库函数有bug。

在库函数stm8l15x_rtc.c中加入下面红色代码。其实原理很简单,等待寄存器设置完成的时候,超时了。。

ErrorStatus RTC_AlarmCmd(FunctionalState NewState)
{
  __IO uint16_t alrawfcount = 0;
  ErrorStatus status = ERROR;
  uint8_t temp1 = 0;

  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  /* Disable the write protection for RTC registers */
  RTC_WriteProtectionCmd(DISABLE);

  /* Configure the Alarm state */
  if (NewState != DISABLE)
  { /*Enable the Alarm*/
    RTC->CR2 |= (uint8_t)(RTC_CR2_ALRAE);
    status = SUCCESS;
  }
  else
  {  /* Disable the Alarm */
    RTC->CR2 &= (uint8_t)~(RTC_CR2_ALRAE) ;

    /* Wait until ALRxWF flag is set */
    temp1 = (uint8_t)(RTC->ISR1 & RTC_ISR1_ALRAWF);
    while ((alrawfcount != ALRAWF_TIMEOUT) && (temp1 == RESET))
    {                    //ALRAWF_TIMEOUT     ((uint16_t)0xFFFF)
      temp1 = (uint8_t)(RTC->ISR1 & RTC_ISR1_ALRAWF);   //加入这一段。
      alrawfcount++;
    }

    if ((RTC->ISR1 &  RTC_ISR1_ALRAWF) == RESET)
    {
      status = ERROR;
    }
    else
    {
      status = SUCCESS;
    }
  }

  /* Enable the write protection for RTC registers */
  RTC_WriteProtectionCmd(ENABLE);

  /* Return the status*/
  return (ErrorStatus)status;
}

你可能感兴趣的:(闹钟设定,嵌入式)