打印
[STM8]

加了一行代码程序不正常

[复制链接]
526|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
googcheng|  楼主 | 2019-11-13 17:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
static void RTC_Config(void)
{
    RTC_InitTypeDef RTC_InitStr;
        RTC_TimeTypeDef RTC_TimeStr;
        RTC_DateTypeDef RTC_DateStr;
   
  /* Configures the RTC */
  CLK_RTCClockConfig(CLK_RTCCLKSource_LSE, CLK_RTCCLKDiv_1);
  /* Wait for LSE clock to be ready */
  while(CLK_GetFlagStatus(CLK_FLAG_LSERDY) == RESET);
  /* wait for 1 second for the LSE Stabilisation */
  //LSE_StabTime();
  //delay_ms(1000);
  
  CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE);
  
  RTC_InitStr.RTC_HourFormat = RTC_HourFormat_24;
  RTC_InitStr.RTC_AsynchPrediv = 127;
  RTC_InitStr.RTC_SynchPrediv = 255;
  RTC_Init(&RTC_InitStr);
  
  RTC_TimeStructInit(&RTC_TimeStr);
  RTC_TimeStr.RTC_Hours = 00;
  RTC_TimeStr.RTC_Minutes = 00;
  RTC_TimeStr.RTC_Seconds = 00;
  RTC_SetTime(RTC_Format_BIN, &RTC_TimeStr);
  
  RTC_DateStructInit(&RTC_DateStr);
  //RTC_DateStr.RTC_WeekDay = RTC_Weekday_Tuesday;
  RTC_DateStr.RTC_Date = 2;
  RTC_DateStr.RTC_Month = RTC_Month_February;
  RTC_DateStr.RTC_Year = 19;
  RTC_SetDate(RTC_Format_BIN, &RTC_DateStr);
  
  RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits);
  RTC_ITConfig(RTC_IT_WUT, ENABLE);

  /* Enable Interrupts*/
  enableInterrupts();
}


这句
while(CLK_GetFlagStatus(CLK_FLAG_LSERDY) == RESET);

使用特权

评论回复
沙发
airwill| | 2019-11-13 21:43 | 只看该作者
会不会 LSE 的晶振就没有么?

使用特权

评论回复
板凳
googcheng|  楼主 | 2019-11-14 08:44 | 只看该作者
airwill 发表于 2019-11-13 21:43
会不会 LSE 的晶振就没有么?

lse 有的 波形正常。后来改为这个有好了, while( (CLK->ECKCR) & (uint8_t)(1<<3) == (uint8_t)RESET);

这个不知道怎么会影响i2c read

void sEE_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t* NumByteToRead)
{
  //__IO uint32_t timeout = 0xFFFF;
  __IO uint32_t timeout = 0xf00;
  /*!< Wait the end of last communication */
  for (;timeout > 0; timeout--);

  /* Set the pointer to the Number of data to be read. This pointer will be used
      by the DMA Transfer Complete interrupt Handler in order to reset the
      variable to 0. User should check on this variable in order to know if the
      DMA transfer has been completed or not. */
  sEEDataReadPointer = NumByteToRead;

  /*!< While the bus is busy */
  while (I2C_GetFlagStatus(sEE_I2C, I2C_FLAG_BUSY))
  {}

  /*!< Send START condition */
  I2C_GenerateSTART(sEE_I2C, ENABLE);

  /*!< Test on EV5 and clear it */
  while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_MODE_SELECT))
  {}

  /*!< Send EEPROM address for write */
  I2C_Send7bitAddress(sEE_I2C, (uint8_t)sEEAddress, I2C_Direction_Transmitter);

  /*!< Test on EV6 and clear it */
  while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
  {}

#ifdef sEE_M24C64_32

  /*!< Send the EEPROM's internal address to read from: MSB of the address first */
  I2C_SendData(sEE_I2C, (uint8_t)((ReadAddr & 0xFF00) >> 8));

  /*!< Test on EV8 and clear it */
  while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  {}

  /*!< Send the EEPROM's internal address to read from: LSB of the address */
  I2C_SendData(sEE_I2C, (uint8_t)(ReadAddr & 0x00FF));

