iic copy
https://github.com/maomaojixiang/Circular_clock/blob/51f26fa5d602296c8937f06709566e54a3acf257/Circular_clock/CLOCK/sht30.c
https://github.com/search?q=sht30_data_process&type=code
https://blog.csdn.net/qq_34885669/article/details/89240092
https://bbs.huaweicloud.com/forum/forum/thread-34137-1-1.html 优质
https://blog.csdn.net/BearPi/article/details/104311539
https://www.extutorial.com/relative/2599341
https://blog.csdn.net/mculover666/article/details/100113106
https://blog.csdn.net/Mculover666/article/details/100132531
http://news.eeworld.com.cn/mcu/ic542648.html
https://www.mculover666.cn/posts/842429667/
https://zhuanlan.zhihu.com/p/80097994
https://www.shangmayuan.com/a/c51fb8c166224571ba06815e.html
static void delay_us(void)
{
uint32_t i;
for (i = 0; i < (45 * 5); i++)
{
__NOP();
}
}
void delay_n_us(uint32_t nus)
{
for(int i=0; i250)
{
I2C_Stop(hi2c);
return 1;
}
}
I2C_SCL_L(hi2c);
return 0;
}
/*static uint8_t I2C_Wait_Ack(I2C_HandleTypeDef *hi2c)
{
uint8_t ack;
I2C_SDA_H(hi2c);
delay_us();
I2C_SDA_IN(hi2c);
delay_us();
I2C_SCL_H(hi2c);
delay_us();
ack = I2C_SDA_GET(hi2c);
I2C_SCL_L(hi2c);
delay_us();
I2C_SDA_OUT(hi2c);
return ack;
}
static void I2C_Write_Byte(I2C_HandleTypeDef *hi2c, uint8_t data)
{
int i;
I2C_SDA_OUT(hi2c);
I2C_SCL_L(hi2c);
for (i = 0; i < 8; i ++)
{
I2C_SDA(hi2c)=(data&0x80)>>7;
data<<=1;
delay_us();
I2C_SCL_H(hi2c);
delay_n_us(2);
I2C_SCL_L(hi2c);
delay_us();
}
}*/
static void I2C_Write_Byte(I2C_HandleTypeDef *hi2c, uint8_t data)
{
int i;
I2C_SDA_OUT(hi2c);
I2C_SCL_L(hi2c);
for (i = 0; i < 8; i ++)
{
if (data & 0x80)
{
I2C_SDA_H(hi2c);
}
else
{
I2C_SDA_L(hi2c);
}
I2C_SCL_H(hi2c);
delay_n_us(2);
I2C_SCL_L(hi2c);
delay_us();
data <<= 1;
}
}
/*数据处理*/
int sht30_data_process(void)
{
uint8_t temporary[3];
uint16_t data;
uint8_t crc_result;
sht30_read_temp_humi(&hi2c,data_process.sht30_data_buffer);
temporary[0]=data_process.sht30_data_buffer[0];
temporary[1]=data_process.sht30_data_buffer[1];
temporary[2]=data_process.sht30_data_buffer[2];
//crc校验
crc_result=sht30_crc8_check(temporary,2,temporary[2]);
if(crc_result==0)
{
data=((uint16_t)temporary[0] << 8) | temporary[1];
//data_process.SHT30_temperature = (int)((175.0 * ((float)data) / 65535.0 - 45.0) *10.0);
if (save_temperature_unit==0)
{
data_process.SHT30_temperature = (int)(175.0 * ((float)data) / 65535.0 - 45.0);
save_temp = data_process.SHT30_temperature+save_temperature;
}
if (save_temperature_unit==1)
{
data_process.SHT30_temperature = (int)((175.0 * ((float)data) / 65535.0 - 45.0) * 1.8)+32.0;
save_temp = data_process.SHT30_temperature+(save_temperature*1.8);
}
Write_temperature(21503,save_temp);
//printf("温度 = %d save_temp = %d save_temperature = %d\n" , data_process.SHT30_temperature,save_temp,save_temperature);
}
else
{
return 1;
}
temporary[0]=data_process.sht30_data_buffer[3];
temporary[1]=data_process.sht30_data_buffer[4];
temporary[2]=data_process.sht30_data_buffer[5];
//crc校验
crc_result=sht30_crc8_check(temporary,2,temporary[2]);
if(crc_result==0)
{
data=((uint16_t)temporary[0] << 8) | temporary[1];
//data_process.SHT30_humidity = (int)((100.0 * (float)data / 65535.0) *10.0);
data_process.SHT30_humidity = (int)(100.0 * (float)data / 65535.0);
Write_temperature(21507,data_process.SHT30_humidity);
//printf("湿度 = %d\n", data_process.SHT30_humidity);
return 0;
}
else
{
return 2;
}
}
@卐词意不达?/*初始化SHT30*/
void SHT_Init(I2C_HandleTypeDef *hi2c)
{
HAL_Delay(250);
I2C_Start(hi2c);
I2C_Write_Byte(hi2c,0x88);
I2C_Wait_Ack(hi2c);
I2C_Write_Byte(hi2c,0x21);
I2C_Wait_Ack(hi2c);
I2C_Write_Byte(hi2c,0x30);
I2C_Wait_Ack(hi2c);
I2C_Stop(hi2c);
HAL_Delay(150);
}
/*读SHT30数据*/
void sht30_read_temp_humi(I2C_HandleTypeDef *hi2c,uint8_t *p)
{
I2C_Start(hi2c);
I2C_Write_Byte(hi2c,0x88);
I2C_Wait_Ack(hi2c);
I2C_Write_Byte(hi2c,0xe0);
I2C_Wait_Ack(hi2c);
I2C_Write_Byte(hi2c,0x00);
I2C_Wait_Ack(hi2c);
I2C_Start(hi2c);
I2C_Write_Byte(hi2c,0x89);
I2C_Wait_Ack(hi2c);
p[0] = I2C_Read_Byte(hi2c,1);
p[1] = I2C_Read_Byte(hi2c,1);
p[2] = I2C_Read_Byte(hi2c,1);
p[3] = I2C_Read_Byte(hi2c,1);
p[4] = I2C_Read_Byte(hi2c,1);
p[5] = I2C_Read_Byte(hi2c,0);
I2C_Stop(hi2c);
}
/*CRC算法*/
int crc8_compute(uint8_t *check_data, uint8_t num_of_data)
{
uint8_t bit;
uint8_t crc = 0xFF;
uint8_t byteCtr;
for(byteCtr = 0; byteCtr < num_of_data; byteCtr++) {
crc ^= (check_data[byteCtr]);
for(bit = 8; bit > 0; --bit) {
if(crc & 0x80) {
crc = (crc << 1) ^ 0x31;
} else {
crc = (crc << 1);
}
}
}
return crc;
}
int sht30_crc8_check(uint8_t *p,uint8_t num_of_data,uint8_t CrcData)
{
uint8_t crc;
crc = crc8_compute(p, num_of_data);// calculates 8-Bit checksum
if(crc != CrcData)
{
return 1;
}
return 0;
}
/*数据处理*/
int sht30_data_process(void)
{
uint8_t temporary[3];
uint16_t data;
uint8_t crc_result;
sht30_read_temp_humi(&hi2c,data_process.sht30_data_buffer);
temporary[0]=data_process.sht30_data_buffer[0];
temporary[1]=data_process.sht30_data_buffer[1];
temporary[2]=data_process.sht30_data_buffer[2];
//crc校验
crc_result=sht30_crc8_check(temporary,2,temporary[2]);
if(crc_result==0)
{
data=((uint16_t)temporary[0] << 8) | temporary[1];
//data_process.SHT30_temperature = (int)((175.0 * ((float)data) / 65535.0 - 45.0) *10.0);
if (