1178 views|0 replies

2015

Posts

0

Resources
The OP
 

MSP430 Launchpad MSP430g2452 SHT10 Temperature and Humidity Sensor [Copy link]

SHT10 temperature and humidity sensor on MSP430 Launchpad MSP430g2452 platform

//********************************File Information*******************************
//** File Name: Sht10.c
//** Platform: MSP430 LaunchPad MSP430G2452
//** System Function: Sht10 Sensirion Inc driver code
//** Created by: ding
//** Created date: 2014-09-15
//** Version: V1.0
//******************************************************************************
//---------------------------------------
//Hardware Wiring - Interface Definition:
//SCK: -->P1.5 //Define communication clock port
//SDA: -->P1.6 //Define communication data port
//---------------------------------------

#include <msp430g2452.h>//Library
#include <intrinsics.h> //__no_operation();
//#include <math.h> //Library
//#include <stdio.h> //printf();

//Macro definition, delay function, when the parameter is 1, the corresponding delay is 1us and 1ms respectively
#define CPU_F (double)1000000
#define delay_us(x) __delay_cycles((long)(CPU_F * (double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F * (double)x/1000.0))

#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
//adr command r/w
#define STATUS_REG_W 0x06 //000 0011 0 //Write status register
#define STATUS_REG_R 0x07 //000 0011 1 //Read status register
#define MEASURE_TEMP 0x03 //000 0001 1 //Temperature measurement
#define MEASURE_HUMI 0x05 //000 0010 1 //Humidity measurement
#define RESET 0x1e //000 1111 0 //Soft reset

#define bitselect 0x01 //Select the low bit of temperature and humidity to read
#define noACK 0 //No ACK returned
#define ACK 1 //Return ACK
#define HUMIDITY 2
#define TEMPERATURE 1

#define SCK BIT5 //P1.5
#define SDA BIT6 //P1.6
#define SCK_H P1OUT|=SCK //High
#define SCK_L P1OUT&=~SCK //Low
#define SDA_H P1OUT|=SDA //High
#define SDA_L P1OUT&=~SDA //Low

typedef union //Defines two unions
{
unsigned int i;
float f;
}value;

/**************************************************************************************************************
**Function Name: S_Init
**Description: Initialization
**Input Parameters: no
**Output Parameters: no
**************************************************************************************************/
void S_Init()
{
P1SEL&=~(SCK+SDA);
P1DIR|=SCK;
P1DIR&=~SDA;
//BCSCTL1=(XT2OFF+RSEL2); //Turn off XT2, 1MHz DOC
//DCOCTL=DCO2; //设定DCO频率为1MHz
}

/**********************************************************************************************************
**Function Name: S_Transstart
**Description: start时序
** generates a transmission start
** _____ ________
** DATA: |_______|
** ___ ___
** SCK : ___| |___| |______
**********************************************************************************************************/
void S_Transstart()
{
P1DIR|=SDA;
SDA_H;SCK_L;
__no_operation();
SCK_H;
__no_operation();
SDA_L;
__no_operation();
SCK_L;
__no_operation();
__no_operation();
__no_operation();
SCK_H;
__no_operation();
SDA_H;
__no_operation();
SCK_L;
P1DIR&=~SDA;
}

/**********************************************************************************************************
**Function Name: S_WriteByte
**Description: 写函数
**********************************************************************************************************/
char S_WriteByte(unsigned char value)
{
// writes a byte on the Sensibus and checks the acknowledge
unsigned char i,error=0;
P1DIR|=SDA;
for(i=0x80;i>0;i/=2) //shift bit for masking
{
if(i&value) SDA_H; //masking value with i , write to SENSI-BUS
else SDA_L;
SCK_H; //clk for SENSI-BUS
__no_operation();__no_operation();__no_operation();//pulswith approx. 5 us
SCK_L;
}
SDA_H; //release DATA-line
P1DIR&=~SDA; //Change SDA to be input 0:input 1:ouput
SCK_H; //clk #9 for ack
error=P1IN; //check ack (DATA will be pulled down by SHT11)
error&=SDA;
P1DIR|=SDA; //Change SDA to be output 0:input 1:ouput
SCK_L;
if(error)
return 1; //error=1 in case of no acknowledge
return 0;
}

