#include "msp430x14x.h" //头文件 #include "SHT1X.h" #define _nop_() _NOP() //float iAdResult[MM]; //---------------------------------------------------------------------------------- // modul-var 温湿度传感器SHT10驱动程序开始 //---------------------------------------------------------------------------------- //---------------------------------------------------------------------------------- char s_write_byte(unsigned char value) //---------------------------------------------------------------------------------- // writes a byte on the Sensibus and checks the acknowledge { unsigned char i,error=0; DATA_OutDIR(); for (i=0x80;i>0;i/=2) //shift bit for masking { if (i & value) Set_DATA(); //DATA=1 masking value with i , write to SENSI-BUS else Clear_DATA(); //DATA=0 Set_CLK(); //SCK=1 clk for SENSI-BUS _nop_();_nop_();_nop_(); //pulswith approx. 5 us Clear_CLK(); //SCK=0 } Set_DATA(); //DATA=1 release DATA-line DATA_InDIR(); Set_CLK(); //SCK=1 clk #9 for ack //DATA_InDIR(); error=P1IN&DATA; // error=DATA check ack (DATA will be pulled down by SHT11) DATA_OutDIR(); Clear_CLK(); //SCK=0 if(error) return 1; return 0; //error=1 in case of no acknowledge } //---------------------------------------------------------------------------------- char s_read_byte(unsigned char ack) //---------------------------------------------------------------------------------- // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" { unsigned char i,val=0; DATA_OutDIR(); Set_DATA(); //DATA=1 release DATA-line DATA_InDIR(); for (i=0x80;i>0;i/=2) //shift bit for masking { Set_CLK(); //SCK=1 clk for SENSI-BUS if (P1IN&DATA) val=(val | i); //(DATA) read bit Clear_CLK(); //SCK=0 } DATA_OutDIR(); if(ack) Clear_DATA() ; else Set_DATA(); // DATA=!ack; in case of "ack==1" pull down DATA-Line Set_CLK(); //SCK=1 clk #9 for ack _nop_();_nop_();_nop_(); //pulswith approx. 5 us Clear_CLK(); //SCK=0 Set_DATA(); //DATA=1 release DATA-line DATA_InDIR(); return val; } //---------------------------------------------------------------------------------- void s_transstart(void) //---------------------------------------------------------------------------------- // generates a transmission start // _____ ________ // DATA: |_______| // ___ ___ // SCK : ___| |___| |______ { DATA_OutDIR(); Set_DATA(); Clear_CLK(); //DATA=1 SCK=0 Initial state _nop_(); Set_CLK(); //SCK=1 _nop_(); Clear_DATA(); //DATA=0 _nop_(); Clear_CLK(); //SCK=0 _nop_();_nop_();_nop_(); Set_CLK(); //SCK=1 _nop_(); Set_DATA(); //DATA=1 _nop_(); Clear_CLK(); //SCK=0 DATA_InDIR(); } //---------------------------------------------------------------------------------- void s_connectionreset(void) //---------------------------------------------------------------------------------- // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart // _____________________________________________________ ________ // DATA: |_______| // _ _ _ _ _ _ _ _ _ ___ ___ // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______ { unsigned char i; DATA_OutDIR(); Set_DATA(); Clear_CLK(); //DATA=1 SCK=0 Initial state for(i=0;i<9;i++) //9 SCK cycles { Set_CLK(); //SCK=1 Clear_CLK(); //SCK=0 } s_transstart(); //transmission start } //---------------------------------------------------------------------------------- char s_softreset(void) //---------------------------------------------------------------------------------- // resets the sensor by a softreset { unsigned char error=0; s_connectionreset(); //reset communication error+=s_write_byte(RESET); //send RESET-command to sensor return error; //error=1 in case of no response form the sensor } //---------------------------------------------------------------------------------- char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum) //---------------------------------------------------------------------------------- // reads the status register with checksum (8-bit) { unsigned char error=0; s_transstart(); //transmission start error=s_write_byte(STATUS_REG_R); //send command to sensor *p_value=s_read_byte(ACK); //read status register (8-bit) *p_checksum=s_read_byte(noACK); //read checksum (8-bit) return error; //error=1 in case of no response form the sensor } //---------------------------------------------------------------------------------- char s_write_statusreg(unsigned char *p_value) //---------------------------------------------------------------------------------- // writes the status register with checksum (8-bit) { unsigned char error=0; s_transstart(); //transmission start error+=s_write_byte(STATUS_REG_W);//send command to sensor error+=s_write_byte(*p_value); //send value of status register return error; //error>=1 in case of no response form the sensor } //---------------------------------------------------------------------------------- char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode) //---------------------------------------------------------------------------------- // makes a measurement (humidity/temperature) with checksum { unsigned error=0; unsigned int i; s_transstart(); //transmission start switch(mode) { //send command to sensor case TEMP : error+=s_write_byte(MEASURE_TEMP); break; case HUMI : error+=s_write_byte(MEASURE_HUMI); break; default : break; } DATA_InDIR(); for (i=0;i<65535;i++) if((P1IN&DATA)==0) break; //(DATA==0) wait until sensor has finished the measurement if((P1IN&DATA)!=0) error+=1; // (DATA) or timeout (~2 sec.) is reached *(p_value) =s_read_byte(ACK); //read the first byte (MSB) *(p_value+1)=s_read_byte(ACK); //read the second byte (LSB) *p_checksum =s_read_byte(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( float *p_humidity , float *p_temperature ) { const float C1=-4.0; // for 8 Bit const float C2=+0.648; // for 8 Bit const float C3=-0.0000072; // for 8 Bit const float D1=-39.6; // for 12 Bit [url=home.php?mod=space&uid=72445]@[/url] 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 *p_temperature=t_C; //return temperature [癈] *p_humidity=rh_true; //return humidity[%RH] wen=*p_temperature; shi=*p_humidity; } //uchar cTData[2]; //温度数据,2个字节 //uchar cHData[2]; //湿度数据,2个字节 // 温湿度传感器SHT10驱动程序开始
您需要 登录 才可以下载或查看,没有账号?注册
收藏0 举报
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
人才类勋章
时间类勋章
55
176
2
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注电源系统设计
扫码关注21ic项目外包
扫码浏览21ic手机版
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才
京公网安备 11010802024343号