1、 Version 1.2 February 2010 1/14 Sample Code for SHT21 Supporting Communication Software 1 Structure and Hierarchy of Code The sample code is structured in various procedures. The relationship among the procedures is given in Figure 1. system.h system.cI2C_HAL.h I2C_HAL.ctypedefs.hmain.cDisplayDip20
2、4.h DisplayDip204.c SHT2x.h SHT2x.cDisplay modul Sensor modul (SHT2x)I2C Hardware abstraction System modul (global)Main programFigure 1 Structure of sample code for SHT2x. 2 Sample Code 2.1 Main.c /= / S E N S I R I O N AG, Laubisruetistr. 50, CH-8712 Staefa, Switzerland /= / Project : SHT2x Sample
3、Code (V1.2) / File : main.c / Author : MST / Controller: NEC V850/SG3 (uPD70F3740) / Compiler : IAR compiler for V850 (3.50A) / Brief : This code is an example how to implement basic commands for the / humidity and temperature sensor SHT2x. / Due to compatibility reasons the I2C interface is impleme
4、nted / as “bit-banging“ on normal I/Os. This code is written for an / easy understanding and is neither optimized for speed nor code / size. / / Porting to a different microcontroller (uC): / - define the byte-order for your uC (e.g. little endian) in typedefs.h / - definitions of basic types may ha
5、ve to be changed in typedefs.h / - change the port functions / definitions for your uC in I2C_HAL.h/.c / - adapt the timing of the delay function for your uC in system.c / - adapt the HW_Init() in system.c / - change the uC register definition file in system.h /= /Revision: /V1.1 Initial Version for
6、 SHT2x B-Samples /V1.2 Changed calculation formula in SHT2x.c for C-Samples /- Includes - #include “SHT2x.h“ /header file for SHT2x functions #include “I2C_HAL.h“ /header file for I2C hardware abstraction Preface This sample code is made to communicate with SHT2x humidity and temperature sensors. Th
7、e purpose of the code is to ease the own software programming addressing SHT21 sensors. This sample code has been completed and besides the pure measurement it provides CRC checksum control, set resolution, read serial number, low battery indication and soft-reset. This sample code may be also appli
8、ed on EK-H4 and with debugging hard and software it may be modified and adapted. Sample Code SHT21 Version 1.2 February 2010 2/14#include “DisplayDip204.h“ /header file for display functions #include “System.h“ /header file for system settings #include /header file standard input / output functions
9、 /= int main() /= / variables u8t error = 0; /variable for error code. For codes see system.h u8t userRegister; /variable for user register bt endOfBattery; /variable for end of battery nt16 sRH; /variable for raw humidity ticks ft humidityRH; /variable for relative humidity%RH as float char humitit
10、yOutStr21; /output string for humidity value nt16 sT; /variable for raw temperature ticks ft temperatureC; /variable for temperatureC as float char temperatureOutStr21; /output string for temperature value u8t SerialNumber_SHT2x8; /64bit serial number Init_HW(); /initializes Hardware (osc, watchdog,
11、.) I2c_Init(); /initializes uC-ports for I2C DisplayInit(); /initializes LCD DisplayEnableBacklight(); /enable LCD backlight DisplayWriteString(0,0,“ SHT2x Sample Code “); /write project title on LCD DelayMicroSeconds(15000); /wait for sensor initialization t_powerUp (15ms) /note: The following code
12、 segments show how to use the different functions / of SHT2x. The loop does not show a typical sequence in an application while(1) error = 0; / reset error status / - Reset sensor by command - error |= SHT2x_SoftReset(); / - Read the sensors serial number (64bit) - error |= SHT2x_GetSerialNumber(Ser
13、ialNumber_SHT2x); / - Set Resolution e.g. RH 10bit, Temp 13bit - error |= SHT2x_ReadUserRegister( /get actual user reg userRegister = (userRegister error |= SHT2x_WriteUserRegister( /write changed user reg / - measure humidity with “Hold Master Mode (HM)“ - error |= SHT2x_MeasureHM(HUMIDITY, / - mea
14、sure temperature with “Polling Mode“ (no hold master) - error |= SHT2x_MeasurePoll(TEMP, /- calculate humidity and temperature - temperatureC = SHT2x_CalcTemperatureC(sT.u16); humidityRH = SHT2x_CalcRH(sRH.u16); / - check end of battery status (eob)- / note: a RH / Temp. measurement must be executed
15、 to update the status of eob error |= SHT2x_ReadUserRegister( /get actual user reg if( (userRegister else endOfBattery = false; /- write humidity and temperature values on LCD - sprintf(humitityOutStr, “Humidity RH:%6.2f % “,humidityRH); sprintf(temperatureOutStr, “Temperature:%6.2fC“,temperatureC);
16、 DisplayWriteString(2,0,humitityOutStr); DisplayWriteString(3,0,temperatureOutStr); /- write error or low batt status un LCD - if(error != 0) DisplayWriteString(1,3,“Error occurred“); DisplayWriteString(2,0,“Humidity RH: -.- %“); DisplayWriteString(3,0,“Temperature: -.-C“); else if(endOfBattery) Dis
17、playWriteString(1,3,“Low Batt“); else DisplayWriteString(1,0,“ “); DelayMicroSeconds(300000); / wait 0.3s for next measurement Sample Code SHT21 Version 1.2 February 2010 3/142.2 SHT2x.h #ifndef SHT2x_H #define SHT2x_H /= / S E N S I R I O N AG, Laubisruetistr. 50, CH-8712 Staefa, Switzerland /= /
18、Project : SHT2x Sample Code (V1.2) / File : SHT2x.h / Author : MST / Controller: NEC V850/SG3 (uPD70F3740) / Compiler : IAR compiler for V850 (3.50A) / Brief : Sensor layer. Definitions of commands and registers, / functions for sensor access /= /- Includes - #include “I2C_HAL.h“ #include “system.h“
19、 /- Defines - / CRC const u16t POLYNOMIAL = 0x131; /P(x)=x8+x5+x4+1 = 100110001 / sensor command typedef 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.
20、 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 etSHT2xCommand; typedef enum SHT2x_RES_12_14BIT = 0x0
21、0, / 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
22、_EOB_MASK = 0x40, / Mask for EOB bit(6) in user reg. etSHT2xEob; typedef enum SHT2x_HEATER_ON = 0x04, / heater on SHT2x_HEATER_OFF = 0x00, / heater off SHT2x_HEATER_MASK = 0x04, / Mask for Heater bit(2) in user reg. etSHT2xHeater; / measurement signal selection typedef enum HUMIDITY, TEMP etSHT2xMea
23、sureType; typedef enum I2C_ADR_W = 128, / sensor I2C address + write bit I2C_ADR_R = 129 / sensor I2C address + read bit etI2cHeader; /= u8t SHT2x_CheckCrc(u8t data, u8t nbrOfBytes, u8t checksum); /= / calculates checksum for n bytes of data and compares it with expected / checksum / input: data che
24、cksum is built based on this data / nbrOfBytes checksum is built for n bytes of data / checksum expected checksum / return: error: CHECKSUM_ERROR = checksum does not match / 0 = checksum matches /= u8t SHT2x_ReadUserRegister(u8t *pRegisterValue); /= / reads the SHT2x user register (8bit) / input : -
25、 / output: *pRegisterValue / return: error /= u8t SHT2x_WriteUserRegister(u8t *pRegisterValue); /= Sample Code SHT21 Version 1.2 February 2010 4/14/ writes the SHT2x user register (8bit) / input : *pRegisterValue / output: - / return: error /= u8t SHT2x_MeasurePoll(etSHT2xMeasureType eSHT2xMeasureT
26、ype, nt16 *pMeasurand); /= / measures humidity or temperature. This function polls every 10ms until / measurement is ready. / input: eSHT2xMeasureType / output: *pMeasurand: humidity / temperature as raw value / return: error / note: timing for timeout may be changed /= u8t SHT2x_MeasureHM(etSHT2xMe
27、asureType eSHT2xMeasureType, nt16 *pMeasurand); /= / measures humidity or temperature. This function waits for a hold master until / measurement is ready or a timeout occurred. / input: eSHT2xMeasureType / output: *pMeasurand: humidity / temperature as raw value / return: error / note: timing for ti
28、meout may be changed /= u8t SHT2x_SoftReset(); /= / performs a reset / input: - / output: - / return: error /= float SHT2x_CalcRH(u16t u16sRH); /= / calculates the relative humidity / input: sRH: humidity raw value (16bit scaled) / return: pHumidity relative humidity %RH /= float SHT2x_CalcTemperatu
29、reC(u16t u16sT); /= / calculates temperature / input: sT: temperature raw value (16bit scaled) / return: temperature C /= u8t SHT2x_GetSerialNumber(u8t u8SerialNumber); /= / gets serial number of SHT2x according application note “How To / Read-Out the Serial Number“ / note: readout of this function
30、is not CRC checked / / input: - / output: u8SerialNumber: Array of 8 bytes (64Bits) / MSB LSB / u8SerialNumber7 u8SerialNumber0 / SNA_1 SNA_0 SNB_3 SNB_2 SNB_1 SNB_0 SNC_1 SNC_0 / return: error #endif 2.3 SHT2x.c /= / S E N S I R I O N AG, Laubisruetistr. 50, CH-8712 Staefa, Switzerland /= / Project
31、 : 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“ /= u8t SHT2x_CheckCrc(u8t data, u8t nbrOfBytes, u8t checksum) /= u8t
32、crc = 0; u8t byteCtr; /calculates 8-Bit checksum with given polynomial for (byteCtr = 0; byteCtr 0; -bit) if (crc pMeasurand-s16.u8L = data1 = I2c_ReadByte(ACK); checksum=I2c_ReadByte(NO_ACK); /- verify checksum - error |= SHT2x_CheckCrc (data,2,checksum); I2c_StopCondition(); return error; /= u8t S
33、HT2x_MeasurePoll(etSHT2xMeasureType eSHT2xMeasureType, nt16 *pMeasurand) /= u8t checksum; /checksum u8t data2; /data array for checksum verification u8t error=0; /error variable u16t i=0; /counting variable Sample Code SHT21 Version 1.2 February 2010 6/14/- write I2C sensor address and command - I2
34、c_StartCondition(); error |= I2c_WriteByte (I2C_ADR_W); / I2C Adr switch(eSHT2xMeasureType) case HUMIDITY: error |= I2c_WriteByte (TRIG_RH_MEASUREMENT_POLL); break; case TEMP : error |= I2c_WriteByte (TRIG_T_MEASUREMENT_POLL); break; default: assert(0); /- poll every 10ms for measurement ready. Time
35、out after 20 retries (200ms)- do I2c_StartCondition(); DelayMicroSeconds(10000); /delay 10ms if(i+ = 20) break; while(I2c_WriteByte (I2C_ADR_R) = ACK_ERROR); if (i=20) error |= TIME_OUT_ERROR; /- read two data bytes and one checksum byte - pMeasurand-s16.u8H = data0 = I2c_ReadByte(ACK); pMeasurand-s
36、16.u8L = data1 = I2c_ReadByte(ACK); checksum=I2c_ReadByte(NO_ACK); /- verify checksum - error |= SHT2x_CheckCrc (data,2,checksum); I2c_StopCondition(); return error; /= u8t SHT2x_SoftReset() /= u8t error=0; /error variable I2c_StartCondition(); error |= I2c_WriteByte (I2C_ADR_W); / I2C Adr error |= I2c_WriteByte (SOFT_RESET); / Command I2c_StopCondition(); DelayMicroSeconds(15000); / wait till sensor has restarted return error; /= float SHT2x_CalcRH(u16t u16sRH) /= ft humidityRH; / variable for result u16sRH / clear bits 10 (status bits) /