今天继续测试,用上面的代码也是不能成功,怀疑传感器坏了,接到51板子上,温湿度正常。因此还是代码问题。
下面 发代码和测试方法:
/***********************************************************************************
Project: SHT11 demo program (V2.0)
Filename: SHT11.c
Prozessor: st,m32 family
Compiler: Keil Version 6.14
Autor: MST
Copyrigth: (c) Sensirion AG
***********************************************************************************/
#define SHT11_DATA_PIN GPIO_Pin_5
#define SHT11_DATA_PORT GPIOA
#define SHT11_DATA_GPIO_CLK RCC_AHBPeriph_GPIOA
#define SHT11_DATA_SOURCE GPIO_PinSource5
#define SHT11_CLK_PIN GPIO_Pin_4
#define SHT11_CLK_PORT GPIOA
#define SHT11_CLK_GPIO_CLK RCC_AHBPeriph_GPIOA
#define SHT11_CLK_SOURCE GPIO_PinSource4
typedef union
{ unsigned int i;
float f;
} value;
#define SHT11_DATA_H() GPIO_SetBits(SHT11_DATA_PORT,SHT11_DATA_PIN)
#define SHT11_DATA_L() GPIO_ResetBits(SHT11_DATA_PORT,SHT11_DATA_PIN)
#define SHT11_DATA_R() GPIO_ReadInputDataBit(SHT11_DATA_PORT,SHT11_DATA_PIN)
#define SHT11_CLK_H() GPIO_SetBits(SHT11_CLK_PORT,SHT11_CLK_PIN)
#define SHT11_CLK_L() GPIO_ResetBits(SHT11_CLK_PORT,SHT11_CLK_PIN)
enum {TEMP,HUMI};
#define noACK 0
#define ACK 1
//adr command r/w
#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
//毫秒级的延时
void delay_uS(uint16 time)
{
uint16 i=0;
while(time--)
{
__nop();__nop();
//for(i = 10; i> 0;i -- );
}
}
void delay_ms(uint16 time)
{
uint16 i=0;
while(time--)
{
for(i = 12000; i> 0;i -- );
}
}
//----------------------------------------------------------------------------------
// modul-var
//----------------------------------------------------------------------------------
void Init_Sht11_Gpio(void)
{
//SHT11 CLK输出,//SHT11 dat 输出
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIO Periph clock enable */
RCC_AHBPeriphClockCmd(SHT11_DATA_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = SHT11_DATA_PIN ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(SHT11_DATA_PORT, &GPIO_InitStructure);
RCC_AHBPeriphClockCmd(SHT11_CLK_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = SHT11_CLK_PIN ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(SHT11_CLK_PORT, &GPIO_InitStructure);
}
void SHT11_ClkPin_OutPut(char dat)
{
if(dat)
{
GPIO_SetBits(SHT11_CLK_PORT,SHT11_CLK_PIN);
}
else
{
GPIO_ResetBits(SHT11_CLK_PORT,SHT11_CLK_PIN);
}
}
void SHT11_DataPin_Out(void)
{
GPIO_InitTypeDef GPIO_InitStructure1;
GPIO_InitStructure1.GPIO_Pin = SHT11_DATA_PIN;
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure1.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure1.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(SHT11_DATA_PORT, &GPIO_InitStructure1);
}
void SHT11_DataPin_in(void)
{
GPIO_InitTypeDef GPIO_InitStructure1;
char res = 0;
/* DATA DIR INPUT */
RCC_AHBPeriphClockCmd(SHT11_DATA_GPIO_CLK, ENABLE);
GPIO_InitStructure1.GPIO_Pin = SHT11_DATA_PIN;
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure1.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure1.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(SHT11_DATA_PORT, &GPIO_InitStructure1);
}
//----------------------------------------------------------------------------------
char s_write_byte(unsigned char value)
//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
unsigned char i,error=0;
SHT11_DataPin_Out();
for (i=0x80;i>0;i/=2) //shift bit for masking
{
if (i & value)
{
SHT11_DATA_H();//DATA=1; //masking value with i , write to SENSI-BUS
}
else
{
SHT11_DATA_L();//DATA=0;
}
delay_uS(10);
SHT11_CLK_H();//clk for SENSI-BUS
delay_uS(30);//_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SHT11_CLK_L();//SCK=0;
delay_uS(10);
}
SHT11_DATA_H();
delay_uS(10);
SHT11_DataPin_in();
SHT11_CLK_H(); //release DATA-line
error=SHT11_DATA_R();//error=DATA; //check ack (DATA will be pulled down by SHT11)
SHT11_CLK_L();//SCK=0;
return error; //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;
char res = 0;
SHT11_DataPin_Out();
SHT11_DATA_H();//release DATA-line
SHT11_DataPin_in();
for (i=0x80;i>0;i/=2) //shift bit for masking
{
SHT11_CLK_H(); //clk for SENSI-BUS
if (SHT11_DATA_R())
val=(val | i); //read bit
SHT11_CLK_L();//SCK=0;
}
SHT11_DataPin_Out();
if(ack)
SHT11_DATA_L();
else
SHT11_DATA_H();
delay_uS(10);
SHT11_CLK_H();//SCK=1; //clk #9 for ack
delay_uS(30);//_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SHT11_CLK_L();//SCK=0;
delay_uS(10);
SHT11_DATA_H();
return val;
}
//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// __ __
// SCK : _ ___| |___ | |______
{
char j ;
SHT11_DataPin_Out();
SHT11_DATA_H();//DATA=1;
SHT11_CLK_L();//SCK=0; //Initial state
delay_uS(10);//_nop_();
SHT11_CLK_H();//SCK=1;
delay_uS(10);//_nop_();
SHT11_DATA_L();//DATA=0;
delay_uS(10);//_nop_();
SHT11_CLK_L();//SCK=0;
delay_uS(30);//_nop_();_nop_();_nop_();
SHT11_CLK_H();//SCK=1;
delay_uS(10);//_nop_();
SHT11_DATA_H();//DATA=1;
delay_uS(10);//_nop_();
SHT11_CLK_L();//SCK=0;
}
//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
SHT11_DataPin_Out();
SHT11_DATA_H();//DATA=1;
SHT11_CLK_L();//Initial state
for(i=0;i<9;i++) //9 SCK cycles
{
SHT11_CLK_H();//SCK=1;
delay_uS(10);
SHT11_CLK_L();//SCK=0;
delay_uS(10);
}
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
}
long int count;
//----------------------------------------------------------------------------------
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
{
unsigned error=0;
char dat = 0;
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;
}
SHT11_DataPin_in();//while(Read_SHT11_Data_Value());
for (count=0;count<0xffffffff;count++)
{
//delay_uS(1);
if(SHT11_DATA_R()==0)
break;
}
//wait until sensor has finished the measurement
dat = SHT11_DATA_R();
if(dat)
error+=1; // 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 first byte (MSB)
*p_checksum =s_read_byte(noACK); //read checksum
return error;
}
void calc_sth11(float *p_humidity ,float *p_temperature)
//----------------------------------------------------------------------------------------
// calculates temperature [癈] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [癈]
{ const float C1=-2.0468; // for 12 Bit RH
const float C2=+0.0367; // for 12 Bit RH
const float C3=-0.0000015955; // for 12 Bit RH
const float T1=+0.01; // for 12 Bit RH
const float T2=+0.00008; // for 12 Bit RH
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.1; //calc. temperature [癈] from 14 bit temp. ticks @ 5V
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]
}
//--------------------------------------------------------------------
float calc_dewpoint(float h,float t)
//--------------------------------------------------------------------
// calculates dew point
// input: humidity [%RH], temperature [癈]
// output: dew point [癈]
{ float k,dew_point ;
k = (log10(h)-2)/0.4343 + (17.62*t)/(243.12+t);
dew_point = 243.12*k/(17.62-k);
return dew_point;
}
value humi_val,temp_val;
//----------------------------------------------------------------------------------
void GetSht11Value(void)
//----------------------------------------------------------------------------------
// 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 [C]
// 4. calculate dew point [C]
// 5. print temperature, humidity, dew point
{
float dew_point;
unsigned char error,checksum;
unsigned int i;
s_connectionreset();
// while(1)
{ error=0;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
if(error!=0)
{
s_connectionreset(); //in case of an error: connection reset
}
else
{
humi_val.f=(float)humi_val.i; //converts integer to float
temp_val.f=(float)temp_val.i; //converts integer to float
calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
}
for (i=0;i<40000;i++); //(be sure that the compiler doesn't eliminate this line!)
}
}
|