/**********************************************************************************************************
**Function Name: S_ReadByte
**Description: 读函数
**Input Parameters: ack--->reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
**********************************************************************************************************/
char S_ReadByte(unsigned char ack)
{
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
unsigned char i,val=0;
P1DIR|=SDA; //Change SDA to be output 0:input 1:ouput
SDA_H; //release DATA-line
P1DIR&=~SDA; //Change SDA to be input 0:input 1:ouput
for(i=0x80;i>0;i/=2) //shift bit for masking
{
SCK_H; //clk for SENSI-BUS
if(P1IN&SDA)
val=(val|i); //read bit
SCK_L;
}
P1DIR|=SDA; //Change SDA to be output 0:input 1:ouput
if(ack) //in case of "ack==1" pull down DATA-Line
SDA_L;
else
SDA_H;
SCK_H; //clk #9 for ack
__no_operation();__no_operation();__no_operation();//pulswith approx. 5 us
SCK_L;
SDA_H; //release DATA-line
P1DIR&=~SDA; //Change SDA to be output 0:input 1:ouput
return val;
}

/**********************************************************************************************************
**Function Name: S_Connectionreset
**Description: 连接复位函数
** communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
** _____________________________________________________ ________
** DATA: |_______|
** _ _ _ _ _ _ _ _ _ ___ ___
** SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
**********************************************************************************************************/
void S_Connectionreset()
{
unsigned char ClkCnt;
P1DIR|=SDA; //Change SDA to be input 0:input 1:ouput
SDA_H;SCK_L; //Initial state
for(ClkCnt=0;ClkCnt<9;ClkCnt++) //9 SCK cycles
{
SCK_H;
SCK_L;
}
S_Transstart(); //transmission start
}
/**********************************************************************************************************
**Function Name: S_Softreset
**Description: 软件复位函数 resets the sensor by a softreset
**********************************************************************************************************/
char S_Softreset()
{
unsigned char error=0;
S_Connectionreset(); //reset communication
error+=S_WriteByte(RESET); //send RESET-command to sensor
return error; //error=1 in case of no response form the sensor
}

/**********************************************************************************************************
**Function Name: S_WriteStatusReg
**Description: 写状态寄存器
**Input Parameters: *p_value
**********************************************************************************************************/
char S_WriteStatusReg(unsigned char *p_value)
{
unsigned char error=0;
S_Transstart(); //transmission start
error+=S_WriteByte(STATUS_REG_W); //send command to sensor
error+=S_WriteByte(*p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
}

/**********************************************************************************************************
**Function Name: S_Mearsure
**Description: 读时序 makes a measurement (humidity/temperature) with checksum
**Input Parameters: *p_value ,*p_checknum ,mode
**********************************************************************************************************/
unsigned char S_Measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned error=0;
unsigned int i;

S_Transstart(); //transmission start
switch(mode)
{ //send command to sensor
case TEMPERATURE: error+=S_WriteByte(MEASURE_TEMP); break;
case HUMIDITY: error+=S_WriteByte(MEASURE_HUMI); break;
}
P1DIR&=~SDA; //Change SDA to be input 0:input 1:ouput
for(i=0;i<65535;i++) if((P1IN&SDA)==0) break; //wait until sensor has finished the measurement
if(P1IN&SDA) error+=1; //or timeout (~2 sec.) is reached
*(p_value)=S_ReadByte(ACK); //read the first byte (MSB) high 8 bits
*(p_value+1)=S_ReadByte(ACK); //read the second byte (LSB) low 8 bits
*p_checksum=S_ReadByte (noACK); //read checksum
return error;
}

