📄 sht75.c
字号:
#include "includes.h"
/***********************************************************************************
项目名称: SHT15_SHT75温湿度一体化传感器实验
微处理器: P89C58X2
C编译器 : Keil V7.50
实现功能: 以大约1秒钟为周期,测量温度,湿度,计算露点,并最终将这三个参数以9600
波特率通过485总线送给上位机。
作 者: AMO
Email : amo73@126.com
***********************************************************************************/
//#include "commond.h"
//#include "SHT15_SHT75.h"
//#include "delay.h"
//#include <math.h>
//#include <stdio.h>
//sbit LED_RXD = P2^4; //串行接收指示灯
//sbit LED_TXD = P2^5; //串行发送指示灯
/*
sbit KEY_0 = P1^0; //拨码开关0
sbit KEY_1 = P1^1; //拨码开关1
sbit KEY_2 = P1^2; //拨码开关2
sbit KEY_3 = P1^3; //拨码开关3
sbit KEY_4 = P1^4; //拨码开关4
sbit KEY_5 = P1^5; //拨码开关5
sbit KEY_6 = P1^6; //拨码开关6
sbit KEY_7 = P1^7; //拨码开关7
sbit EN_75LBC184 = P3^5; //485总线驱动器SN75LBC184使能控制
*/
typedef struct//union
{
unsigned int i;
float f;
} value;
/***********************************************************************************
I/O口,常量,命令字等定义
***********************************************************************************/
enum {TEMP,HUMI}; //用于判断是温度还是湿度的枚举常量
sbit SHT15_SDA = P2^0; //SHT15_SHT75 数据线
sbit SHT15_SCK = P2^1; //SHT15_SHT75 时钟线
#define noACK 0 //“无应答”信号
#define ACK 1 //“有应答”信号
//Addr 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
//以下为底层子函数
/*******************************************************************************************
* 函数名: SHT15_Write_Byte
* 功能描述: 写入一个字节,并检查从设备应答:“有应答”返回0;“无应答”返回1
* 入口参数: 要写入的字节,uchar型
* 出口参数: 成功(0)/失败(1)标志
* 备注:
*******************************************************************************************/
unsigned char SHT15_Write_Byte(unsigned char value)
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2)
{
if (i & value)
SHT15_SDA=1;
else
SHT15_SDA=0;
SHT15_SCK=1;
_nop_();
_nop_();
_nop_();
SHT15_SCK=0;
}
SHT15_SDA=1;
SHT15_SCK=1;
error=SHT15_SDA;
SHT15_SCK=0;
return error;
}
/*******************************************************************************************
* 函数名: SHT15_Read_Byte
* 功能描述: 读出一个字节,并根据输入参数决定是否发送应答位,1-发送“有应答”
* 入口参数: 应答位标志,1-读出1个字节后发送“有应答”
* 出口参数: 成功(0)/失败(1)标志
* 备注:
*******************************************************************************************/
unsigned char SHT15_Read_Byte(unsigned char ack)
{
unsigned char i,val=0;
SHT15_SDA=1;
for (i=0x80;i>0;i/=2)
{
SHT15_SCK=1;
if (SHT15_SDA)
val=(val | i);
SHT15_SCK=0;
}
SHT15_SDA=!ack;
SHT15_SCK=1;
_nop_();
_nop_();
_nop_();
SHT15_SCK=0;
SHT15_SDA=1;
return val;
}
/*******************************************************************************************
* 函数名: SHT15_Trans_Start
* 功能描述: 启动总线传输
* 入口参数: 无
* 出口参数: 无
* 备注:
* 时序:
_____ ________
DATA: |_______|
___ ___
SCK : ___| |___| |______
*******************************************************************************************/
void SHT15_Trans_Start(void)
{
SHT15_SDA=1;
SHT15_SCK=0;
_nop_();
SHT15_SCK=1;
_nop_();
SHT15_SDA=0;
_nop_();
SHT15_SCK=0;
_nop_();
_nop_();
_nop_();
SHT15_SCK=1;
_nop_();
SHT15_SDA=1;
_nop_();
SHT15_SCK=0;
}
/*******************************************************************************************
* 函数名: SHT15_Connection_Reset
* 功能描述: 通信连接强制复位
* 入口参数: 无
* 出口参数: 无
* 备注:
* 时序:
_____________________________________________________ ________
DATA: |_______|
_ _ _ _ _ _ _ _ _ ___ ___
SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
*******************************************************************************************/
void SHT15_Connection_Reset(void)
{
unsigned char i;
SHT15_SDA=1;
SHT15_SCK=0;
for(i=0;i<9;i++)
{
SHT15_SCK=1;
SHT15_SCK=0;
}
SHT15_Trans_Start();
}
/*******************************************************************************************
* 函数名: s_softreset
* 功能描述: 传感器软复位
* 入口参数: 无
* 出口参数: 成功(0)/失败(1)标志
* 备注:
*******************************************************************************************/
unsigned char s_softreset(void)
{
unsigned char error=0;
SHT15_Connection_Reset(); //通信连接强制复位
error+=SHT15_Write_Byte(RESET); //发送软复位命令
return error; //error=1 表示传感器没有响应(线路故障)
}
/*******************************************************************************************
* 函数名: SHT15_Read_Status_Reg
* 功能描述: 读取传感器寄存器状态,以及8-bit校验和
* 入口参数: 寄存器状态缓冲区首址,校验和缓冲区首址
* 出口参数: 成功(0)/失败(1)标志
* 备注:
*******************************************************************************************/
//unsigned char SHT15_Read_Status_Reg(unsigned char *p_value, unsigned char *p_checksum)
//{
// unsigned char error=0;
// SHT15_Trans_Start();
// error=SHT15_Write_Byte(STATUS_REG_R);
// *p_value=SHT15_Read_Byte(ACK);
// *p_checksum=SHT15_Read_Byte(noACK);
// return error; //error=1 表示传感器没有响应(线路故障)
//}
/*******************************************************************************************
* 函数名: SHT15_Write_Status_Reg
* 功能描述: 写入传感器寄存器状态
* 入口参数: 寄存器状态缓冲区首址
* 出口参数: 成功(0)/失败(1)标志
* 备注:
*******************************************************************************************/
//unsigned char SHT15_Write_Status_Reg(unsigned char *p_value)
//{
// unsigned char error=0;
// SHT15_Trans_Start();
// error+=SHT15_Write_Byte(STATUS_REG_W);
// error+=SHT15_Write_Byte(*p_value);
// return error; //error=1 表示传感器没有响应(线路故障)
//}
/*******************************************************************************************
* 函数名: SHT15_Measure
* 功能描述: 测量1次(可选择温度/湿度)
* 入口参数: 测量结果缓冲区首址,校验和缓冲区首址,测量模式(温度/湿度)
* 出口参数: 成功(0)/失败(1)标志
* 备注:
*******************************************************************************************/
unsigned char SHT15_Measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned error=0;
unsigned int i;
SHT15_Trans_Start();
switch(mode)
{
case TEMP : error+=SHT15_Write_Byte(MEASURE_TEMP);
break;
case HUMI : error+=SHT15_Write_Byte(MEASURE_HUMI);
break;
default : break;
}
for (i=0;i<65535;i++)//等待测量结束,并加入超时错误检测(防死)
{
if(SHT15_SDA == 0)
{
break;
}
}
if(SHT15_SDA)
{
error+=1; //超时出错
}
*(p_value) = SHT15_Read_Byte(ACK); //读第1个字节(MSB)
*(p_value+1) = SHT15_Read_Byte(ACK); //读第2个字节(LSB)
*p_checksum = SHT15_Read_Byte(noACK); //读取校验和
return error;
}
/*******************************************************************************************
* 函数名: Init_Uart
* 功能描述: 配置串行口为9600波特率
* 入口参数: 无
* 出口参数: 无
* 备注: 晶振为11.0592MHz;其余见代码注释
*******************************************************************************************/
/*
void Init_Uart()
{
SCON = 0x40; //串行方式1,不允许串行接收
TMOD = 0x20; //定时器T1,方式2
//**** 根据单片机时钟模式修改下面的代码 ****
// TH1 = 0xFA; //6时钟模式
TH1 = 0xFD; //12时钟模式
//******************************************
TR1 = 1; //启动定时器
TI = 1; //发送第一个字符
}
*/
/*******************************************************************************************
* 函数名: SHT15_Calculate
* 功能描述: 转换温度和湿度为常用格式(分别为摄氏度和%RH)
* 入口参数: 湿度缓冲指针(12 bit AD转换值),温度缓冲指针(14 bit AD转换值)
* 出口参数: 无
* 备注:
*******************************************************************************************/
void SHT15_Calculate(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; //calc. temperature from ticks to [癈]
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]
}
/*******************************************************************************************
* 函数名: SHT15_Calculate_Dew_Point
* 功能描述: 根据温度和湿度,计算露点(单位是:摄氏度)
* 入口参数: 湿度(%RH),温度(摄氏度)
* 出口参数: 露点(摄氏度)
* 备注:
*******************************************************************************************/
float SHT15_Calculate_Dew_Point(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 SHT75_Demo(void)
{
static idata unsigned char count=0;
static idata value humi_val[_SmoothNum],temp_val[_SmoothNum];
float humi,temp;
float dew_point;
unsigned char error,checksum;
unsigned int i;
// Init_Uart();
s_softreset();//传感器软复位
T0_Delay_ms(10);
SHT15_Connection_Reset();
// EN_75LBC184 = 1; //允许485发送
// LED_RXD = 1; //关闭待机指示灯
error=0;
count ++;
if(count>=_SmoothNum)
count=0;
error+=SHT15_Measure((unsigned char*) &temp_val[count].i,&checksum,TEMP); //测量温度
error+=SHT15_Measure((unsigned char*) &humi_val[count].i,&checksum,HUMI); //测量湿度
if(error!=0)
SHT15_Connection_Reset(); //如果发生错误,通信连接强制复位
else
{
humi_val[count].f=(float)humi_val[count].i; //整形转换为浮点数
temp_val[count].f=(float)temp_val[count].i; //整形转换为浮点数
for(i=0;i<_SmoothNum;i++)
{
humi += humi_val[i].f;
temp += temp_val[i].f;
}
humi /= (float)_SmoothNum;
temp /= (float)_SmoothNum;
SHT15_Calculate(&humi,&temp); //计算湿度和温度
dew_point=SHT15_Calculate_Dew_Point(humi,temp); //计算露点
// LED_TXD=0;//点亮发送指示灯
printf("temp:%5.2fC humi:%5.2f%% dew point:%5.2fC\r\n",
temp,humi,dew_point);//打印结果
// LED_TXD=1;//关闭发送指示灯
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -