求助程序问题,sht11温度不显示数据,但是湿度显示
时间:10-02
整理:3721RD
点击:
#include <reg52.h>
#include <stdio.h>
#include <intrins.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
#define noACK 0
#define ACK 1
void delay(uint x); //毫秒延时函数
sbit DATA=P1^0;
sbit SCK=P1^1;
sbit LCD_RS=P2^5;
sbit LCD_RW=P2^6;
sbit LCD_EN=P2^7;
//SHT11写字节
uchar s_write_byte(uchar value)
{
uchar i,error=0;
for (i=0x80;i>0;i/=2) // //高位为1,循环右移
{
if (i & value) // 和要发送的数相与,结果为发送的位
DATA=1;
else
DATA=0;
SCK=1;
_nop_();
_nop_();
_nop_(); //延时3us
SCK=0;
}
DATA=1; //释放数据线
SCK=1;
error=DATA; //检查应答信号,确认通讯正常
SCK=0;
return error; //error=1 通讯错误
}
// SHT11读字节程序
uchar s_read_byte(uchar ack)
{
uchar i,val=0;
DATA=1; //释放数据线
for (i=0x80;i>0;i/=2)
{
SCK=1;
if (DATA) val=(val | i); //读一位数据线的值
SCK=0;
}
DATA=!ack; //如果是校验,读取完后结束通讯
SCK=1;
_nop_();
_nop_();
_nop_();
SCK=0;
DATA=1;
return val;
}
//SHT11启动传输
void s_transstart(void)
{
DATA=1;
SCK=0;
_nop_();
SCK=1;
_nop_();
DATA=0;
_nop_();
SCK=0;
_nop_();
_nop_();
_nop_();
SCK=1;
_nop_();
DATA=1;
_nop_();
SCK=0;
}
//连接复位
void s_connectionreset(void)
{
uchar i;
DATA=1;
SCK=0;
for(i=0;i<9;i++) //dat保持高,SCK时钟触发9次,发送启动传输,通迅即复位
{ SCK=1;
SCK=0;
}
s_transstart(); //启动传输
}
//SHT11温度检测
uchar measure_T()
{
uchar FH,FL,error=0;
uint i, tem;
s_connectionreset();
s_transstart();
error+=s_write_byte(0x03); ////测量温度
for (i=0;i<65535;i++)
if(DATA==0) break;
if(DATA) error+=1;
FH =s_read_byte(ACK); //读第一个字节,高字节 (MSB)
FL=s_read_byte(ACK); //读第二个字节,低字节 (LSB)
tem=FH*256+FL;
return tem;
}
//将检测到的数据转化为相应的温度数据
float c_T(uint temp_val) //将检测到的数据转化为相应的温度数据
{
uchar i;
const float d1=-40.0; // 14位温度精度 5V条件 修正公式
const float d2=+0.01; // 14位温度精度 5V条件 修正公式
float t_c;
temp_val=temp_val&0x3fff; //取低14位
t_c=d1+d2*(float)temp_val;
for(i=100;i>0;i--)
delay(1);
return t_c;
}
//SHT11湿度检测
uchar measure_H()
{
uchar FH,FL,error=0;
uint i, hum;
s_connectionreset();
s_transstart();
error+=s_write_byte(0x05); //测量湿度
for (i=0;i<65535;i++)
if(DATA==0) break;
if(DATA) error+=1;
FH =s_read_byte(ACK); //读第一个字节,高字节 (MSB)
FL=s_read_byte(ACK); //读第二个字节,低字节 (LSB)
hum=FH*256+FL;
return hum;
}
//检测到的数据转化为相应的湿度数据
float c_H(float t,float h)
{
uchar i;
const float C1=-4.0; // 12位湿度精度 修正公式
const float C2=+0.0405; // 12位湿度精度 修正公式
const float C3=-0.0000028; // 12位湿度精度 修正公式
const float T1=+0.01; // 14位温度精度 5V条件 修正公式
const float T2=+0.00008; // 14位温度精度 5V条件 修正公式
float rh=h;
float rh_lin; // rh_lin: 湿度 linear值
float rh_true; // rh_true: 湿度 ture值
float t_C; // t_C : 温度 ℃
t_C=t*0.01-40; //补偿温度
rh_lin=C3*rh*rh + C2*rh + C1; //相对湿度非线性补偿
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //相对湿度对于温度依赖性补偿
if(rh_true>100)
rh_true=100; //湿度最大修正
if(rh_true<0.1)
rh_true=0.1; //湿度最小修正
for(i=100;i>0;i--)
delay(1);
return rh_true;
}
//MS延时函数
void delay(uint x)
{
uint i,j;
for(i=0;i<x;i++)
for(j=0;j<112;j++);
}
//LCD1602写指令
void write_com(uchar com)
{
LCD_RS=0;
LCD_RW=0;
P0=com;
delay(5);
LCD_EN=1;
delay(5);
LCD_EN=0;
}
//LCD1602写数据
void write_dat(uchar dat)
{
LCD_RS=1;
LCD_RW=0;
P0=dat;
LCD_EN=1;
delay(5);
LCD_EN=0;
}
//LCD1602初始化
void LCD_init()
{
LCD_EN=0;
write_com(0x38); //8位数据端口,16*2行显示,5*7点阵
write_com(0x0c); //显示开,关光标
write_com(0x06); //移动光标
write_com(0x01); //清除LCD的显示内容
delay(5);
}
//温度显示
void dipaly_T(float temperature)
{
float temp;
int t_qian,t_bai,t_shi,t_ge;
temp=temperature*100;
t_qian=(int)temp/1000;
t_bai =(int)temp%1000/100;
t_shi =(int)temp%100/10;
t_ge =(int)temp%10;
write_com(0x80+0x06);
write_dat(0x30+t_qian);
delay(10);
write_com(0x80+0x07);
write_dat(0x30+t_bai);
delay(10);
write_com(0x80+0x09);
write_dat(0x30+t_shi);
delay(10);
write_com(0x80+0x10);
write_dat(0x30+t_ge);
delay(10);
}
//湿度显示
void dipaly_H(float humidity)
{
float hum;
int h_qian,h_bai,h_shi,h_ge;
hum=humidity*100;
h_qian=(int)hum/1000;
h_bai =(int)hum%1000/100;
h_shi =(int)hum%100/10;
h_ge =(int)hum%10;
write_com(0x80+0X46);
write_dat(0x30+h_qian);
delay(10);
write_com(0x80+0x47);
write_dat(0x30+h_bai);
delay(10);
write_com(0x80+0x48);
write_dat(0x30+h_shi);
delay(10);
write_com(0x80+0x50);
write_dat(0x30+h_ge);
delay(10);
}
void main()
{
float dis_tempval,dis_humival;
uint tempval,humival;
uchar error;
LCD_init();
while(1)
{
tempval=measure_T();
humival=measure_H();
if(error==0)
{
delay(500);
dis_tempval=c_T(tempval);
dipaly_T(dis_tempval);
dis_humival=c_H(dis_tempval,humival);
dipaly_H(dis_humival);
}
else
continue;
}
}
#include <stdio.h>
#include <intrins.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
#define noACK 0
#define ACK 1
void delay(uint x); //毫秒延时函数
sbit DATA=P1^0;
sbit SCK=P1^1;
sbit LCD_RS=P2^5;
sbit LCD_RW=P2^6;
sbit LCD_EN=P2^7;
//SHT11写字节
uchar s_write_byte(uchar value)
{
uchar i,error=0;
for (i=0x80;i>0;i/=2) // //高位为1,循环右移
{
if (i & value) // 和要发送的数相与,结果为发送的位
DATA=1;
else
DATA=0;
SCK=1;
_nop_();
_nop_();
_nop_(); //延时3us
SCK=0;
}
DATA=1; //释放数据线
SCK=1;
error=DATA; //检查应答信号,确认通讯正常
SCK=0;
return error; //error=1 通讯错误
}
// SHT11读字节程序
uchar s_read_byte(uchar ack)
{
uchar i,val=0;
DATA=1; //释放数据线
for (i=0x80;i>0;i/=2)
{
SCK=1;
if (DATA) val=(val | i); //读一位数据线的值
SCK=0;
}
DATA=!ack; //如果是校验,读取完后结束通讯
SCK=1;
_nop_();
_nop_();
_nop_();
SCK=0;
DATA=1;
return val;
}
//SHT11启动传输
void s_transstart(void)
{
DATA=1;
SCK=0;
_nop_();
SCK=1;
_nop_();
DATA=0;
_nop_();
SCK=0;
_nop_();
_nop_();
_nop_();
SCK=1;
_nop_();
DATA=1;
_nop_();
SCK=0;
}
//连接复位
void s_connectionreset(void)
{
uchar i;
DATA=1;
SCK=0;
for(i=0;i<9;i++) //dat保持高,SCK时钟触发9次,发送启动传输,通迅即复位
{ SCK=1;
SCK=0;
}
s_transstart(); //启动传输
}
//SHT11温度检测
uchar measure_T()
{
uchar FH,FL,error=0;
uint i, tem;
s_connectionreset();
s_transstart();
error+=s_write_byte(0x03); ////测量温度
for (i=0;i<65535;i++)
if(DATA==0) break;
if(DATA) error+=1;
FH =s_read_byte(ACK); //读第一个字节,高字节 (MSB)
FL=s_read_byte(ACK); //读第二个字节,低字节 (LSB)
tem=FH*256+FL;
return tem;
}
//将检测到的数据转化为相应的温度数据
float c_T(uint temp_val) //将检测到的数据转化为相应的温度数据
{
uchar i;
const float d1=-40.0; // 14位温度精度 5V条件 修正公式
const float d2=+0.01; // 14位温度精度 5V条件 修正公式
float t_c;
temp_val=temp_val&0x3fff; //取低14位
t_c=d1+d2*(float)temp_val;
for(i=100;i>0;i--)
delay(1);
return t_c;
}
//SHT11湿度检测
uchar measure_H()
{
uchar FH,FL,error=0;
uint i, hum;
s_connectionreset();
s_transstart();
error+=s_write_byte(0x05); //测量湿度
for (i=0;i<65535;i++)
if(DATA==0) break;
if(DATA) error+=1;
FH =s_read_byte(ACK); //读第一个字节,高字节 (MSB)
FL=s_read_byte(ACK); //读第二个字节,低字节 (LSB)
hum=FH*256+FL;
return hum;
}
//检测到的数据转化为相应的湿度数据
float c_H(float t,float h)
{
uchar i;
const float C1=-4.0; // 12位湿度精度 修正公式
const float C2=+0.0405; // 12位湿度精度 修正公式
const float C3=-0.0000028; // 12位湿度精度 修正公式
const float T1=+0.01; // 14位温度精度 5V条件 修正公式
const float T2=+0.00008; // 14位温度精度 5V条件 修正公式
float rh=h;
float rh_lin; // rh_lin: 湿度 linear值
float rh_true; // rh_true: 湿度 ture值
float t_C; // t_C : 温度 ℃
t_C=t*0.01-40; //补偿温度
rh_lin=C3*rh*rh + C2*rh + C1; //相对湿度非线性补偿
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //相对湿度对于温度依赖性补偿
if(rh_true>100)
rh_true=100; //湿度最大修正
if(rh_true<0.1)
rh_true=0.1; //湿度最小修正
for(i=100;i>0;i--)
delay(1);
return rh_true;
}
//MS延时函数
void delay(uint x)
{
uint i,j;
for(i=0;i<x;i++)
for(j=0;j<112;j++);
}
//LCD1602写指令
void write_com(uchar com)
{
LCD_RS=0;
LCD_RW=0;
P0=com;
delay(5);
LCD_EN=1;
delay(5);
LCD_EN=0;
}
//LCD1602写数据
void write_dat(uchar dat)
{
LCD_RS=1;
LCD_RW=0;
P0=dat;
LCD_EN=1;
delay(5);
LCD_EN=0;
}
//LCD1602初始化
void LCD_init()
{
LCD_EN=0;
write_com(0x38); //8位数据端口,16*2行显示,5*7点阵
write_com(0x0c); //显示开,关光标
write_com(0x06); //移动光标
write_com(0x01); //清除LCD的显示内容
delay(5);
}
//温度显示
void dipaly_T(float temperature)
{
float temp;
int t_qian,t_bai,t_shi,t_ge;
temp=temperature*100;
t_qian=(int)temp/1000;
t_bai =(int)temp%1000/100;
t_shi =(int)temp%100/10;
t_ge =(int)temp%10;
write_com(0x80+0x06);
write_dat(0x30+t_qian);
delay(10);
write_com(0x80+0x07);
write_dat(0x30+t_bai);
delay(10);
write_com(0x80+0x09);
write_dat(0x30+t_shi);
delay(10);
write_com(0x80+0x10);
write_dat(0x30+t_ge);
delay(10);
}
//湿度显示
void dipaly_H(float humidity)
{
float hum;
int h_qian,h_bai,h_shi,h_ge;
hum=humidity*100;
h_qian=(int)hum/1000;
h_bai =(int)hum%1000/100;
h_shi =(int)hum%100/10;
h_ge =(int)hum%10;
write_com(0x80+0X46);
write_dat(0x30+h_qian);
delay(10);
write_com(0x80+0x47);
write_dat(0x30+h_bai);
delay(10);
write_com(0x80+0x48);
write_dat(0x30+h_shi);
delay(10);
write_com(0x80+0x50);
write_dat(0x30+h_ge);
delay(10);
}
void main()
{
float dis_tempval,dis_humival;
uint tempval,humival;
uchar error;
LCD_init();
while(1)
{
tempval=measure_T();
humival=measure_H();
if(error==0)
{
delay(500);
dis_tempval=c_T(tempval);
dipaly_T(dis_tempval);
dis_humival=c_H(dis_tempval,humival);
dipaly_H(dis_humival);
}
else
continue;
}
}
你的程序应该是网上的资料,理论上可能时序会有区别,你去确认一下,然后就是电路图你有没有按照说明接上拉电阻,可以用示波器看一下,如果没有10K电阻可以把输出方式改为推挽输出。
