当前位置: 首页 > 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

http://www.xdnf.cn/news/388657.html

相关文章:

  • 【C++】AVL树实现
  • 害怕和别人发生冲突怎么办? --deepseek
  • [特殊字符] 免税商品优选购物商城系统 | Java + SpringBoot + Vue | 前后端分离实战项目分享
  • 线程的一些事(2)
  • 拍摄学习笔记【前期】(一)曝光
  • SQL 数据库监控:SQL语句监控工具与实践案例
  • 【Redis】Redis的主从复制
  • Linux常见指令解析(三)
  • jenkins built-in节点如何删除
  • TeledyneLeCroy在OFC2025 EA展台上展示了其400G/800G的全包围的测试解决方案,满足了UEC联盟和UALINK联盟的技术需求
  • 【25软考网工】第六章(3)数字签名和数字证书
  • opencv关键点检测
  • C语音学习---函数指针
  • Redis爆肝总结
  • 【嵌入式DIY实例-Arduino篇】-DIY遥控手柄
  • IC ATE集成电路测试学习——电流测试的原理和方法
  • 数据库与SQL核心技术解析:从基础到JDBC编程实战
  • 设计模式系列(1):总览与引导
  • NX989NY104美光科技芯片NY109NY113
  • LVGL(lv_btnmatrix矩阵按钮)
  • Babel 基础使用指南:从安装到编译的完整流程
  • MySQL主从复制
  • CacheBackEmbedding 组件的运行流程和使用注意事项
  • 使用 SHAP 进行特征交互检测:揭示变量之间的复杂依赖关系
  • 实战项目6(09)
  • 【计算机视觉】OpenCV实战项目:Text-Extraction-Table-Image:基于OpenCV与OCR的表格图像文本提取系统深度解析
  • Java泛型补充与理解
  • LangChain 使用指南与原理
  • ENSP-OSPF综合实验
  • 无人机空中物流优化:用 Python 打造高效配送模型