#endif /*!< sEE_M24C64_32 */

  /*!< Test on EV8 and clear it */
  while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  {}

  /*!< Send STRAT condition a second time */
  I2C_GenerateSTART(sEE_I2C, ENABLE);

  /*!< Test on EV5 and clear it */
  while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_MODE_SELECT))
  {}

  /*!< Send EEPROM address for read */
  I2C_Send7bitAddress(sEE_I2C, (uint8_t)sEEAddress|0x01, I2C_Direction_Receiver);

  /*!< Test on EV6 and clear it */
  while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
  {}


  // cheng
  I2C_AcknowledgeConfig(sEE_I2C, ENABLE);
  while((uint16_t)(*NumByteToRead))  
  {
    if((uint16_t)(*NumByteToRead) == 1)
    {
      /* Disable Acknowledgement */
      //I2C_AcknowledgeConfig(I2C_ACK_NONE);
      I2C_AcknowledgeConfig(sEE_I2C, DISABLE);

    /*!< Send STOP Condition */
      I2C_GenerateSTOP(sEE_I2C, ENABLE);
      /* Send STOP Condition */
      //I2C_GenerateSTOP(ENABLE);
    }

    /* Test on EV7 and clear it */
    while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))
    {}

    /*!< Read a byte from the EEPROM */
    *pBuffer = I2C_ReceiveData(sEE_I2C);

      /* Point to the next location where the byte read will be saved */
    pBuffer++;
      
      /* Decrement the read bytes counter */
    (uint16_t)(*NumByteToRead)--;        
      
  }

  /* Enable Acknowledgement to be ready for another reception */
  I2C_AcknowledgeConfig(sEE_I2C, ENABLE);
  #if 0
  /* If number of data to be read is 1, then DMA couldn't be used */
  if ((uint16_t)(*NumByteToRead) < 2)
  {
    /*!< Disable Acknowledgement */
    I2C_AcknowledgeConfig(sEE_I2C, DISABLE);

    /*!< Send STOP Condition */
    I2C_GenerateSTOP(sEE_I2C, ENABLE);

    /*!< Test on EV7 and clear it */
    while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))
    {}

    /*!< Read a byte from the EEPROM */
    *pBuffer = I2C_ReceiveData(sEE_I2C);

    /*!< Decrement the read bytes counter */
    (uint16_t)(*NumByteToRead)--;

    /*!< Enable Acknowledgement to be ready for another reception */
    I2C_AcknowledgeConfig(sEE_I2C, ENABLE);
   
   
  }
  #endif
  
  #if 0
  /* DMA could be used for number of data higher than 1 */
  else
  {
      
   
    /* Configure the DMA Rx Channel with the buffer address and the buffer size */
    sEE_LowLevel_DMAConfig((uint16_t)pBuffer, (uint8_t)(*NumByteToRead), sEE_DIRECTION_RX);

    /* Inform the DMA that the next End Of Transfer Signal will be the last one */
    I2C_DMALastTransferCmd(sEE_I2C, ENABLE);

    /* Enable the DMA Rx Channel */
    DMA_Cmd(sEE_I2C_DMA_CHANNEL_RX, ENABLE);

    /* Global DMA Enable */
    DMA_GlobalCmd(ENABLE);
   
  }
  #endif
}

使用特权

评论回复
地板
airwill| | 2019-11-14 12:39 | 只看该作者
看样子是    while(CLK_GetFlagStatus(CLK_FLAG_LSERDY) == RESET);
的问题.
估计晶振未就绪,这  CLK_GetFlagStatus(CLK_FLAG_LSERDY) 返回值并非 RESET.
得查一下这个函数的说明呢

使用特权

评论回复
5
木木guainv| | 2019-12-9 16:52 | 只看该作者
看起来没有问题啊

使用特权

评论回复
6
晓伍| | 2019-12-9 16:55 | 只看该作者
帮楼主顶一下

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

32

主题

58

帖子

2

粉丝