323 lines
9.5 KiB
C
323 lines
9.5 KiB
C
|
|
//==============================================================================
|
|||
|
|
// S E N S I R I O N AG, Laubisruetistr. 50, CH-8712 Staefa, Switzerland
|
|||
|
|
//==============================================================================
|
|||
|
|
// Project : SHT2x Sample Code (V1.2)
|
|||
|
|
// File : SHT2x.c
|
|||
|
|
// Author : MST
|
|||
|
|
// Controller: NEC V850/SG3 (uPD70F3740)
|
|||
|
|
// Compiler : IAR compiler for V850 (3.50A)
|
|||
|
|
// Brief : Sensor layer. Functions for sensor access
|
|||
|
|
//==============================================================================
|
|||
|
|
|
|||
|
|
//---------- Includes ----------------------------------------------------------
|
|||
|
|
#include "SHT2x.h"
|
|||
|
|
|
|||
|
|
#include "iot_i2c_api.h"
|
|||
|
|
#include "iot_io_api.h"
|
|||
|
|
|
|||
|
|
|
|||
|
|
extern void delay_sometime(void);
|
|||
|
|
extern int g_port;
|
|||
|
|
|
|||
|
|
//==============================================================================
|
|||
|
|
u8t SHT2x_CheckCrc(u8t data[], u8t nbrOfBytes, u8t checksum)
|
|||
|
|
//==============================================================================
|
|||
|
|
{
|
|||
|
|
u8t crc = 0;
|
|||
|
|
u8t byteCtr;
|
|||
|
|
//calculates 8-Bit checksum with given polynomial
|
|||
|
|
for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr)
|
|||
|
|
{ crc ^= (data[byteCtr]);
|
|||
|
|
for (u8t bit = 8; bit > 0; --bit)
|
|||
|
|
{ if (crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL;
|
|||
|
|
else crc = (crc << 1);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (crc != checksum) return CHECKSUM_ERROR;
|
|||
|
|
else return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
//===========================================================================
|
|||
|
|
u8t SHT2x_ReadUserRegister(u8t *pRegisterValue)
|
|||
|
|
//===========================================================================
|
|||
|
|
{
|
|||
|
|
u8t checksum; //variable for checksum byte
|
|||
|
|
u8t error=0; //variable for error code
|
|||
|
|
|
|||
|
|
int ret ;
|
|||
|
|
char send_buf[6];
|
|||
|
|
|
|||
|
|
send_buf[0] = USER_REG_R;
|
|||
|
|
|
|||
|
|
//Read from memory location 1
|
|||
|
|
ret = iot_i2c_write(g_port,SHT2X_ADDR,(char *)send_buf, 1);
|
|||
|
|
if ( ret ) {
|
|||
|
|
iot_printf("write failed ret = %d.\n", ret);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (iot_i2c_read(g_port, SHT2X_ADDR, send_buf, 2)) {
|
|||
|
|
iot_printf("read failed.\n");
|
|||
|
|
} else {
|
|||
|
|
for (int i = 0; i < 4; i++) {
|
|||
|
|
iot_printf("read[%d]=%02x\r\n", i, send_buf[i] & 0xff);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
*pRegisterValue = send_buf[0];
|
|||
|
|
checksum = send_buf[1];
|
|||
|
|
error |= SHT2x_CheckCrc (pRegisterValue, 1, checksum);
|
|||
|
|
if (error) {
|
|||
|
|
iot_printf("read user crc err\r\n");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return error;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//===========================================================================
|
|||
|
|
u8t SHT2x_WriteUserRegister(u8t *pRegisterValue)
|
|||
|
|
//===========================================================================
|
|||
|
|
{
|
|||
|
|
u8t error=0; //variable for error code
|
|||
|
|
|
|||
|
|
int ret ;
|
|||
|
|
char send_buf[6];
|
|||
|
|
|
|||
|
|
send_buf[0] = USER_REG_W;
|
|||
|
|
|
|||
|
|
ret = iot_i2c_write(g_port,SHT2X_ADDR,(char *)send_buf, 1);
|
|||
|
|
if ( ret ) {
|
|||
|
|
iot_printf("write failed ret = %d.\n", ret);
|
|||
|
|
}
|
|||
|
|
return error;
|
|||
|
|
}
|
|||
|
|
#if 0
|
|||
|
|
|
|||
|
|
|
|||
|
|
//===========================================================================
|
|||
|
|
u8t SHT2x_MeasureHM(etSHT2xMeasureType eSHT2xMeasureType, nt16 *pMeasurand)
|
|||
|
|
//===========================================================================
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
u8t checksum; //checksum
|
|||
|
|
u8t data[2]; //data array for checksum verification
|
|||
|
|
u8t error=0; //error variable
|
|||
|
|
u16t i; //counting variable
|
|||
|
|
|
|||
|
|
//-- write I2C sensor address and command --
|
|||
|
|
I2c_StartCondition();
|
|||
|
|
error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr
|
|||
|
|
switch(eSHT2xMeasureType)
|
|||
|
|
{ case HUMIDITY: error |= I2c_WriteByte (TRIG_RH_MEASUREMENT_HM); break;
|
|||
|
|
case TEMP : error |= I2c_WriteByte (TRIG_T_MEASUREMENT_HM); break;
|
|||
|
|
default: break;
|
|||
|
|
}
|
|||
|
|
//-- wait until hold master is released --
|
|||
|
|
I2c_StartCondition();
|
|||
|
|
error |= I2c_WriteByte (I2C_ADR_R);
|
|||
|
|
SCL=HIGH; // set SCL I/O port as input
|
|||
|
|
for(i=0; i<1000; i++) // wait until master hold is released or
|
|||
|
|
{ DelayMicroSeconds(1000); // a timeout (~1s) is reached
|
|||
|
|
if (SCL_CONF==1) break;
|
|||
|
|
}
|
|||
|
|
//-- check for timeout --
|
|||
|
|
if(SCL_CONF==0) error |= TIME_OUT_ERROR;
|
|||
|
|
|
|||
|
|
//-- read two data bytes and one checksum byte --
|
|||
|
|
pMeasurand->s16.u8H = data[0] = I2c_ReadByte(ACK);
|
|||
|
|
pMeasurand->s16.u8L = data[1] = I2c_ReadByte(ACK);
|
|||
|
|
checksum=I2c_ReadByte(NO_ACK);
|
|||
|
|
|
|||
|
|
//-- verify checksum --
|
|||
|
|
error |= SHT2x_CheckCrc (data,2,checksum);
|
|||
|
|
I2c_StopCondition();
|
|||
|
|
return error;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
#endif
|
|||
|
|
|
|||
|
|
//===========================================================================
|
|||
|
|
u8t SHT2x_MeasurePoll(etSHT2xMeasureType eSHT2xMeasureType, nt16 *pMeasurand)
|
|||
|
|
//===========================================================================
|
|||
|
|
{
|
|||
|
|
u8t checksum; //checksum
|
|||
|
|
u8t error=0; //error variable
|
|||
|
|
|
|||
|
|
//-- write I2C sensor address and command --
|
|||
|
|
|
|||
|
|
int ret ;
|
|||
|
|
char send_buf[6];
|
|||
|
|
|
|||
|
|
switch(eSHT2xMeasureType)
|
|||
|
|
{ case HUMIDITY: send_buf[0] = TRIG_RH_MEASUREMENT_POLL; break;
|
|||
|
|
case TEMP : send_buf[0] = TRIG_T_MEASUREMENT_POLL; break;
|
|||
|
|
default: break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
ret = iot_i2c_write(g_port,SHT2X_ADDR,(char *)send_buf, 1);
|
|||
|
|
if ( ret ) {
|
|||
|
|
iot_printf("write failed ret = %d.\n", ret);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
delay_sometime();
|
|||
|
|
delay_sometime();
|
|||
|
|
delay_sometime();
|
|||
|
|
|
|||
|
|
//-- read two data bytes and one checksum byte --
|
|||
|
|
if (iot_i2c_read(g_port, SHT2X_ADDR, send_buf, 3)) {
|
|||
|
|
iot_printf("read failed.\n");
|
|||
|
|
} else {
|
|||
|
|
//for (int i = 0; i < 3; i++) {
|
|||
|
|
//iot_printf("read[%d]=%02x\r\n", i, send_buf[i] & 0xff);
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
pMeasurand->s16.u8H = send_buf[0];
|
|||
|
|
pMeasurand->s16.u8L = send_buf[1];
|
|||
|
|
checksum = (u8t)send_buf[2];
|
|||
|
|
|
|||
|
|
//-- verify checksum --
|
|||
|
|
error |= SHT2x_CheckCrc ((u8t *)send_buf, 2, checksum);
|
|||
|
|
|
|||
|
|
if (error) {
|
|||
|
|
iot_printf("MeasurePoll crc err\r\n");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return error;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
//===========================================================================
|
|||
|
|
u8t SHT2x_SoftReset()
|
|||
|
|
//===========================================================================
|
|||
|
|
{
|
|||
|
|
u8t error=0; //error variable
|
|||
|
|
int ret;
|
|||
|
|
|
|||
|
|
char send_buf[1];
|
|||
|
|
send_buf[0] = SOFT_RESET;
|
|||
|
|
ret = iot_i2c_write(g_port,SHT2X_ADDR,(char *)send_buf, sizeof(send_buf));
|
|||
|
|
if ( ret ) {
|
|||
|
|
iot_printf("write failed ret = %d.\n", ret);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
delay_sometime();
|
|||
|
|
return error;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//==============================================================================
|
|||
|
|
float SHT2x_CalcRH(u16t u16sRH)
|
|||
|
|
//==============================================================================
|
|||
|
|
{
|
|||
|
|
ft humidityRH; // variable for result
|
|||
|
|
|
|||
|
|
u16sRH &= ~0x0003; // clear bits [1..0] (status bits)
|
|||
|
|
//-- calculate relative humidity [%RH] --
|
|||
|
|
|
|||
|
|
humidityRH = -6.0 + 125.0/65536 * (ft)u16sRH; // RH= -6 + 125 * SRH/2^16
|
|||
|
|
return humidityRH;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//==============================================================================
|
|||
|
|
float SHT2x_CalcTemperatureC(u16t u16sT)
|
|||
|
|
//==============================================================================
|
|||
|
|
{
|
|||
|
|
ft temperatureC; // variable for result
|
|||
|
|
|
|||
|
|
u16sT &= ~0x0003; // clear bits [1..0] (status bits)
|
|||
|
|
|
|||
|
|
//-- calculate temperature [<5B>C] --
|
|||
|
|
temperatureC= -46.85 + 175.72/65536 *(ft)u16sT; //T= -46.85 + 175.72 * ST/2^16
|
|||
|
|
return temperatureC;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//==============================================================================
|
|||
|
|
u8t SHT2x_GetSerialNumber(u8t u8SerialNumber[])
|
|||
|
|
//==============================================================================
|
|||
|
|
{
|
|||
|
|
u8t error=0; //error variable
|
|||
|
|
|
|||
|
|
int ret ;
|
|||
|
|
char send_buf[6];
|
|||
|
|
|
|||
|
|
send_buf[0] = 0xFA;
|
|||
|
|
send_buf[1] = 0x0F;
|
|||
|
|
|
|||
|
|
//Read from memory location 1
|
|||
|
|
ret = iot_i2c_write(g_port,SHT2X_ADDR,(char *)send_buf, 2);
|
|||
|
|
if ( ret ) {
|
|||
|
|
iot_printf("write failed ret = %d.\n", ret);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
if (iot_i2c_read(g_port, SHT2X_ADDR, send_buf, 4)) {
|
|||
|
|
iot_printf("read failed.\n");
|
|||
|
|
} else {
|
|||
|
|
for (int i = 0; i < 4; i++) {
|
|||
|
|
iot_printf("read[%d]=%02x\r\n", i, send_buf[i] & 0xff);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//Read from memory location 2
|
|||
|
|
|
|||
|
|
send_buf[0] = 0xFC;
|
|||
|
|
send_buf[1] = 0xC9;
|
|||
|
|
|
|||
|
|
//Read from memory location 1
|
|||
|
|
ret = iot_i2c_write(g_port,SHT2X_ADDR,(char *)send_buf, 2);
|
|||
|
|
if ( ret ) {
|
|||
|
|
iot_printf("write failed ret = %d.\n", ret);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
if (iot_i2c_read(g_port, SHT2X_ADDR, send_buf, 6)) {
|
|||
|
|
iot_printf("read failed.\n");
|
|||
|
|
} else {
|
|||
|
|
for (int i = 0; i < 6; i++) {
|
|||
|
|
iot_printf("read[%d]=%02x\r\n", i, send_buf[i] & 0xff);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return error;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
void SHT2x_test_loop(void)
|
|||
|
|
{
|
|||
|
|
u8t error = 0; //variable for error code. For codes see system.h
|
|||
|
|
u8t userRegister; //variable for user register
|
|||
|
|
u8t serial_num[6] ;
|
|||
|
|
nt16 sT;
|
|||
|
|
nt16 sRH;
|
|||
|
|
ft temperatureC;
|
|||
|
|
ft humidityRH;
|
|||
|
|
|
|||
|
|
SHT2x_SoftReset();
|
|||
|
|
SHT2x_GetSerialNumber(serial_num);
|
|||
|
|
|
|||
|
|
|
|||
|
|
// --- 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
|
|||
|
|
|
|||
|
|
while(1)
|
|||
|
|
{
|
|||
|
|
SHT2x_MeasurePoll(TEMP, &sT);
|
|||
|
|
//-- calculate humidity and temperature --
|
|||
|
|
temperatureC = SHT2x_CalcTemperatureC(sT.u16);
|
|||
|
|
delay_sometime();
|
|||
|
|
SHT2x_MeasurePoll(HUMIDITY, &sRH);
|
|||
|
|
humidityRH = SHT2x_CalcRH(sRH.u16);
|
|||
|
|
|
|||
|
|
iot_printf("temp=%f, humidityRH=%f\r\n", temperatureC, humidityRH);
|
|||
|
|
|
|||
|
|
delay_sometime();
|
|||
|
|
delay_sometime();
|
|||
|
|
delay_sometime();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|