开始写状态寄存器的值,然后读 是我写入的值 但是温湿度的数据很大 反反复复改了几次 还是没用 求大佬帮忙解决下 下面是我的程序:
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++) //´¥·¢SCKʱÖÓ9´Î
{
SHT10_SCK = 1;
SHT10_Dly();
SHT10_SCK = 0;
SHT10_Dly();
}
SHT10_TransStart();
}
/*************************************************************
Function :SHT10_SoftReset
Description:Èí¼þ¸´Î»
return : err: 0-ÕýÈ· 1-´íÎó
*************************************************************/
u8 SHT10_SoftReset(void)
{
u8 err = 0;
SHT10_ConReset(); //ͨѶ¸´Î»
err += SHT10_WriteByte(SOFTRESET); //д¸´Î»Ö¸Áî
return err;
}
/*************************************************************
Description:¶Á״̬¼Ä´æÆ÷
Input : p_value-¶Áµ½µÄÊý¾Ý;p_checksun-¶Áµ½µÄУÑéÊý¾Ý
return : err: 0-ÕýÈ· 1-´íÎó
*************************************************************/
u8 SHT10_ReadStatusReg(u8 *p_value, u8 *p_checksum)
{
u8 err = 0;
SHT10_TransStart(); //¿ªÊ¼´«Êä
err = SHT10_WriteByte(STATUS_REG_R);
*p_value = SHT10_ReadByte(ACK); //¶Á״̬¼Ä´æÆ÷
*p_checksum = SHT10_ReadByte(noACK); //¶ÁÈ¡¼ìÑé
return err;
}
/*************************************************************
Description:д״̬¼Ä´æÆ÷
Input : p_value-ҪдÈëµÄÊý¾ÝÖµ
return : err: 0-ÕýÈ· 1-´íÎó
*************************************************************/
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:´Óζȴ«¸ÐÆ÷¶ÁȡζÈ
Input : p_value-¶Áµ½µÄÊý¾Ý;p_checksum-¶ÁÈ¡µÄУÑéÊý
mode:Ñ¡ÔñT»òÕßRH
return : err: 0-ÕýÈ· 1-´íÎó
************************************************************/
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:¼ÆËãʪζȵÄÖµ
Input : t-´Ó´«¸ÐÆ÷¶Á³öµÄζÈÖµ;rh-´Ó´«¸ÐÆ÷¶Á³öµÄʪ¶ÈÖµ
p_humidity-¸ù¾Ý¹«Ê½¼ÆËãʵ¼Êʪ¶ÈÖµ;p_temperature-¸ù¾Ý¹«Ê½¼ÆËãʵ¼ÊζÈÖµ (ʪζȲ¹³¥)
*************************************************************/
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ÏßÐÔÖµ
float RH_Ture; //RHÕæʵֵ
float temp_C;
temp_C = d1 + d2 * t; //¼ÆËãζÈÖµ
RH_Lin = C1 + C2 * rh + C3 * rh * rh; //¼ÆËãʪ¶ÈÖµ
RH_Ture = (temp_C -25) * (T1 + T2 * rh) + RH_Lin; //ʪ¶ÈµÄζȲ¹³¥£¬¼ÆËãʵ¼Êʪ¶ÈÖµ
if(RH_Ture > 100) //ÉèÖÃʪ¶ÈÉÏÏÞ
{RH_Ture = 100;}
if(RH_Ture < 0.1)
{RH_Ture = 0.1;} //ÉèÖÃʪ¶ÈÏÂÏÞ
*p_humidity = RH_Ture;
*p_temperature = temp_C;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
捣鼓了一天 公司又让我去做其他的项目 现在又来调试了
亲测,延时是精确延时,器件是SHT15
一周热门 更多>