温湿度传感器SHT4X
软件IIC:软件I2C master2-CSDN博客
SoftI2C_Port:SoftI2C_Port-CSDN博客
SHT4X.h
#ifndef SHT4X_h
#define SHT4X_h
#include"SoftI2CMaster.h"
extern unsigned char SHT4X_Read0xFD(SoftI2CMasterWireObj obj,unsigned short int *T_Data,unsigned short int *RH_Data);
extern unsigned char SHT4X_Read0x89(SoftI2CMasterWireObj obj,unsigned long *serialNum);
#endif
SHT4X.c
#include"SHT4X.h"
#define CRC8_POLYNOMIAL 0x31
#define CRC8_INIT 0xFF
static unsigned char sensirion_common_generate_crc(const unsigned char* data, unsigned short int count)
{unsigned short int current_byte;unsigned char crc = CRC8_INIT;unsigned char crc_bit;/* calculates 8-Bit checksum with given polynomial */for (current_byte = 0; current_byte < count; ++current_byte) {crc ^= (data[current_byte]);for (crc_bit = 8; crc_bit > 0; --crc_bit) {if (crc & 0x80){crc = (crc << 1) ^ CRC8_POLYNOMIAL;}else{crc = (crc << 1);}}}return crc;
}
unsigned char SHT4X_Read0xFD(SoftI2CMasterWireObj obj,unsigned short int *T_Data,unsigned short int *RH_Data)
{unsigned char rtn=0;unsigned char temp[3];unsigned char crc8;SoftI2CMaster_Start(obj);SoftI2CMaster_LoadOutByte(obj,(0x44<<1));while(SoftI2CMaster_WaitAck(obj)==1);SoftI2CMaster_LoadOutByte(obj,0xFD);while(SoftI2CMaster_WaitAck(obj)==1);SoftI2CMaster_Stop(obj);{volatile int i=1500000;while(i--);}SoftI2CMaster_Start(obj);SoftI2CMaster_LoadOutByte(obj,(0x44<<1)|1);while(SoftI2CMaster_WaitAck(obj)==1);temp[0]=SoftI2CMaster_WaitData(obj);SoftI2CMaster_LoadOutACK(obj,0);temp[1]=SoftI2CMaster_WaitData(obj);SoftI2CMaster_LoadOutACK(obj,0);temp[2]=SoftI2CMaster_WaitData(obj);SoftI2CMaster_LoadOutACK(obj,0);crc8=sensirion_common_generate_crc(temp,2);if(crc8==temp[2]){*T_Data=temp[0]<<8|temp[1];rtn|=1;//printf("\r\n->%.2X %.2X %.2X crc8:%.2X",temp[0],temp[1],temp[2],crc8);}temp[0]=SoftI2CMaster_WaitData(obj);SoftI2CMaster_LoadOutACK(obj,0);temp[1]=SoftI2CMaster_WaitData(obj);SoftI2CMaster_LoadOutACK(obj,0);temp[2]=SoftI2CMaster_WaitData(obj);SoftI2CMaster_LoadOutACK(obj,1);crc8=sensirion_common_generate_crc(temp,2);if(crc8==temp[2]){*RH_Data=temp[0]<<8|temp[1];rtn|=2;//printf("\r\n->%.2X %.2X %.2X crc8:%.2X",temp[0],temp[1],temp[2],crc8);}SoftI2CMaster_Stop(obj);return rtn;
}
unsigned char SHT4X_Read0x89(SoftI2CMasterWireObj obj,unsigned long *serialNum)
{unsigned long num;unsigned char rtn=0;unsigned char temp[3];unsigned char crc8;SoftI2CMaster_Start(obj);SoftI2CMaster_LoadOutByte(obj,(0x44<<1));while(SoftI2CMaster_WaitAck(obj)==1);SoftI2CMaster_LoadOutByte(obj,0x89);while(SoftI2CMaster_WaitAck(obj)==1);SoftI2CMaster_Stop(obj);{volatile int i=1500000;while(i--);}SoftI2CMaster_Start(obj);SoftI2CMaster_LoadOutByte(obj,(0x44<<1)|1);while(SoftI2CMaster_WaitAck(obj)==1);temp[0]=SoftI2CMaster_WaitData(obj);SoftI2CMaster_LoadOutACK(obj,0);temp[1]=SoftI2CMaster_WaitData(obj);SoftI2CMaster_LoadOutACK(obj,0);temp[2]=SoftI2CMaster_WaitData(obj);SoftI2CMaster_LoadOutACK(obj,0);crc8=sensirion_common_generate_crc(temp,2);if(crc8==temp[2]){num=temp[0]<<8|temp[1];rtn|=1;//printf("\r\n->%.2X %.2X %.2X crc8:%.2X",temp[0],temp[1],temp[2],crc8);}temp[0]=SoftI2CMaster_WaitData(obj);SoftI2CMaster_LoadOutACK(obj,0);temp[1]=SoftI2CMaster_WaitData(obj);SoftI2CMaster_LoadOutACK(obj,0);temp[2]=SoftI2CMaster_WaitData(obj);SoftI2CMaster_LoadOutACK(obj,1);crc8=sensirion_common_generate_crc(temp,2);if(crc8==temp[2]){num<<=16;num|=temp[0]<<8|temp[1];rtn|=2;//printf("\r\n->%.2X %.2X %.2X crc8:%.2X",temp[0],temp[1],temp[2],crc8);}SoftI2CMaster_Stop(obj);*serialNum=num;return rtn;
}
#ifdef XC
#ifdef Debug
#include"XCOSnTh.h"
#include"I2CPort.h"
static int TestCmd(CmdObj obj,char *str,int len)
{unsigned char data;unsigned short int t_data;unsigned short int rh_data;data=SHT4X_Read0xFD(&I2CPort001,&t_data,&rh_data);/*** formulas for conversion of the sensor signals, optimized for fixed point* algebra:* Temperature = 175 * S_T / 65535 - 45* Relative Humidity = 125 * (S_RH / 65535) - 6*/if(data&1){{long temperature = ((21875 * (long)t_data) >> 13) - 45000;obj->printf("\r\nt_data:0x%.X %ld",t_data,temperature);}{double temperature =t_data;temperature=175.0*temperature/65535.0-45.0;obj->printf("\r\nt_data:0x%.X %f",t_data,temperature);}}if(data&2){{long humidity = ((15625 * (long)rh_data) >> 13) - 6000;obj->printf("\r\nrh_data:0x%.X %ld",rh_data,humidity);}{double humidity =rh_data;humidity=125.0*(humidity/65535.0)-6.0;obj->printf("\r\nrh_data:0x%.X %f",rh_data,humidity);}}return 1;
}
CmdDef(SHT4X,0,TestCmd,"");
static int SerialNumCMD(CmdObj obj,char *str,int len)
{unsigned long num=0;if(SHT4X_Read0x89(&I2CPort001,&num)==0x03){obj->printf("\r\n serial num:%.8X",num);}else{obj->printf("\r\n serial num:error");}return 1;
}
CmdDef(SHT4X_serial,0,SerialNumCMD,"");
#endif
#endif