MSP430 Launchpad MSP430g2452 SHT10 Temperature and Humidity Sensor[Copy link]
//************************************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 connection-interface definition: [/size ] //SCK: -->P1.5 //Define the communication clock port //SDA: -->P1.6 //Define the communication data port //--------------------------------------- #include//Library
#include //__no_operation();
//#include //Library
//#include //printf(); //Macro definition, delay function, when the parameter is 1, the corresponding delays are 1us and 1ms respectively [ size=4]#define CPU_F (double)1000000 #define delay_us(x) __delay_cycles((long)(CPU_F * (double)x/1000000.0)) [size=4 ]#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-order reading of temperature and humidity [size= 4]#define noACK 0 //No ACK returned #define ACK 1 //Return ACK #define HUMIDITY 2 #define TEMPERATURE 1 [size= 4] #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 [ size=4]#define SDA_L P1OUT&=~SDA //Low typedef union //Two unions are defined { unsigned int i; float f; }value; [size= 4] /****************************************** *************************************************** ****************** **Function Name: S_Init **Description: Initialization[/size ] **Input Parameters: no **Output Parameters: no ************** *************************************************** ******************************************/ [size= 4]void S_Init() { P1SEL&=~(SCK+SDA); P1DIR|=SCK; P1DIR&=~SDA; //BCSCTL1= (XT2OFF+RSEL2); //Turn off XT2, 1MHz DOC //DCOCTL=DCO2; //Set the DCO frequency to 1MHz } /**************************************** *************************************************** ********************* **Function Name: S_Transstart **Description: start Timing ** 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) 高8位
*(p_value+1)=S_ReadByte(ACK); //read the second byte (LSB)低8位
*p_checksum=S_ReadByte(noACK); //read checksum
return error;
}
/**********************************************************************************************************
**Function Name: S_Calculate
**Description: 计算
**Input Parameters: humi [Ticks] (12 bit) 默认
** temp [Ticks] (14 bit) 默认
**Output Parameters: humi [%RH]
** temp [℃]
**********************************************************************************************************/
void S_Calculate(unsigned int *p_humidity ,unsigned int *p_temperature)
//’1’ = 8bit 湿度 / 12bit 温度分辨率 -->可修改分辨率
//’0’ = 12bit 湿度 / 14bit 温度.分辨率 -->默认选择
{
//12bit 湿度 / 14bit 温度.分辨率
const float C1=-2.0468; // for 12 Bit 湿度转换参数
const float C2=0.0367; // for 12 Bit 湿度转换参数
const float C3=-0.0000015955; // for 12 Bit 湿度转换参数
const float D1=-39.6; // for 14 Bit @ 3V 温度
const float D2=0.01; // for 14 Bit @ 3V 温度
const float T1=0.01; // for 12 bit 湿度信号的温度补偿
const float T2=0.00008; // for 12 bit 湿度信号的温度补偿
//8bit 湿度 / 12bit 温度分辨率
/*
const float C1=-2.0468; // for 8 Bit 湿度转换参数
const float C2=+0.5872; // for 8 Bit 湿度转换参数
const float C3=-0.00040845; // for 8 Bit 湿度转换参数
const float D1=-39.6; // for 12 Bit @ 3V 温度
const float D2=+0.04; // for 12 Bit @ 3V 温度
const float T1=0.01; // for 8 bit 湿度信号的温度补偿
const float T2=0.00128; // for 8 bit 湿度信号的温度补偿
*/
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
/**********************************************************************************************************
**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 [/ size] S_Init(); //Initialization S_Connectionreset(); //Connection reset //S_WriteStatusReg((unsigned char *)&Reg_Resolution);//Modify measurement resolution: 8bit humidity/12bit temperature 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) [/size ] S_Connectionreset(); //in case of an error: connection reset else { / /Note: Modifying the resolution requires the sensor to be powered on and off to take effect //-------------Measurement resolution: 8-bit humidity/ 12-bit temperature--- -------------// //8bit humidity MSB=0x0000,00000(invalid) LSB=0xmmmm,mmmm(valid) [size =4] //humilow=(humi_val.i&0xff00); //humi_val.i=humilow>>8; //Humidity //12bit temperature MSB=0x0000,mmmm( 4bits valid) LSB=0xmmmm,mmmm(valid) [size =4] //temphigh=((temp_val.i&0x0f)<<8); //templow=((temp_val.i&0xff00)>>8); //temp_val.i=temphigh+templow;//Temperature //-------------Measurement resolution: 12bit humidity/14bit temperature-------------// [/size ] //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------------------------------ [size=4 ] for (uint i=0;i<40000;i++);//(be sure that the compiler doesn't eliminate this line!) //---------------------------------------------- ------------------------------------- } } )
//-----------------------------------------------------------------------------------
}
} )
//-----------------------------------------------------------------------------------
}
} )
//-----------------------------------------------------------------------------------
}
} )
//-----------------------------------------------------------------------------------
}
} 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
//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!)
//-----------------------------------------------------------------------------------
}
} =0) S_Connectionreset(); //in case of an error: connection reset else { //Note: Modifying the resolution requires the sensor to be powered on and off to take effect //-------------Measurement resolution: 8bit humidity/ 12bit temperature----------------// //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 //-------------Measurement resolution: 12bit humidity/14bit temperature----------------// //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!) //------------------------------------------------------------------------------------------------ } } =0) S_Connectionreset(); //in case of an error: connection reset else { //Note: Modifying the resolution requires the sensor to be powered on and off to take effect //-------------Measurement resolution: 8bit humidity/ 12bit temperature----------------// //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 //-------------Measurement resolution: 12bit humidity/14bit temperature----------------// //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!) //------------------------------------------------------------------------------------------------ } } i&0xff00)>>8);
//temp_val.i=temphigh+templow;//Temperature
//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!)
//-----------------------------------------------------------------------------------
}
} i&0xff00)>>8);
//temp_val.i=temphigh+templow;//Temperature
//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!)
//-----------------------------------------------------------------------------------
}
} 8s to avoid heating up SHTxx------------------------------
for (uint i=0;i<40000;i++);//(be sure that the compiler doesn't eliminate this line!)
//-----------------------------------------------------------------------------------
}
} 8s to avoid heating up SHTxx------------------------------
for (uint i=0;i<40000;i++);//(be sure that the compiler doesn't eliminate this line!)
//-----------------------------------------------------------------------------------
}
}