#include "HT01.h"
#define SCLL1 GPIOB->BRR = GPIO_Pin_10
#define SCLH1 GPIOB->BSRR = GPIO_Pin_10
#define SDAL1 GPIOB->BRR = GPIO_Pin_11
#define SDAH1 GPIOB->BSRR = GPIO_Pin_11
#define SDA_STATE GPIOB->IDR&GPIO_Pin_11
u16 CO2, TVOC;
u16 I2C_RX[4];
u16 I2C_RX1[4];
u16 I2C_RX2[4];
void HT_Init (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void start_IIc(void)
{
/* SDA,SCL都为高,准备发送起始条件 */
SDAH1;
delay_us(2);
SCLH1;
delay_us(10);
/* 发送起始条件: SCL高, SDA高->低 */
SDAL1;
delay_us(10);
SCLL1;
delay_us(4);
return;
// delay_us(1);
}
void stop_IIc(void)
{
/* 结束条件: SCL高, SDA低->高 */
SDAL1;
delay_us(2);
SCLH1;
delay_us(10);
SDAH1;
delay_us(8);
return;
}
u8 sendbyte(unsigned char c)
{
u8 i,ack;
for(i=0; i<8; i++)
{
if((c << i) & 0x80)
{
SDAH1;
}
else
{
SDAL1;
}
delay_us(10);
SCLH1;
delay_us(10);
SCLL1;
}
delay_us(4);
SDAH1;
delay_us(6);
SCLH1;
delay_us(10);
if(SDA_STATE)
{
ack = 1;
}
else
{
ack = 0;
}
SCLL1;
delay_us(4);
return ack;
}
u8 RcvByte(void)
{
u8 i,retc=0;
/* ????????? */
SDAH1;
for(i=0; i<8; i++)
{
SCLL1;
delay_us(10);
SCLH1;
delay_us(10);
retc = retc << 1;
if(SDA_STATE)
{
retc++;
}
}
SCLL1;
delay_us(10);
return retc;
}
void Ack_I2c(unsigned char a)
{
/* 发送应答或非应答信号 */
if(a == 0)
{
SDAL1; /* 应答 */
}
else
{
SDAH1; /* 非应答 */
}
delay_us(6);
SCLH1;
delay_us(10);
SCLL1;
delay_us(4);
return;
}
void HT_Mode(void)
{
start_IIc();
sendbyte(0xB4);
sendbyte(0xF4);
// stop_IIc();
// start_IIc();
stop_IIc();
}
void HT_Set(void)
{
start_IIc();
sendbyte(0xB4);
sendbyte(0x01);
sendbyte(0x18);
// stop_IIc();
// start_IIc();
stop_IIc();
}
void IAQ( u16 *s, u8 length )
{
u8 i;
start_IIc();
sendbyte(0xB4);
sendbyte(0x02);
// stop_IIc();
start_IIc();
sendbyte(0xB5);
for(i = 0; i < (length - 1); i++)
{
*s = RcvByte();
Ack_I2c(0);
s++;
}
*s = RcvByte();
Ack_I2c(1);
stop_IIc();
}
void Temp(u16 *s, u8 length)
{
u8 ack, i;
u16 j = 0;
start_IIc();
sendbyte(0x80);
sendbyte(0xF3);
do
{
delay_us(10);
start_IIc();
ack = sendbyte(0x81);
j++;
if(j == 1000)
return;
}
while(ack == 1);
// delay_us(10);
delay_us(20);
for(i = 0; i < (length - 1); i++)
{
delay_us(10);
*s = RcvByte();
Ack_I2c(0);
s++;
}
*s = RcvByte();
Ack_I2c(1);
stop_IIc();
}
void RH(u16 *s, u8 length)
{
u8 ack, i;
u16 j = 0;
start_IIc();
sendbyte(0x80);
sendbyte(0xF5);
do
{
delay_us(10);
start_IIc();
ack = sendbyte(0x81);
j++;
if(j == 1000)
return;
}
while(ack == 1);
// delay_us(20);
delay_us(20);
for(i = 0; i < (length - 1); i++)
{
delay_us(10);
*s = RcvByte();
Ack_I2c(0);
s++;
}
*s = RcvByte();
Ack_I2c(1);
stop_IIc();
}
void Value_of_CT(void)
{
u16 c, t;
IAQ(I2C_RX, 4);
c = ((I2C_RX[0] << 8) | (I2C_RX[1]));//CO2单位是PPM
t = ((I2C_RX[2] << 8) | (I2C_RX[3]));//TVOC单位是ppb
CO2 = c;
TVOC = t;
}
u16 Value_of_Temp(void)
{
double t, temp;
Temp(I2C_RX1, 4);
t = ((I2C_RX1[0] * 256) + (I2C_RX1[1] & 0xFC));//最低两位必须为0
temp =(double)(t * 0.02681 - 468.5);//根据资料公式算法:温度=-46.85+175.72*S/2^16
return temp;
}
u16 Value_of_RH(void)
{
double r, rh;
RH(I2C_RX2, 4);
r = (I2C_RX2[0] * 256 + (I2C_RX2[1] & 0xFC));//最低两位必须为0
rh = (double)(r *0.0190735 - 60);//根据资料公式算法:湿度=-6+125*S/2^16
return rh;
}