跪求SHT10能用的,调试了一天,还是没有结果

2019-07-20 23:34发布

开始写状态寄存器的值,然后读  是我写入的值 但是温湿度的数据很大   反反复复改了几次  还是没用  求大佬帮忙解决下  下面是我的程序:
void SHT10_Dly(void)
{
u16 i;
for(i = 500; i > 0; i--);
}
//配置SHT10 IO口
void SHT10_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);

SHT10_ConReset();
}

//设置SDA为输出模式
void SHT10_DATAOut(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}

//设置SDA为输入模式
void SHT10_DATAIn(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}

//写8位数据
//value:要写入的数据
//return err:0-正确(从机应答)  1-错误(从机无应答)
u8 SHT10_WriteByte(u8 value)
{
u8 i, err = 0;
SHT10_DATAOut();
for(i = 0x80; i > 0; i /= 2)
{
   if(value&i)
   {SHT10_Outdata = 1;}
   else
   {SHT10_Outdata = 0;}
   SHT10_Dly();
   SHT10_SCK = 1;
   SHT10_Dly();
   SHT10_SCK = 0;
   SHT10_Dly();
}
SHT10_DATAIn();
SHT10_SCK = 1;
err = SHT10_Inputdata;
SHT10_SCK = 0;
return err;
}
//读8位数据
//Ack:1-选择8位数据CRC8校验 0-选择SDA输出高电平(主机应答),中止结束
u8 SHT10_ReadByte(u8 Ack)
{
u8 i, val = 0;
SHT10_DATAIn();
for(i = 0x80; i > 0; i /= 2)
{
  SHT10_Dly();
  SHT10_SCK = 1;
  SHT10_Dly();
  if(SHT10_Inputdata)
  {val |= 0x01;}
  SHT10_SCK = 0;
  }
  SHT10_DATAOut();
  if(Ack)
  {SHT10_Outdata = 0;}    //Ó¦´ð,Ôò»á¶ÁCRC8УÑéÊý¾Ý
  else
  {SHT10_Outdata = 1;}    //²»´ðÓ¦,Êý¾ÝÖÁ´Ë½áÊø
  SHT10_Dly();
  SHT10_SCK = 1;
  SHT10_Dly();
  SHT10_SCK = 0;
  SHT10_Dly();
  return val;
}

