温湿度传感器已经接到香蕉派上许久了,虽然wiringpi也通过了,但是编程能力有限,一直没有读出温湿度来
找到相关代码
if(!Mode) { printf("Raspi-SHT21 V3.0.0 by Martin Steppuhn (www.emsystech.de) [" __DATE__ " " __TIME__"]\n"); printf("Options:\n"); printf(" S : [20.0 99]\n"); printf(" L : [temperature=20.0][humidity=99]\n"); printf(" C : [Temperature,20,0][Humidity,99]\n"); printf("RaspberryHwRevision=%i\r\n",HwRev); } if(HwRev < 2) I2C_Open("/dev/i2c-0"); // Hardware Revision 1.0 else I2C_Open("/dev/i2c-1"); // Hardware Revision 2.0 I2C_Setup(I2C_SLAVE, 0x40);
uint8 SHT21_Read(int16 *temperature,uint8 *humidity) { uint32 val; uint8 buf[4]; Sht21Error = 0; //=== Softreset ================================================== I2C_Write1(0xFE); // softreset < 15ms DelayMs(50); //=== Temperature ================================================= I2C_Write1(0xF3); // start temperature measurement DelayMs(260); // Temperature@14Bit typ=66ms max=85ms I2C_Read(buf,3); // read temperature data if(buf[2] == CalcSht21Crc(buf,2)) // check CRC { val = buf[0]; val <<= 8; val += buf[1]; val &= 0xFFFC; // T = -46,85 + 175,72 * St/65535 da 1/10K --> * 10 // T = -468,5 + 1757,2 * St/65535 verinfachen // T = -468,5 + St / 37,2956.. damit Konstante ganzzahlig wird mit 2 erweitern // T = -937 + 2*St / 37,2956.. Bruch f黵 Division mit 256 erweitern // T = (-937 + (St * 512) / (37,2956.. * 256) ) / 2 // T = (((St * 512) / 9548) - 937) / 2 // val = (((val * 512) / 9548) - 937) / 2; *temperature = ((val * 512) / 9548); *temperature = ((*temperature) - 937) / 2; } else { Sht21Error |= ERROR_SHT21_CRC_TEMP; } //=== Humidity =================================================== I2C_Write1(0xF5); // start humidity measurement DelayMs(60); // RH@12Bit typ=22ms max=20ms I2C_Read(buf,3); // read humidity data if(buf[2] == CalcSht21Crc(buf,2)) { val = buf[0]; val <<= 8; val += buf[1]; val &= 0xFFFC; // T = -6 + 125* Srh/65535 // T = -6 + Srh / 524,28 // T = -6 + (Srh * 256) / 134215 | *256 wegen Numerik erweitern val = ((val * 256) / 134215) - 6; *humidity = val; } else { Sht21Error |= ERROR_SHT21_CRC_TEMP; } if(I2cError) Sht21Error |= ERROR_SHT21_I2C; return Sht21Error; }
相比较之下wiringpi写得比较抽象,怪不得没用好呢,慢慢习惯吧
union i2c_smbus_data { uint8_t byte ; uint16_t word ; uint8_t block [I2C_SMBUS_BLOCK_MAX + 2] ; // block [0] is used for length + one more for PEC } ; struct i2c_smbus_ioctl_data { char read_write ; uint8_t command ; int size ; union i2c_smbus_data *data ; } ; static inline int i2c_smbus_access (int fd, char rw, uint8_t command, int size, union i2c_smbus_data *data) { struct i2c_smbus_ioctl_data args ; args.read_write = rw ; args.command = command ; args.size = size ; args.data = data ; return ioctl (fd, I2C_SMBUS, &args) ; }
有奖活动 | |
---|---|
【EEPW电子工程师创研计划】技术变现通道已开启~ | |
发原创文章 【每月瓜分千元赏金 凭实力攒钱买好礼~】 | |
【EEPW在线】E起听工程师的声音! | |
“我踩过的那些坑”主题活动——第001期 | |
高校联络员开始招募啦!有惊喜!! | |
【工程师专属福利】每天30秒,积分轻松拿!EEPW宠粉打卡计划启动! | |
送您一块开发板,2025年“我要开发板活动”又开始了! | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
【我踩过的那些坑】STM32的硬件通讯调试过程的“坑”被打赏50分 | |
【我踩过的那些坑】晶振使用的问题被打赏100分 | |
【我踩过的那些坑】电感选型错误导致的处理器连接不上被打赏50分 | |
【我踩过的那些坑】工作那些年踩过的记忆深刻的坑被打赏10分 | |
【我踩过的那些坑】DRC使用位置错误导致的问题被打赏100分 | |
我踩过的那些坑之混合OTL功放与落地音箱被打赏50分 | |
汽车电子中巡航控制系统的使用被打赏10分 | |
【我踩过的那些坑】工作那些年踩过的记忆深刻的坑被打赏100分 | |
分享汽车电子中巡航控制系统知识被打赏10分 | |
分享安全气囊系统的检修注意事项被打赏10分 |