SHT11温湿度传感器AVR单片机程序
扫描二维码
随时随地手机看文章
#include"shtxx.h"
voidshtxx_init(void)
{
shtxx_temp=shtxx_humi=0;
SHTXX_SCK_LOW();
SHTXX_DAT_1();
shtxx_reconnect();
}
voidshtxx_reconnect(void)
{
SHTXX_DAT_1();
SHTXX_SCK_LOW();
for(uint8i=0;i<9;i++)
{
SHTXX_SCK_HIGH();SHTXX_SCK_LOW();
}
SHTXX_START();
}
uint8shtxx_SOFtrst(void)
{
uint8error=0;
shtxx_reconnect();
error+=shtxx_write_byte(SHTXX_SOFT_RST);
returnerror;
}
//写命令函数
//参数:命令类型
//返回:0成功,1失败
uint8shtxx_write_byte(uint8cmd)
{
uint8ack;
for(uint8i=8;i>0;i--)
{
if(BITCHK(cmd,(i-1)))//trans’1’
{
SHTXX_DAT_1();
SHTXX_SCK_HIGH();SHTXX_SCK_LOW();
}
else//trans’0’
{
SHTXX_DAT_0();
SHTXX_SCK_HIGH();SHTXX_SCK_LOW();
}
}
//SHTxx会以下述方式表示已正确地接收到指令:
//在第8个SCK时钟的下降沿之后,将DATA下拉为低电平(ACK位)。
//在第9个SCK时钟的下降沿之后,释放DATA(恢复高电平)。
BITCLR(SHTXX_DAT_DDR,SHTXX_DAT_BIT);
SHTXX_SCK_HIGH();
ack=BITCHK(SHTXX_DAT_PIN,SHTXX_DAT_BIT);
SHTXX_SCK_LOW();
returnack;
}
uint8shtxx_read_byte(uint8dat_crc)
{
uint8dat8=0;
for(uint8i=8;i>0;i--)
{
BITCLR(SHTXX_DAT_DDR,SHTXX_DAT_BIT);
c4680504cSHTXX_SCK_HIGH();
if(BITCHK(SHTXX_DAT_PIN,SHTXX_DAT_BIT))
BITSET(dat8,(i-1));
SHTXX_SCK_LOW();
}
if(dat_crc)
{
SHTXX_DAT_0();
SHTXX_SCK_HIGH();SHTXX_SCK_LOW();
}
else
{
SHTXX_DAT_1();
SHTXX_SCK_HIGH();SHTXX_SCK_LOW();
}
SHTXX_DAT_1();
returndat8;
}
int8shtxx_measure(uint8mode)
{
uint8error=0;
SHTXX_START();
switch(mode)
{
caseSHTXX_MODE_TEMP:
error=shtxx_write_byte(SHTXX_CMD_TEMPTURE);
for(uint16i=0;i<65535;i++)
{
_delay_us(5);
BITCLR(SHTXX_DAT_DDR,SHTXX_DAT_BIT);
if(!(BITCHK(SHTXX_DAT_PIN,SHTXX_DAT_BIT)))break;
}
BITCLR(SHTXX_DAT_DDR,SHTXX_DAT_BIT);
if(BITCHK(SHTXX_DAT_PIN,SHTXX_DAT_BIT))error+=1;
shtxx_temp=shtxx_read_byte(1);
shtxx_temp=(shtxx_temp<<8)+shtxx_read_byte(1);
shtxx_crc=shtxx_read_byte(0);
break;
caseSHTXX_MODE_HUMI:
error=shtxx_write_byte(SHTXX_CMD_HUMIDITY);
for(uint16i=0;i<65535;i++)
{
_delay_us(5);
BITCLR(SHTXX_DAT_DDR,SHTXX_DAT_BIT);
if(!(BITCHK(SHTXX_DAT_PIN,SHTXX_DAT_BIT)))break;
}
BITCLR(SHTXX_DAT_DDR,SHTXX_DAT_BIT);
if(BITCHK(SHTXX_DAT_PIN,SHTXX_DAT_BIT))error+=1;
shtxx_humi=shtxx_read_byte(1);
shtxx_humi=(shtxx_humi<<8)+shtxx_read_byte(1);
shtxx_crc=shtxx_read_byte(0);
break;
default:
break;
}
returnerror;
}
voidshtxx_calc(void)
{
uint32rh_line=0;
//所有湿度常量放大10000000
constuint32C1=40000000;
constuint32C2=405000;
constuint32C3=28;
constuint32T1=100000;
constuint32T2=8000;
//温度常量放大100
t_c=shtxx_temp-4000;//结果除以100
rh_line=C2*shtxx_humi-C3*shtxx_humi*shtxx_humi-C1;
rh_true=(t_c/100-25)*(T1+T2*shtxx_humi)+rh_line;
if(rh_true>1000000000)rh_true=1000000000;//cutifthevalueisoutsideof
if(rh_true<1000000)rh_true=1000000;//thephysICalpossiblerange
}
voidhex_bcd(uint32hex32)
{
MEMSet(bcd32,’ ’,10);
while(hex32>=1000000000)//
{
bcd32[9]++;
hex32=hex32-1000000000;
}
while(hex32>=100000000)//
{
bcd32[8]++;
hex32=hex32-100000000;
}
while(hex32>=10000000)//
{
bcd32[7]++;
hex32=hex32-10000000;
}
while(hex32>=1000000)//
{
bcd32[6]++;
hex32=hex32-1000000;
}
while(hex32>=100000)//
{
bcd32[5]++;
hex32=hex32-100000;
}
while(hex32>=10000)//
{
bcd32[4]++;
hex32=hex32-10000;
}
while(hex32>=1000)//
{
bcd32[3]++;
hex32=hex32-1000;
}
while(hex32>=100)//
{
bcd32[2]++;
hex32=hex32-100;
}
while(hex32>=10)//
{
bcd32[1]++;
hex32=hex32-10;
}
bcd32[0]=hex32;
}
voidshtxx_disp(void)
{
if(!shtxx_measure(SHTXX_MODE_TEMP))
{
shtxx_calc();
hex_bcd(t_c);
LCD_print_str(2,1,"温度:");
if(bcd32[4]!=0)
lcd_write_dat(bcd32[4]+0x30);
lcd_write_dat(bcd32[3]+0x30);
lcd_write_dat(bcd32[2]+0x30);
lcd_write_dat(’.’);
lcd_write_dat(bcd32[1]+0x30);
lcd_write_dat(bcd32[0]+0x30);
}
if(!shtxx_measure(SHTXX_MODE_HUMI))
{
shtxx_calc();
hex_bcd(rh_true);
lcd_print_str(3,1,"湿度:");
if(bcd32[9]!=0)
lcd_write_dat(bcd32[9]+0x30);
lcd_write_dat(bcd32[8]+0x30);
lcd_write_dat(bcd32[7]+0x30);
lcd_write_dat(’.’);
lcd_write_dat(bcd32[6]+0x30);
lcd_write_dat(bcd32[5]+0x30);
}
}