/*************************************************************
Description开始传输信号,时序如下:
_____ ________
DATA: |_______|
___ ___
SCK : ___| |___| |______
*************************************************************/
void SHT10_TransStart(void)
{
  SHT10_DATAOut();
  SHT10_Outdata = 1;
  SHT10_SCK = 0;
  SHT10_Dly();
  SHT10_SCK = 1;
  SHT10_Dly();
  SHT10_Outdata = 0;
  SHT10_Dly();
  SHT10_SCK = 0;
  SHT10_Dly();
  SHT10_SCK = 1;
  SHT10_Dly();
  SHT10_Outdata = 1;
  SHT10_Dly();
  SHT10_SCK = 0;
}
/*************************************************************
Function :SHT10_ConReset
Description:通信复位,时序如下:
_____________________________________________________ ________
DATA: |_______|
_ _ _ _ _ _ _ _ _ ___ ___
SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
*************************************************************/
void SHT10_ConReset(void)
{
u8 i;
SHT10_DATAOut();
SHT10_Outdata = 1;
SHT10_SCK = 0;
for(i = 0; i < 9; i++)   //&acute;&yen;·&cent;SCK&Ecirc;±&Ouml;&Oacute;9&acute;&Icirc;
{
  SHT10_SCK = 1;
  SHT10_Dly();
  SHT10_SCK = 0;
  SHT10_Dly();
}
SHT10_TransStart();
}
/*************************************************************
Function :SHT10_SoftReset
Description:&Egrave;í&frac14;&thorn;&cedil;&acute;&Icirc;&raquo;
return : err: 0-&Otilde;&yacute;&Egrave;·  1-&acute;í&Icirc;ó
*************************************************************/
u8 SHT10_SoftReset(void)
{
u8 err = 0;
SHT10_ConReset(); //&Iacute;¨&Ntilde;&para;&cedil;&acute;&Icirc;&raquo;
err += SHT10_WriteByte(SOFTRESET); //&ETH;&acute;&cedil;&acute;&Icirc;&raquo;&Ouml;&cedil;&Aacute;&icirc;
return err;
}
/*************************************************************
Description:&para;&Aacute;×&acute;&Igrave;&not;&frac14;&Auml;&acute;&aelig;&AElig;÷
Input : p_value-&para;&Aacute;&micro;&frac12;&micro;&Auml;&Ecirc;&yacute;&frac34;&Yacute;;p_checksun-&para;&Aacute;&micro;&frac12;&micro;&Auml;&ETH;&pound;&Ntilde;é&Ecirc;&yacute;&frac34;&Yacute;
return : err: 0-&Otilde;&yacute;&Egrave;· 1-&acute;í&Icirc;ó
*************************************************************/
u8 SHT10_ReadStatusReg(u8 *p_value, u8 *p_checksum)
{
u8 err = 0;
SHT10_TransStart();  //&iquest;&ordf;&Ecirc;&frac14;&acute;&laquo;&Ecirc;&auml;
err = SHT10_WriteByte(STATUS_REG_R);
*p_value = SHT10_ReadByte(ACK);   //&para;&Aacute;×&acute;&Igrave;&not;&frac14;&Auml;&acute;&aelig;&AElig;÷
*p_checksum = SHT10_ReadByte(noACK); //&para;&Aacute;&Egrave;&iexcl;&frac14;ì&Ntilde;é
return err;
}
/*************************************************************
Description:&ETH;&acute;×&acute;&Igrave;&not;&frac14;&Auml;&acute;&aelig;&AElig;÷
Input : p_value-&Ograve;&ordf;&ETH;&acute;&Egrave;&euml;&micro;&Auml;&Ecirc;&yacute;&frac34;&Yacute;&Ouml;&micro;
return : err: 0-&Otilde;&yacute;&Egrave;·  1-&acute;í&Icirc;ó
*************************************************************/
u8 SHT10_WriteStatusReg(u8 *p_value)
{
u8 err = 0;
SHT10_TransStart();
err += SHT10_WriteByte(STATUS_REG_W);
err += SHT10_WriteByte(*p_value);
return err;
}
/*************************************************************
Description:&acute;&Oacute;&Icirc;&Acirc;&para;&Egrave;&acute;&laquo;&cedil;&ETH;&AElig;÷&para;&Aacute;&Egrave;&iexcl;&Icirc;&Acirc;&para;&Egrave;
Input : p_value-&para;&Aacute;&micro;&frac12;&micro;&Auml;&Ecirc;&yacute;&frac34;&Yacute;;p_checksum-&para;&Aacute;&Egrave;&iexcl;&micro;&Auml;&ETH;&pound;&Ntilde;é&Ecirc;&yacute;
mode:&Ntilde;&iexcl;&Ocirc;&ntilde;T&raquo;ò&Otilde;&szlig;RH
return : err: 0-&Otilde;&yacute;&Egrave;·  1-&acute;í&Icirc;ó
************************************************************/
u8 SHT10_Measure(u16 *p_value, u8 *p_checksum, u8 mode)
{
u8 err = 0,i;
u8 value_H = 0;
u8 value_L = 0;
SHT10_TransStart();
switch(mode)
{
  case TEMP:
  err += SHT10_WriteByte(MEASURE_TEMP);
  break;
  case HUMI:
  err += SHT10_WriteByte(MEASURE_HUMI);
  break;
  default:
  break;
}
SHT10_DATAIn();
for (i=0;i<72000000;i++)   
{
  if(SHT10_Inputdata==0)
    {break; }
}
if(SHT10_Inputdata == 1)
{err += 1;}
value_H = SHT10_ReadByte(ACK);
value_L = SHT10_ReadByte(ACK);
*p_checksum = SHT10_ReadByte(noACK);
*p_value = (value_H << 8) | value_L;
return err;
}
/*************************************************************
Description:&frac14;&AElig;&Euml;&atilde;&Ecirc;&ordf;&Icirc;&Acirc;&para;&Egrave;&micro;&Auml;&Ouml;&micro;
Input : t-&acute;&Oacute;&acute;&laquo;&cedil;&ETH;&AElig;÷&para;&Aacute;&sup3;&ouml;&micro;&Auml;&Icirc;&Acirc;&para;&Egrave;&Ouml;&micro;;rh-&acute;&Oacute;&acute;&laquo;&cedil;&ETH;&AElig;÷&para;&Aacute;&sup3;&ouml;&micro;&Auml;&Ecirc;&ordf;&para;&Egrave;&Ouml;&micro;
p_humidity-&cedil;ù&frac34;&Yacute;&sup1;&laquo;&Ecirc;&frac12;&frac14;&AElig;&Euml;&atilde;&Ecirc;&micro;&frac14;&Ecirc;&Ecirc;&ordf;&para;&Egrave;&Ouml;&micro;;p_temperature-&cedil;ù&frac34;&Yacute;&sup1;&laquo;&Ecirc;&frac12;&frac14;&AElig;&Euml;&atilde;&Ecirc;&micro;&frac14;&Ecirc;&Icirc;&Acirc;&para;&Egrave;&Ouml;&micro; (&Ecirc;&ordf;&Icirc;&Acirc;&para;&Egrave;&sup2;&sup1;&sup3;&yen;)
*************************************************************/
void SHT10_Calculate(u16 t, u16 rh, float *p_temperature, float *p_humidity)
{
const float d1 = -39.6;
const float d2 = +0.01;
const float C1 = -2.0468;
const float C2 = +0.0367;
const float C3 = -0.0000015955;
const float T1 = +0.01;
const float T2 = +0.00008;
float RH_Lin;  //RH&Iuml;&szlig;&ETH;&Ocirc;&Ouml;&micro;
float RH_Ture; //RH&Otilde;&aelig;&Ecirc;&micro;&Ouml;&micro;
float temp_C;
temp_C = d1 + d2 * t;   //&frac14;&AElig;&Euml;&atilde;&Icirc;&Acirc;&para;&Egrave;&Ouml;&micro;
RH_Lin = C1 + C2 * rh + C3 * rh * rh; //&frac14;&AElig;&Euml;&atilde;&Ecirc;&ordf;&para;&Egrave;&Ouml;&micro;
RH_Ture = (temp_C -25) * (T1 + T2 * rh) + RH_Lin;  //&Ecirc;&ordf;&para;&Egrave;&micro;&Auml;&Icirc;&Acirc;&para;&Egrave;&sup2;&sup1;&sup3;&yen;&pound;&not;&frac14;&AElig;&Euml;&atilde;&Ecirc;&micro;&frac14;&Ecirc;&Ecirc;&ordf;&para;&Egrave;&Ouml;&micro;
if(RH_Ture > 100)  //&Eacute;è&Ouml;&Atilde;&Ecirc;&ordf;&para;&Egrave;&Eacute;&Iuml;&Iuml;&THORN;
{RH_Ture = 100;}
if(RH_Ture < 0.1)
{RH_Ture = 0.1;}   //&Eacute;è&Ouml;&Atilde;&Ecirc;&ordf;&para;&Egrave;&Iuml;&Acirc;&Iuml;&THORN;
*p_humidity = RH_Ture;
*p_temperature = temp_C;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
15条回答
peng1554
1楼-- · 2019-07-20 23:34
 精彩回答 2  元偷偷看……
程功之道
2楼-- · 2019-07-21 03:28
peng1554 发表于 2019-6-12 18:17
论坛这边记得有人分享 可以搜搜

论坛的搜了  试了   也没用
peng1554
3楼-- · 2019-07-21 04:44
程功之道 发表于 2019-6-12 18:20
论坛的搜了  试了   也没用

好吧~~
lidp000
4楼-- · 2019-07-21 10:23
 精彩回答 2  元偷偷看……
程功之道
5楼-- · 2019-07-21 11:21
lidp000 发表于 2019-6-14 11:16
1、SHT官方的程序就可以2、程序中的延时要挺严的

这个不是标准的I2C  用逻辑分析仪抓   也没法看  好的  我去看看
lidp000
6楼-- · 2019-07-21 12:05
程功之道 发表于 2019-6-14 17:08
这个不是标准的I2C  用逻辑分析仪抓   也没法看  好的  我去看看

你的延时时间争取和自带历程的延时时间一样就行,确实不是标准的IIC

一周热门 更多>