int SHT30_I2C_Read_Singlesh(void)
{
uint16_t timer_over = 0;
uint8_t i = 0;
uint8_t sht30_I2CRxData[6]={0};//装要接受的数据的数据
uint8_t sht30_I2C_RxDataLen=6;//接受数据的长度
float temperature =0.0 ;
float humidity=0.0;
SHT30_I2CTxData[0]= ( 0xff00 & CMD_MEAS_POLLING_M) >> 8;
SHT30_I2CTxData[1]=0X00FF & CMD_MEAS_POLLING_M ;
SHT30_I2C_TxDataLen = 2;//数据长度
// printf("1\r\n");
// I2C_SoftwareResetCmd(ENABLE);
I2C_Init(I2C_Mode_I2C, 100000, I2C_DutyCycle_16_9, I2C_Ack_Enable, I2C_AckAddr_7bit, 0x11);
DelayMs(10);
while(I2C_GetFlagStatus(I2C_FLAG_BUSY));
// printf("2\r\n");
I2C_GenerateSTART(ENABLE);
// printf("3\r\n");
while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
DelayMs(10);
// printf("4\r\n");
I2C_Send7bitAddress(SHT30_Adderss, I2C_Direction_Transmitter);
DelayMs(10);
// printf("5\r\n");
while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
// printf("6\r\n");
while(i < 2)
{
if(I2C_GetFlagStatus(I2C_FLAG_TXE) != RESET)
{
I2C_SendData(SHT30_I2CTxData[i]);
i++;
}
}
// printf("7\r\n");
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(ENABLE);
// printf("8\r\n");
while(!I2C_GetFlagStatus(I2C_FLAG_BUSY));
// printf("9\r\n");
I2C_GenerateSTART(ENABLE);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
#if 1
DelayUs(100);
// printf("10\r\n");
I2C_Send7bitAddress(SHT30_Adderss, I2C_Direction_Receiver);
// printf("10\r\n");
while(!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
timer_over++;
DelayMs(1);
if(timer_over>=1000)
{
// I2C_SoftwareResetCmd(ENABLE);
SHT30_I2C_Softreset();
// I2C_Init(I2C_Mode_I2C, 100000, I2C_DutyCycle_16_9, I2C_Ack_Enable, I2C_AckAddr_7bit, 0x11);
return -1;
}
};
printf("11\r\n");
i=0;
while(i < 6)
{
// printf("->i =%d\r\n",i);
if(I2C_GetFlagStatus(I2C_FLAG_RXNE) != RESET)
{
sht30_I2CRxData[i]=I2C_ReceiveData();//
i++;
if(i==5)
{
// printf("->i =%d\r\n",i);
I2C_AcknowledgeConfig(DISABLE);//:清除 ACK 位
I2C_GenerateSTOP(ENABLE);//STOP 位置位
}
}
}
if(crc8(sht30_I2CRxData, 2) == sht30_I2CRxData[2])
{
temperature = -45.0 + (sht30_I2CRxData[1] | sht30_I2CRxData[0] << 8) * 175.0 / (0xFFFF - 1);
printf("-->%.2f \r\n",temperature);
// LOG_D("electric_Temp_hum_1: %d.%d \n", (int)electric_Temp_hum_1.Temperature, (int)(electric_Temp_hum_1.Temperature * 10) % 10);
}
if(crc8(sht30_I2CRxData + 3, 2) == sht30_I2CRxData[5])
{
humidity = (sht30_I2CRxData[4] | sht30_I2CRxData[3] << 8) * 0.0015259022;
printf("-->%.2f \r\n",humidity);
}
#endif
// while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// I2C_AcknowledgeConfig(DISABLE);//:清除 ACK 位
// I2C_GenerateSTOP(ENABLE);//STOP 位置位
// printf("-->%x %x %x %x %x %x \r\n",sht30_I2CRxData[0],sht30_I2CRxData[1],sht30_I2CRxData[2],sht30_I2CRxData[3],sht30_I2CRxData[4],sht30_I2CRxData[5]);
// while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// I2C_GenerateSTOP(ENABLE);
// I2C_SoftwareResetCmd(ENABLE);
}
第一次下载固件的时候,i2c正常读取数据,只要一断电重启,第一遍运行就卡在这里,第二遍运行就卡其他地方了