[STM8] 加了一行代码程序不正常

[复制链接]
664|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

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

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

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

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

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

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

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

  25. #ifdef sEE_M24C64_32

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

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

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

  33. #endif /*!< sEE_M24C64_32 */

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

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

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

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

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


  47.   // cheng
  48.   I2C_AcknowledgeConfig(sEE_I2C, ENABLE);
  49.   while((uint16_t)(*NumByteToRead))  
  50.   {
  51.     if((uint16_t)(*NumByteToRead) == 1)
  52.     {
  53.       /* Disable Acknowledgement */
  54.       //I2C_AcknowledgeConfig(I2C_ACK_NONE);
  55.       I2C_AcknowledgeConfig(sEE_I2C, DISABLE);

  56.     /*!< Send STOP Condition */
  57.       I2C_GenerateSTOP(sEE_I2C, ENABLE);
  58.       /* Send STOP Condition */
  59.       //I2C_GenerateSTOP(ENABLE);
  60.     }

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

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

  66.       /* Point to the next location where the byte read will be saved */
  67.     pBuffer++;
  68.       
  69.       /* Decrement the read bytes counter */
  70.     (uint16_t)(*NumByteToRead)--;        
  71.       
  72.   }

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

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

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

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

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

  90.     /*!< Enable Acknowledgement to be ready for another reception */
  91.     I2C_AcknowledgeConfig(sEE_I2C, ENABLE);
  92.    
  93.    
  94.   }
  95.   #endif
  96.   
  97.   #if 0
  98.   /* DMA could be used for number of data higher than 1 */
  99.   else
  100.   {
  101.       
  102.    
  103.     /* Configure the DMA Rx Channel with the buffer address and the buffer size */
  104.     sEE_LowLevel_DMAConfig((uint16_t)pBuffer, (uint8_t)(*NumByteToRead), sEE_DIRECTION_RX);

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

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

  109.     /* Global DMA Enable */
  110.     DMA_GlobalCmd(ENABLE);
  111.    
  112.   }
  113.   #endif
  114. }
airwill 发表于 2019-11-14 12:39 | 显示全部楼层
看样子是    while(CLK_GetFlagStatus(CLK_FLAG_LSERDY) == RESET);
的问题.
估计晶振未就绪,这  CLK_GetFlagStatus(CLK_FLAG_LSERDY) 返回值并非 RESET.
得查一下这个函数的说明呢
木木guainv 发表于 2019-12-9 16:52 | 显示全部楼层
看起来没有问题啊
晓伍 发表于 2019-12-9 16:55 | 显示全部楼层
帮楼主顶一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则

32

主题

58

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部