RTL8710读温湿度传感器SHT21
来源:互联网 发布:java jdk 64位 编辑:程序博客网 时间:2024/04/29 11:16
连接RTL8710与SHT21模块
- I2C1 SDA (PC_4) to extended board's SDA- I2C1 SCL (PC_5) to extended board's SCL
程序
直接复制到SDK里面工程中,替换
#include "device.h"#include "PinNames.h"#include "basic_types.h"#include "diag.h" #include "osdep_api.h"#include "i2c_api.h"#include "pinmap.h"#include "rtl_lib.h"#include <stdbool.h>#define NO_ERROR 0x00#define ACK_ERROR 0x01#define CHECKSUM_ERROR 0x02#define NULL_ERROR 0x03#define MBED_I2C_MTR_SDA PC_4#define MBED_I2C_MTR_SCL PC_5#define MBED_I2C_SLAVE_ADDR0 0x40#define POLYNOMIAL 0x131 // P(x) = x^8 + x^5 + x^4 + 1 = 100110001#define MBED_I2C_BUS_CLK 100000 //hz#define I2C_DATA_MAX_LENGTH 16uint8_t i2cdata_write[I2C_DATA_MAX_LENGTH];uint8_t i2cdata_read[I2C_DATA_MAX_LENGTH];int i2cdata_read_pos;volatile i2c_t i2cmaster;// Sensor Commandstypedef enum{ TRIG_T_MEASUREMENT_HM = 0xE3, // command trig. temp meas. hold master TRIG_RH_MEASUREMENT_HM = 0xE5, // command trig. humidity meas. hold master TRIG_T_MEASUREMENT_POLL = 0xF3, // command trig. temp meas. no hold master TRIG_RH_MEASUREMENT_POLL = 0xF5, // command trig. humidity meas. no hold master USER_REG_W = 0xE6, // command writing user register USER_REG_R = 0xE7, // command reading user register SOFT_RESET = 0xFE, // command soft reset SerialNumber_location_1 = 0xFA0F, SerialNumber_location_2 = 0xFCC9}etSHT2xCommand;typedef enum { SHT2x_RES_12_14BIT = 0x00, // RH=12bit, T=14bit SHT2x_RES_8_12BIT = 0x01, // RH= 8bit, T=12bit SHT2x_RES_10_13BIT = 0x80, // RH=10bit, T=13bit SHT2x_RES_11_11BIT = 0x81, // RH=11bit, T=11bit SHT2x_RES_MASK = 0x81 // Mask for res. bits (7,0) in user reg.} etSHT2xResolution;typedef enum { SHT2x_EOB_ON = 0x40, // end of battery SHT2x_EOB_MASK = 0x40, // Mask for EOB bit(6) in user reg.} etSHT2xEob;static int SHT2x_GetSerialNumber(uint8_t u8SerialNumber[]);static int SHT2x_WriteCommand8(uint8_t cmd);static int SHT2x_WriteCommand16(uint16_t cmd);static int SHT2x_CheckCrc(uint8_t data[], uint8_t nbrOfBytes, uint8_t checksum);static float SHT2x_CalcTemperature(uint16_t rawValue);static float SHT2x_CalcHumidity(uint16_t rawValue);int SHT2x_ReadUserRegister(uint8_t *pRegisterValue);int SHT2x_WriteUserRegister(uint8_t *pRegisterValue);int SHT2x_Init(uint8_t u8SerialNumber[]){ uint8_t userRegister; //variable for user register int error = NO_ERROR; DiagPrintf("SHT2x_Init \r\n"); i2c_init((i2c_t*)&i2cmaster, MBED_I2C_MTR_SDA ,MBED_I2C_MTR_SCL); i2c_frequency((i2c_t*)&i2cmaster,MBED_I2C_BUS_CLK); if (u8SerialNumber == NULL ) return NULL_ERROR; // --- Reset sensor by command --- error |= SHT2x_SoftReset(); // --- Read the sensors serial number (64bit) --- error |= SHT2x_GetSerialNumber(u8SerialNumber); // --- Set Resolution e.g. RH 10bit, Temp 13bit --- error |= SHT2x_ReadUserRegister(&userRegister); //get actual user reg userRegister = (userRegister & ~SHT2x_RES_MASK) | SHT2x_RES_10_13BIT; error |= SHT2x_WriteUserRegister(&userRegister); //write changed user reg return error;}static int SHT2x_GetSerialNumber(uint8_t u8SerialNumber[]){ int error = NO_ERROR; SHT2x_WriteCommand16(SerialNumber_location_1); if (i2c_read((i2c_t*)&i2cmaster, MBED_I2C_SLAVE_ADDR0, (char*)&i2cdata_read[0], 8, 1) != 8) error |= NULL_ERROR; u8SerialNumber[5] = i2cdata_read[0];//SNB_3 u8SerialNumber[4] = i2cdata_read[2];//SNB_2 u8SerialNumber[3] = i2cdata_read[4];//SNB_1 u8SerialNumber[2] = i2cdata_read[6];//SNB_0 SHT2x_WriteCommand16(SerialNumber_location_2); if (i2c_read((i2c_t*)&i2cmaster, MBED_I2C_SLAVE_ADDR0, (char*)&i2cdata_read[0], 6, 1) != 6) error |= NULL_ERROR; u8SerialNumber[1] = i2cdata_read[0];//SNC_1 u8SerialNumber[0] = i2cdata_read[1];//SNC_0 u8SerialNumber[7] = i2cdata_read[3];//SNA_1 u8SerialNumber[6] = i2cdata_read[4];//SNA_0 return error;}static int SHT2x_CheckCrc(uint8_t data[], uint8_t nbrOfBytes, uint8_t checksum){ uint8_t crc = 0; uint8_t byteCtr; int error = NO_ERROR; //calculates 8-Bit checksum with given polynomial for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr) { crc ^= (data[byteCtr]); for (uint8_t bit = 8; bit > 0; --bit) { if (crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL; else crc = (crc << 1); } } if (crc != checksum) error |= CHECKSUM_ERROR; return error;}//===========================================================================int SHT2x_SoftReset()//==========================================================================={ int error = NO_ERROR; SHT2x_WriteCommand8(SOFT_RESET); Mdelay(15); // wait till sensor has restarted return error;}//===========================================================================int SHT2x_ReadUserRegister(uint8_t *pRegisterValue)//==========================================================================={ int error = NO_ERROR; SHT2x_WriteCommand8(USER_REG_R); if (i2c_read((i2c_t*)&i2cmaster, MBED_I2C_SLAVE_ADDR0, (char*)&i2cdata_read[0], 2, 1) != 2) error |= NULL_ERROR; error |= SHT2x_CheckCrc (i2cdata_read,1,i2cdata_read[1]); return error;}//===========================================================================int SHT2x_WriteUserRegister(uint8_t *pRegisterValue)//==========================================================================={ int error = NO_ERROR; SHT2x_WriteCommand8(USER_REG_W); if (i2c_write((i2c_t*)&i2cmaster, MBED_I2C_SLAVE_ADDR0, (char*)pRegisterValue, 1, 1) != 1) error |= NULL_ERROR; return error;}static int SHT2x_WriteCommand8(uint8_t cmd){ int error = NO_ERROR; i2cdata_write[0] = (uint8_t)(cmd&0xFF); if (i2c_write((i2c_t*)&i2cmaster, MBED_I2C_SLAVE_ADDR0, &i2cdata_write[0], 1, 0) != 1) error |= NULL_ERROR; return error;}static int SHT2x_WriteCommand16(uint16_t cmd){ int error = NO_ERROR; i2cdata_write[0] = (uint8_t)(cmd >>8);i2cdata_write[1] = (uint8_t)(cmd&0xFF); if (i2c_write((i2c_t*)&i2cmaster, MBED_I2C_SLAVE_ADDR0, &i2cdata_write[0], 2, 0) != 2) error |= NULL_ERROR; return error;}static float SHT2x_CalcTemperature(uint16_t rawValue){ rawValue &= ~0x0003; // clear bits [1..0] (status bits) return -46.85 + 175.72/65536 *(float)rawValue; //T= -46.85 + 175.72 * ST/2^16}static float SHT2x_CalcHumidity(uint16_t rawValue){ rawValue &= ~0x0003; // clear bits [1..0] (status bits) return -6.0 + 125.0/65536 * (float)rawValue; // RH= -6 + 125 * SRH/2^16}int SHT2x_GetTempAndHumi(float *temp, float *humi){ int error = NO_ERROR; uint16_t rawValueTemp; uint16_t rawValueHumi; SHT2x_WriteCommand8(TRIG_T_MEASUREMENT_HM); //-- read two data bytes and one checksum byte -- if (i2c_read((i2c_t*)&i2cmaster, MBED_I2C_SLAVE_ADDR0, (char*)&i2cdata_read[0], 3, 1) != 3) error |= NULL_ERROR; //-- verify checksum -- error |= SHT2x_CheckCrc (i2cdata_read,2,i2cdata_read[2]); rawValueTemp = i2cdata_read[0] << 8 | i2cdata_read[1]; SHT2x_WriteCommand8(TRIG_RH_MEASUREMENT_HM); //-- read two data bytes and one checksum byte -- if (i2c_read((i2c_t*)&i2cmaster, MBED_I2C_SLAVE_ADDR0, (char*)&i2cdata_read[0], 3, 1) != 3) error |= NULL_ERROR; //-- verify checksum -- error |= SHT2x_CheckCrc (i2cdata_read,2,i2cdata_read[2]); rawValueHumi = i2cdata_read[0] << 8 | i2cdata_read[1]; //diag_printf("raw temp=0x%x, raw humidity=0x%x, error=%d\n", // rawValueTemp, rawValueHumi, error); //-- calculate humidity and temperature -- if ( error == NO_ERROR ) { *temp = SHT2x_CalcTemperature(rawValueTemp); *humi = SHT2x_CalcHumidity(rawValueHumi); } return error;}void main(void){ gpio_t gpio_led;int led_status;int i2clocalcnt;int error; uint8_t userRegister; //variable for user register int endOfBattery; //variable for end of batteryuint8_t sht2x_serialnumber[8];float temperature = 1.123f;float humidity = 2.456f; DBG_8195A("sleep 10 sec. to wait for UART console\n");Mdelay(10000); DBG_8195A("start i2c example - SHT2x\n"); error = SHT2x_Init(sht2x_serialnumber);if ( error == NO_ERROR ) { DiagPrintf("SHT2x init ok, Serial Number = 0x %02x %02x %02x %02x %02x %02x %02x %02x\r\n", sht2x_serialnumber[7],sht2x_serialnumber[6],sht2x_serialnumber[5],sht2x_serialnumber[4], sht2x_serialnumber[3],sht2x_serialnumber[2],sht2x_serialnumber[1],sht2x_serialnumber[0]);} else { DiagPrintf("SHT2x init FAILED! \r\n"); for(;;);}while(1){error |= SHT2x_GetTempAndHumi(&temperature, &humidity); // --- check end of battery status (eob)--- // note: a RH / Temp. measurement must be executed to update the status of eob error |= SHT2x_ReadUserRegister(&userRegister); //get actual user reg if( (userRegister & SHT2x_EOB_MASK) == SHT2x_EOB_ON ) endOfBattery = true; else endOfBattery = false; rtl_printf("temp=%f, humidity=%f, error=%d\n", temperature, humidity, error);Mdelay(1000);}}
阅读全文
0 0
- RTL8710读温湿度传感器SHT21
- 传感器系列 温湿度传感器DHt11
- DHT21温湿度传感器使用
- 温湿度传感器(九纯健科技)
- 温湿度传感器DHT11
- 温湿度传感器驱动模块
- 温湿度传感器调研报告
- DHT11温湿度传感器
- sht11温湿度传感器
- 温湿度传感器-DHT11
- Arduino - DHT11温湿度传感器
- arduino DHT11 温湿度传感器
- DHT11数字温湿度传感器
- 温湿度传感器---DHT11
- DHT11温湿度传感器
- 温湿度传感器之DHT11
- 数字温湿度传感器DHT11使用
- 温湿度传感器DHT11的数据处理
- 数据库与数据仓库的区别
- mysql中TIMESTAMP和DATETIME的比较
- JS的多种提示框、定时器、定时任务
- 汇编程序的简化写法
- Angular双向数据绑定
- RTL8710读温湿度传感器SHT21
- 智能一代云平台(二十五):利用Nginx对不同的域名进行解析
- 《Efficient Android Threading》Chapter 3---Threads on Android (Android中的线程)
- 微信好友改名、改頭像、改簽名通知
- MySQL中函数使用
- oc 仿微信摇一摇
- 【Shader实战篇】PBR贴图之Specular镜面贴图
- Tmux学习笔记 --- Centos下安装Tmux
- 福利:工作经常用到的Mac软件整理(全)