当前位置: 首页 > news >正文

温湿度传感器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

相关文章:

  • SpringBoot 自动装配原理 自定义一个 starter
  • 【并发编程】Redisson 的分布式锁
  • 设计模式系列(1):总览与引导
  • 使用PHP对接印度股票市场API
  • 沐言智语开源Muyan-TTS模型,词错率、语音质量评分都处于开源模型的一线水平,推理速度相当快~
  • 【ns3】安装(包括无网安装)
  • Lua再学习
  • GTS-400 系列运动控制器板卡介绍(二十)---PT 动态FIFO
  • GitHub 趋势日报 (2025年05月10日)
  • 线程池使用ThreadLocal注意事项
  • docker安装superset实践
  • 极新携手火山引擎,共探AI时代生态共建的破局点与增长引擎
  • Linux511SSH连接 禁止root登录 服务任务解决方案 scp Vmware三种模式回顾
  • Kids A-Z安卓版:儿童英语启蒙的优质选择
  • 《异常链机制详解:如何优雅地传递Java中的错误信息?》
  • 嵌入式中屏幕的通信方式
  • LVGL(lv_label实战)
  • 2025御网杯wp(web,misc,crypto)
  • Python异常处理全解析:从基础到高级应用实战
  • 天授强化学习库了解
  • 媒体:“西北大学副校长范代娣成陕西首富”系乌龙,但她的人生如同开挂
  • 持续8年仍难终了的纠纷:败诉方因拒执罪被立案,胜诉方银行账户遭冻结
  • 外媒:初步结果显示,菲律宾前总统杜特尔特当选达沃市市长
  • 福建宁德市长张永宁拟任设区市党委正职,曾获评全国优秀县委书记
  • 江西吉水通报一男子拒服兵役:不得考公,两年内经商、升学等受限
  • 季后赛主场优势消失之谜,这事竟然要赖库里