#include "sht20.h"
int TT,RH;
bit SHTMeasureOverBit;
unsigned int RealStat; // 温湿度测量时序控制
unsigned int RHcount_AS; //SHT20的湿度计数值
unsigned int ADCData_Rt; //SHT20的温度计数值
void SHT_CalcRHT()
{
RH=RHcount_AS*0.0190734863-60;//hum 参照SHT20方程
// cntarray[0]=ADCData_Rt*0.0268127441-468.5;//+MyEMBcfg.T_off;//temp 参照SHT20方程
TT=ADCData_Rt*0.0268127441-468.5;//temp 参照SHT20方程
if(pianyip_t == 1)
{
TT = TT -((Temperature_Temp%100)*10);
}
if(pianyip_t == 0)
{
TT = TT +(Temperature_Temp*10);
}
if(pianyi_h == 1)
{
RH = RH -((Humidity_Temp%100)*10);
}
if(pianyi_h == 0)
{
RH = RH +(Humidity_Temp*10);
}
}
void SHT20_Init() // SHT 测量初始化
{
S_Init();
SHTMeasureOverBit=0;
RH=0;
TT=0;
RealStat=0;
}
//SHT IIC 接口
void S_Init(void)
{
S_SCL_1;
S_Delay();
S_SDA_1;
S_Delay();
}
void S_Start(void)
{
// S_DIR_OUT;
// P3M0 |=S_SDA;
S_SDA_1;
S_Delay();
S_SCL_1;
S_Delay();
S_SDA_0;
S_Delay();
S_SCL_0;
}
void S_Stop(void)
{
S_SDA_0;
S_Delay();
S_SCL_1;
S_Delay();
S_SDA_1;
S_Delay();
}
void S_ReceiveAck(void)
{
volatile int i=0;
S_SCL_1;
S_Delay();
while (S_SDA_IN== 0x01&&i<1005)
{ i++; }
S_SCL_0;
S_Delay();
}
void S_Acknowledge(void)
{
S_SCL_0;
S_Delay();
S_SDA_0;
S_Delay();
S_SCL_1;
S_Delay();
S_SCL_0;
}
void S_Nacknowledge(void)
{
S_SCL_0;
S_Delay();
S_SDA_1;
S_Delay();
S_SCL_1;
S_Delay();
S_SCL_0;
}
void S_WriteByte(unsigned char WData)
{
int j;
for (j=0; j<8; j++)
{
S_SCL_0;
S_Delay();
if (((WData >> 7) & 0x01) == 0x01)
{
S_SDA_1;
}
else
{
S_SDA_0;
}
S_Delay();
S_SCL_1;
S_Delay();
WData = WData << 1;
}
S_SCL_0;//用于接收ACK
S_SDA_0;
Delay_us(30);
//Delay(8);
}
unsigned char S_ReadByte(void)
{
unsigned char i;
unsigned char TempBit = 0;
unsigned char TempData = 0;
S_SDA_1;
// S_DIR_IN;
// P3M0 |=S_SDA;
S_Delay();
for (i=0; i<8; i++)
{
S_Delay();
S_SCL_1;
S_Delay();
if (S_SDA_IN == 0x01 ) /*sda==1*/
{
TempBit = 1;
}
else
{
TempBit = 0;
}
S_Delay();
S_SCL_0;
S_Delay();
S_Delay();
TempData = (TempData << 1) | TempBit;
}
//S_DIR_OUT;
// P3M0 |=S_SDA ;
S_SDA_1;
S_Delay();
Delay_us(30);
TempData=TempData;
return(TempData);
}
void S_Delay(void)
{
unsigned int i;
for(i=0;i<50;i++); /*用于调整CLK 脉冲宽度*/
}
void S_MeasureTemp(void) //温度测量命令
{
S_Start();
//////////////////////
//发送读命令
S_WriteByte(0x80);
S_Delay();
//读取ACK
S_ReceiveAck();
S_WriteByte(0xF3); // 非主机触发T测量
S_Delay();
S_Delay();
//读取ACK
S_ReceiveAck();
}
unsigned int S_Read_SHT20_Value(void) //读取SHT20 的温度或者湿度输出参数
{
unsigned int temp = 0;
char hi,lo;
hi=0x00;
lo=0x00;
S_Start();
//读取T的测量结果
S_WriteByte(0x81);
S_Delay();
//读取ACK
S_ReceiveAck();
//读高字节
hi = S_ReadByte();
S_Delay();
//主机发送ACK
S_Acknowledge();
//读低字节
lo = S_ReadByte();
S_Delay();
//主机发送NACK
S_Nacknowledge();
//停止信号
S_Stop();
lo=lo&0xFC;
temp =hi*256 + lo;
return temp ;
}
void S_MeasureHum(void) //湿度度测量命令
{
S_Start();
//////////////////////
//发送读命令
S_WriteByte(0x80);
S_Delay();
//读取ACK
S_ReceiveAck();
S_WriteByte(0xF5); // 非主机触发T测量
S_Delay();
S_Delay();
//读取ACK
S_ReceiveAck();
}
void Delay_us(unsigned int nValue)//微秒为单位,11.0592MHz为主时钟
{
int nCount;
int i;
int j;
nCount = 60000;
for(i = nValue;i > 0;i--)
{
for(j = nCount;j > 0;j--);
}
return;
}