具体的编码实现如下:
/*SHT1X通讯复位*/
void ResetSHT1XCommunication(BusPinOperation*SetBusPin)
{
/*将data线设置为输出模式*/
SetDataPineDirection(Out);
Delayms(1);
SetBusPin[DataPin](Set);
SetBusPin[SckPin](Reset);
for(inti=0;i<9;i++)
{
SetBusPin[SckPin](Set);
Delayus(10);
SetBusPin[SckPin](Reset);
Delayus(10);
}
StartSHT1XOperation(SetBusPin);
}
同时也可以实现软件复位,状态寄存器复位为默认状态。在要发送下一个命令前,至少等待 11ms。具体实现如下:
/*对SHT1X实现软件复位*/
uint8_t Sht1xSoftwareReset(BusPinOperation*SetBusPin)
{
uint8_terr = 0;
ResetSHT1XCommunication(SetBusPin);
err=err+WriteByteT0Sht1x(SOFTWARE_RESET,SetBusPin);
return err;
}
(3)操作状态寄存器
SHT1x 的某些高级功能可以通过给状态寄存器发送指令来实现,如选择测量分辨率,电量不足提醒,使用 OTP 加载或启动加热功能等。
/*读状态寄存器*/
uint8_t ReadStatusRegister(uint8_t*pValue,uint8_t *pCheckSum,BusPinOperation *SetBusPin)
{
uint8_terr=0;
StartSHT1XOperation(SetBusPin);
err=WriteByteT0Sht1x(READ_STATUS_REGISTER,SetBusPin);
*pValue=ReadByteFromSht1x( Ack,SetBusPin);
*pCheckSum=ReadByteFromSht1x( noAck,SetBusPin);
return err;
}
/*写状态寄存器*/
uint8_t WriteStatusRegister(uint8_t*pValue,BusPinOperation *SetBusPin)
{
uint8_terr=0;
StartSHT1XOperation(SetBusPin);
err+=WriteByteT0Sht1x(WRITE_STATUS_REGISTER,SetBusPin);
err+=WriteByteT0Sht1x(*pValue,SetBusPin);
return err;
}
(4)温湿度的读取和计算
发布一组测量命令(‘00000101’表示相对湿度RH,‘00000011’表示温度T)后,控制器要等待测量结束。这个过程需要大约20/80/320ms,分别对应8/12/14bit 测量。根据本人测试8位时,15ms可以;12位时,70ms可以;14位时240ms可以。 SHT1x 通过下拉DATA 至低电平并进入空闲模式,表示测量的结束。控制器在再次触发SCK 时钟前,必须等待这个“数据备妥”信号来读出数据。检测数据可以先被存储,这样控制器可以继续执行其它任务在需要时再读出数据。
/*获取SHT1X的温度值*/
float GetSht1xTemperatureValue(floatvdd,BusPinOperation *SetBusPin)
{
floattempValue=0.0;
uint16_tsot=0;
uint8_terr=0;
uint8_thighByte=0;
uint8_tlowByte=0;
uint8_tcheckSum=0;
StartSHT1XOperation(SetBusPin);
WriteByteT0Sht1x(TEMP_MEAS_COMMAND,SetBusPin);
SetDataPineDirection(In);
Delayms(240);
if(ReadDataPinBit() == 1)
{
err +=1;
}
highByte=ReadByteFromSht1x(Ack,SetBusPin);
lowByte=ReadByteFromSht1x(Ack,SetBusPin);
checkSum=ReadByteFromSht1x(noAck,SetBusPin);
sot=(uint16_t)highByte;
sot=(sot*256)+(uint16_t)lowByte;
if(err !=0)
{
ResetSHT1XCommunication(SetBusPin);
}
else
{
tempValue=ConvertTemperatureData(sot,vdd);
}
return tempValue;
}
/*获取SHT1X的湿度值*/
float GetSht1xHumidityValue(floattemp,BusPinOperation *SetBusPin)
{
floathumiValue=0.0;
uint16_tsorh=0;
uint8_terr=0;
uint8_thighByte=0;
uint8_tlowByte=0;
uint8_tcheckSum=0;
StartSHT1XOperation(SetBusPin);
WriteByteT0Sht1x(HUMI_MEAS_COMMAND,SetBusPin);
SetDataPineDirection(In);
Delayms(70);
if(ReadDataPinBit() == 1)
{
err +=1;
}
highByte=ReadByteFromSht1x(Ack,SetBusPin);
lowByte=ReadByteFromSht1x(Ack,SetBusPin);
checkSum=ReadByteFromSht1x(noAck,SetBusPin);
sorh=(highByte<<8)|lowByte;
if(err !=0)
{
ResetSHT1XCommunication(SetBusPin);
}
else
{
humiValue=ConvertHumidityData(sorh,temp);
}
return humiValue;
}
对于其他的计算部分直接按公式便写即可。