/**************************************** *************************************************** *******************
**Function Name: S_Calculate
**Description: Calculate
**Input Parameters: humi [Ticks] (12 bit) Default
** temp [Ticks] (14 bit) Default
**Output Parameters: humi [%RH]
** temp [℃]
******************************* *************************************************** ***************************/
void S_Calculate(unsigned int *p_humidity, unsigned int *p_temperature)
//'1' = 8bit humidity/ 12bit temperature resolution --> Modifiable resolution
//'0' = 12bit humidity / 14bit temperature.resolution --> Default selection
{
//12bit humidity / 14bit temperature.resolution
const float C1=-2.0468; // for 12 Bit humidity conversion parameter
const float C2=0.0367; // for 12 Bit humidity conversion parameter
const float C3=-0.0000015955; // for 12 Bit humidity conversion parameter
const float D1=-39.6; // for 14 Bit @ 3V temperature
const float D2=0.01; // for 14 Bit @ 3V temperature
const float T1=0.01; // for 12 bit temperature compensation of humidity signal
const float T2=0.00008; // for 12 bit temperature compensation of humidity signal


//8bit humidity / 12bit temperature resolution
/*
const float C1=-2.0468; // for 8 Bit humidity conversion parameter
const float C2=+0.5872; // for 8 Bit humidity conversion parameter
const float C3=-0.00040845; // for 8 Bit humidity conversion parameter
const float D1=-39.6; // for 12 Bit @ 3V temperature
const float D2=+0.04; // for 12 Bit @ 3V temperature
const float T1=0.01; // for 8 bit humidity signal temperature compensation
const float T2=0.00128; // for 8 bit Temperature compensation of humidity signal
*/
float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true ; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [℃]

t_C=t*D2+D1; //calc. temperature from ticks to [℃]
rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity. from ticks to [%RH]
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
if(rh_true>100)rh_true=100; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.1; //the physical possible range

*p_temperature = (uint)t_C; //return temperature [℃]
*p_humidity = (uint)rh_true; //return humidity[%RH]
}

//************************************************************************************
//**Function Name: calc_dewpoint
//**Description: 露点
//************************************************************************************
/*
float Calc_Dewpoint(float h,float t)
// calculates dew point
// input: humidity [%RH], temperature [℃]
// output: dew point [℃]
{ float logEx,dew_point;
logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point;
}
*/

/**********************************************************************************************************
**Function Name: 主函数
**********************************************************************************************************/
void main()
{
// sample program that shows how to use SHT11 functions
// 1. connection reset
// 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
// 3. calculate humidity [%RH] and temperature [℃]
// 4. calculate dew point [℃]
// 5. print temperature, humidity, dew point

value humi_val,temp_val;
//float dew_point; //露点
unsigned char error,checksum;
//unsigned int temphigh,templow,humilow;
unsigned int temphigh,templow,humihigh,humilow;
//unsigned int Reg_Resolution=0x01;

WDTCTL=WDTPW+WDTHOLD; //Stop watchdog timer

S_Init(); //初始化
S_Connectionreset(); //连接复位
//S_WriteStatusReg((unsigned char *)&Reg_Resolution);//修改测量分辨率:8bit湿度 / 12bit温度
while(1)
{
error=0;
error+=S_Measure((unsigned char*) &humi_val.i,&checksum,HUMIDITY); //measure humidity
error+=S_Measure((unsigned char*) &temp_val.i,&checksum,TEMPERATURE); //measure temperature
if(error!=0)
S_Connectionreset(); //in case of an error: connection reset
else
{
//Note:修改分辨率需要传感器重新上下电才可生效
//-------------测量分辨率:8bit湿度 / 12bit温度----------------//
//8bit humidity MSB=0x0000,00000(invalid) LSB=0xmmmm,mmmm(valid)
//humilow=(humi_val.i&0xff00);
//humi_val.i=humilow>>8; //Humidity
//12bit temperature MSB=0x0000,mmmm( 4bits valid) LSB=0xmmmm,mmmm(valid)
//temphigh=((temp_val.i&0x0f)<<8);
//templow=((temp_val.i&0xff00)>>8);
//temp_val.i=temphigh+templow;//Temperature

//-------------测量分辨率:12bit湿度 / 14bit温度----------------//
//12bit humidity MSB=0x0000,mmmm(4bits valid) LSB=0xmmmm,mmmm(valid)
humihigh=((humi_val.i&0x0f)<<8);
humilow=((humi_val.i&0xff00)>>8);
humi_val.i=humihigh+humilow;//Humidity

//14bit temperature MSB=0x00mm,mmmm( 6bit valid) LSB=0xmmmm,mmmm(valid)
temphigh=((temp_val.i&0x3f)<<8);
templow=((temp_val.i&0xff00)>>8);
temp_val.i=temphigh+templow;//Temperature

S_Calculate(&humi_val.i,&temp_val.i); //calculate humidity, temperature

//dew_point=Calc_Dewpoint(humi_val.i,temp_val.i); //calculate dew point
//printf("temp:%5.1fC humi:%5.1f%% dew point:%5.1fC\n",temp_val.i,humi_val.i,dew_point);
}
//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
for (uint i=0;i<40000;i++);//(be sure that the compiler doesn't eliminate this line!)
//-----------------------------------------------------------------------------------
}
}

This post is from Microcontroller MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list