前言
单总线
一、GXHT3W简介
GXHT3W是北京中科银河芯设计生产的一款使用单总线协议通信的温湿度传感器,区别于IIC和SPI等常用接口的地方在于单总线支持两线/三线通信,通信距离远,在主机驱动能力足够的情况下可达数百米,同时,总线上可搭载多个传感器。
在驱动程序方面,GXHT3W和18b20系列的读写以及复位时序兼容,也有64位ID,与18b20不同的地方只有读取的数据多了湿度数据,以及计算公式不同。
二、参数简介
1.工作参数
电气特性参数
硬件电路
三线制电路
当需要驱动更多传感器或者在长距离环境下使用时,可以增加强驱动电路来辅助DQ上拉和下拉。
三、驱动程序
1.读写时序
GXHT3W有IIC和单总线两种通信方式,其中IIC通信和GXHT3X系列相同,兼容SHT3x系列,因此在这就对单总线通信时序做一些简单介绍。使用单总线通信时,用过18b20的朋友可以直接使用18b20程序驱动,下面做一个使用18b20程序驱动的简单示例。
初始化时序代码如下(示例):
//直接使用18b20的复位程序
static void GX18B20_Rst(void)
{
GX18B20_Mode_Out_PP();
GX18B20_DQ_0;
delay_us(750);
GX18B20_DQ_1;
delay_us(15);
}
static uint8_t GX18B20_Presence(void)
{
uint8_t pulse_time = 0;
GX18B20_Mode_IPU();
while( GX18B20_DQ_IN() && pulse_time<100 )
{
pulse_time++;
delay_us(1);
}
if( pulse_time >=100 )
return 1;
else
pulse_time = 0;
while( !GX18B20_DQ_IN() && pulse_time<240 )
{
pulse_time++;
delay_us(1);
}
if( pulse_time >=240 )
return 1;
else
return 0;
}
复位的具体时间只要满足复位时序图的要求均可
2.读写数据
读写时序如图所示,写1和写0时的时间要超过图中所示的最小时间,主机下拉后的读取动作不能太晚,这里厂家给出了建议的读取时序,如下图:
读取数据的时候IO需要切换到浮空或开漏。
代码如下(示例):
static uint8_t GX18B20_ReadBit(void)
{
uint8_t dat;
GX18B20_Mode_Out_PP();
GX18B20_DQ_0;
delay_us(10);
GX18B20_Mode_IPU();
if( GX18B20_DQ_IN() == SET )
dat = 1;
else
dat = 0;
delay_us(45);
return dat;
}
static uint8_t GX18B20_ReadByte(void)
{
uint8_t i, j, dat = 0;
for(i=0; i<8; i++)
{
j = GX18B20_ReadBit();
dat = (dat) | (j<<i);
}
return dat;
}
static void GX18B20_WriteByte(uint8_t dat)
{
uint8_t i, testb;
GX18B20_Mode_Out_PP();
for( i=0; i<8; i++ )
{
testb = dat&0x01;
dat = dat>>1;
if (testb)
{
GX18B20_DQ_0;
/* 1us < < 15us */
delay_us(8);
GX18B20_DQ_1;
delay_us(58);
}
else
{
GX18B20_DQ_0;
/* 60us < < 120us */
delay_us(70);
GX18B20_DQ_1;
delay_us(2);
}
}
}
3.数据处理
代码如下(示例):
void GXHT3w_GetTemp_SkipRom ( void )
{
uint8_t tpmsb, tplsb, Th, Tl, con, res, humil, humim, crc, i;
u16 tem,hum;
float Temperature=0;
float Humidity=0;
GX18B20_SkipRom ();
GX18B20_WriteByte(0X44);
delay_ms(10);
GX18B20_SkipRom ();
GX18B20_WriteByte(0XBE);
tplsb = GX18B20_ReadByte();
tpmsb = GX18B20_ReadByte();
Th = GX18B20_ReadByte();
Tl = GX18B20_ReadByte();
con = GX18B20_ReadByte();
res = GX18B20_ReadByte();
humil = GX18B20_ReadByte();
humim = GX18B20_ReadByte();
crc = GX18B20_ReadByte();
tem = ((tpmsb<<8) | tplsb);//ζÈÆ´½Ó
printf("\r\n%x\r\n",tem);
hum = ((humil<<8) | humim);//ʪ¶ÈÆ´½Ó
printf("%x\r\n",Th);
printf("%x\r\n",Tl);
printf("%x\r\n",hum);
Temperature= (175.0*(float)tem /4095.0-45.0) ;// T = -45 + 175 * tem / (2^16-1)
Humidity= (100.0*(float)hum/65535.0);// RH = hum*100 / (2^16-1)
sprintf(Humiture_buff2,"%2.2f*C %2.2f%%",Temperature,Humidity);
printf("TH:%s\r\n",Humiture_buff2);
}
void GXHT3w_GetTemp_MatchRom ( uint8_t * GX18b20_id )
{
uint8_t tpmsb, tplsb, Th, Tl, con, res, humil, humim, crc, i;
u16 tem,hum;
float Temperature=0;
float Humidity=0;
OLED_Fill(0x00);
GX18B20_MatchRom ();
for(i=0;i<8;i++)
GX18B20_WriteByte ( GX18b20_id [ i ] );
GX18B20_WriteByte(0X44);
delay_ms(800);
GX18B20_MatchRom ();
for(i=0;i<8;i++)
GX18B20_WriteByte ( GX18b20_id [ i ] );
GX18B20_WriteByte(0XBE);
tplsb = GX18B20_ReadByte();
tpmsb = GX18B20_ReadByte();
Th = GX18B20_ReadByte();
Tl = GX18B20_ReadByte();
con = GX18B20_ReadByte();
res = GX18B20_ReadByte();
humil = GX18B20_ReadByte();
humim = GX18B20_ReadByte();
crc = GX18B20_ReadByte();
tem = ((tpmsb<<8) | tplsb);
hum = ((humil<<8) | humim);
Temperature= (175.0*(float)tem /4095.0-45.0) ;// T = -45 + 175 * tem / (2^16-1)
Humidity= (100.0*(float)hum/65535.0);// RH = hum*100 / (2^16-1)
sprintf(Humiture_buff2,"%2.2f*C %2.2f%%",Temperature,Humidity);
printf("TH:%s\r\n",Humiture_buff2);
}
GXHT3W的命令与18b20兼容,读取温湿度的时候可以使用跳过ROM的方式,先发送0xcc 和0x44,然后发送0xcc和0xbe读取。也可以使用匹配ROM的方式读取,发送0x55匹配ID命令后,发送芯片的64位ID,然后发送0x44转换命令;读取也一样,先发送0x55匹配ID命令后,发送芯片的64位ID,然后发送0xbe,读取数据。
到这里为止,驱动程序与18b20都兼容,在后面的数据处理部分有所差别。GXHT3W前两个字节为温度数据,七和八字节为湿度数据。具体计算公式参考代码所示。
PS:发送0xcc和匹配rom前都需要先发送复位命令。
总结
单总线通信的温湿度传感器GXHT3W具有体积小,功耗低,通信距离长,可多个并联使用等优点,很适合在多点长距离温湿度检测等场合使用。
ps:15665169484