新手上路
- 积分
- 33
- 金钱
- 33
- 注册时间
- 2019-8-2
- 在线时间
- 8 小时
|
void SHT_Init(uint8_t msb,uint8_t lsb)
{
delay_ms(20);
IIC_Start();
IIC_Send_Byte(0x88+0);//¸ßμçÆ½0x8a
//IIC_Wait_Ack();
if(IIC_Wait_Ack())
{
}
IIC_Read_Byte(msb);
IIC_Wait_Ack();
IIC_Send_Byte(lsb);
IIC_Wait_Ack();
IIC_Stop();
delay_ms(2000);
}
void sht30_read_temp_humi(u8 *p)
{
SHT_Init(0x21,0x30);
SHT_Init(0xe0,0x00);
IIC_Start();
IIC_Send_Byte(0x89);
IIC_Wait_Ack();
//delay_ms(100);
//while (IIC_Wait_Ack());
//delay_ms(1000);
IIC_SDA=1;
p[0] = IIC_Read_Byte(1);//ζè¸ß°Ëλ
p[1] = IIC_Read_Byte(1);//ζèμí°Ëλ
p[2] = IIC_Read_Byte(1);
p[3] = IIC_Read_Byte(1);
p[4] = IIC_Read_Byte(1);
p[5] = IIC_Read_Byte(0);
IIC_Stop();
delay_ms(10);
}
int sht30_crc8_check(u8 *p,u8 num_of_data,u8 CrcData)
{
u8 crc;
crc = crc8_compute(p, num_of_data);// calculates 8-Bit checksum
if(crc != CrcData)
{
return 1;
}
return 0;
}
int sht30_data_process(void)
{
u8 temporary[3];
u16 data;
u8 crc_result;
sht30_read_temp_humi(sht30_data_buffer);
//??????????,????????
temporary[0]=sht30_data_buffer[0];
temporary[1]=sht30_data_buffer[1];
temporary[2]=sht30_data_buffer[2];
crc_result=sht30_crc8_check(temporary,2,temporary[2]);
if(crc_result==0)
{
//?2?8????????16????
data=((u16)temporary[0] << 8) | temporary[1];
//????,?16????????10???????,?????????,data_process.SHT30_temperature????????,?????????????.???????????c???????????
SHT30_temperature = (int)((175.0 * ((float)data) / 65535.0 - 45.0) *10.0);
}
else
{
return 1;
}
return 0;
}
int crc8_compute(u8 *check_data, u8 num_of_data)
{
uint8_t bit; // bit mask
uint8_t crc = 0xFF; // calculated checksum
uint8_t byteCtr; // byte counter
// calculates 8-Bit checksum with given polynomial
for(byteCtr = 0; byteCtr < num_of_data; byteCtr++) {
crc ^= (check_data[byteCtr]);
//crc??,????1?^0x31
for(bit = 8; bit > 0; --bit) {
if(crc & 0x80) {
crc = (crc << 1) ^ 0x31;
} else {
crc = (crc << 1);
}
}
}
return crc;
}
|
|