各位大哥,最近用430F149与温湿度传感器SHT10相连测温湿度,老是读不出数据,麻烦大家帮忙看下程序,DATA:P20,SCLK:P21
#include <msp430x14x.h>
#include<math.h>
//*****************************************************************
// DHT90的预定义区
//*****************************************************************
#define noACK 0
#define ACK 1 //回答信号
//地址 命令
#define STATUS_REG_W 0x06 //000 0011 0
#define STATUS_REG_R 0x07 //000 0011 1
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
#define RESET 0x1e //000 1111 0
#define _nop_() _NOP();_NOP();_NOP();
#define SCK_H P2OUT|=BIT1
#define SCK_L P2OUT&=(~BIT1)
#define SDA_H P2OUT|=BIT0
#define SDA_L P2OUT&=(~BIT0)
#define READ (P2IN&BIT0)
float DHT90_Humility=0;
float DHT90_tempter=0;
int tempter;
int humility;
//***************************************************************************************
typedef union
{
unsigned int i;
float f;
} value;
enum {TEMP,HUMI}; ///枚举测量温度或湿度
void s_transstart(void); //传输启动
char s_read_byte(unsigned char ack);//读
char s_write_byte(unsigned char value);//写
void s_connectionreset(void);//
char s_softreset(void);//
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum);
char s_write_statusreg(unsigned char *p_value);//writes the status register with checksum (8-bit)
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);//makes a measurement (humidity/temperature) with checksum
void calc_sth11(float *p_humidity ,float *p_temperature);
float calc_dewpoint(float h,float t);
char humidity(float * humi_val_f,float * temp_val_f);
//***************************************************************************************
// 函数体定义区
//***************************************************************************************
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned char error=0;
unsigned int i;
s_transstart(); //传输启动
switch(mode) //send command to sensor
{
case TEMP : error+=s_write_byte(MEASURE_TEMP); break; //写命令 测量温度
case HUMI : error+=s_write_byte(MEASURE_HUMI); break; //写命令 测量湿度 error=1 没有回答信号
default : break;
}
P2DIR&=0xfe; //in
for (i=0;i<65535;i++)
if(READ==0)
break; //wait until sensor has finished the measurement
if(READ==1) error+=1; // or timeout (~2 sec.) is reached
else
{
*(p_value) =s_read_byte(ACK); //读高八位 (MSB)
*(p_value+1)=s_read_byte(ACK); //读低八位 (LSB)
*p_checksum =s_read_byte(noACK); //读 校验
}
return error;
}
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;
P2DIR|=BIT0; //out
SDA_H; //release DATA-line
P2DIR&=0xfe; //in
for (i=0x80;i>0;i/=2) //除法 就是右移
{
SCK_H;
if (READ) val=(val | i); //一次读一位数据
SCK_L;
}
// READ=!ack; //in case of "ack==1" pull down DATA-Line
P2DIR|=BIT0;
if(ack)
{
SDA_L; //?????????????????????????????
}
else
{
SDA_H;
}
_nop_();
SCK_H; //clk #9 for ack
_nop_(); // 5 us
SCK_L;
SDA_H; //拉高
return val;
}
void s_transstart(void)
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
SDA_H;
SCK_L; // transmission start 传输启动
_nop_();
SCK_H;
_nop_();
SDA_L;
_nop_();
SCK_L;
_nop_();
SCK_H;
_nop_();
SDA_H;
_nop_();
SCK_L;
_nop_();
}
//--------------------------------------------------------------------
void calc_sth11(float *p_humidity ,float *p_temperature) //数据处理
{
const float C1=-4.0; // for 12 Bit
const float C2=0.0405; // for 12 Bit
const float C3=-0.0000028; // for 12 Bit
const float T1=0.01; // for 14 Bit @ 5V
const float T2=0.00008; // for 14 Bit @ 5V
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*0.01 - 40.0; //calc. temperature from ticks to
rh_lin=C3*rh*rh + C2*rh + C1; //计算相对湿度
rh_true=(t_C-25.0)*(T1+T2*rh)+rh_lin; //温度补偿后的数据
if(rh_true>100.0)rh_true=100.0; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.10; //the physical possi××e range
*p_temperature=t_C; //返回温度值
*p_humidity=rh_true; //返回湿度值
}
//--------------------------------------------------------------------------------------
char s_write_byte(unsigned char value)// 写字节函数
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2)
{
if (i & value) SDA_H; //一次写一位数据
else SDA_L;
SCK_H;
_nop_();
SCK_L;
_nop_();
}
SDA_H; //release DATA-line
_nop_();
SCK_H; //clk #9 for ack
_nop_();
P2DIR&=0xfe; //in
_nop_();
_nop_();
error=READ; //检查回答信号
SCK_L;
_nop_();
return error; //error=1 没有回答信号
}
//----------------------------------------------------------------------------------
float calc_dewpoint(float h,float t)
{
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;
}
//--------------------------------------------------------------------------- -----
void s_connectionreset(void)
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
{
unsigned char i;
SDA_H;
_nop_();
_nop_();
_nop_();
SCK_L; //Initial state
_nop_();
_nop_();
_nop_();
for(i=0;i<9;i++) //9 SCK cycles
{
SCK_H;
_nop_();
_nop_();
_nop_();
SCK_L;
_nop_();
_nop_();
_nop_();
}
s_transstart(); //传输启动
}
////////////////////////////////////////////////////////////////////////////////////
char humidity(float * humi_val_f,float * temp_val_f)
{
value humi_val,temp_val; //湿度和温度值
// float dew_point;
unsigned char err,checksum;
unsigned int i=0;
s_connectionreset(); //连接复位
err=0;
err+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //测量湿度
err+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //测量温度
if(err!=0)
{
s_connectionreset(); //in case of an error: connection reset
}
else
{
humi_val.f=(float)humi_val.i;
temp_val.f=(float)temp_val.i; //数据类型转换
calc_sth11(&humi_val.f,&temp_val.f); //计算温度和湿度
// dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point?
* humi_val_f=humi_val.f;
* temp_val_f=temp_val.f;
return 1;
}
for (i=0;i<40000;i++); //(be sure that the compiler doesn't eliminate this line!)- wait approx. 0.8s to ××oid heating up SHTxx
return 0;
}
void main()
{
WDTCTL=WDTPW+WDTHOLD; //Stop watchdog timer to prevent time out reset
P2DIR=(BIT0|BIT1); // OUT 为常态
while(1)
{
humidity(&DHT90_Humility,&DHT90_tempter);
tempter=(int)(DHT90_tempter*100);
humility=(int)(DHT90_Humility*100);
}
}
楼主,你好!
我最近用的是SHT71+MSP430F247。
我觉得硬件比较重要,就是DAT线要加10K的上拉,而CLK线不能加上拉。
我的已经调试成功了。
能把你程序发我看下吗 我的邮箱是brank1986@163.COM,谢谢你了
就是DAT线要加10K的上拉,而CLK线不能加上拉。 -----------还有这事儿?
注意一下for(i《65535)的循环,可以加大些试试,那个dht90的程序是我写的,还有问题可以问我,QQ:309344129
真的,我用的就出这问题了,DAT10k上拉,SCK不能上拉呢。
回复 楼主 lovablehua 的帖子
你好,楼主,程序写出来了么?我现在正在搞这个,能帮帮忙吗?我的QQ:50816473