【分布式温湿度采集系统】+W800WIFI+温湿度传感器
[复制链接]
通过W800芯片的I2C接口读取温湿度传感器SHT20,联盛德提供的W800芯片例程还是很棒的,使用起来非常方便。通过开启I2C例程宏定义,然后在I2C例程文件中修改对应的设置。
例程主循环,配置IO口,初始化外设I2C。
int i2c_demo(char *buf)
{
// 设置I2C对应IO口
wm_i2c_scl_config(WM_IO_PA_01);
wm_i2c_sda_config(WM_IO_PA_04);
tls_i2c_init(I2C_FREQ);
while(1)
{
SHT20_ReadTemperature();
SHT20_ReadHumidity();
tls_os_time_delay(1000);
}
return WM_SUCCESS;
}
读取温度
u16 SHT20_ReadTemperature(void)
{
u16 temp=0;
u8 check;
//printf("\nread addr=%x\n",ReadAddr);
tls_i2c_write_byte(0x80,1);
tls_i2c_wait_ack();
tls_i2c_write_byte(0xF3,0);
tls_i2c_wait_ack();
tls_os_time_delay(2);
tls_i2c_stop();
tls_os_time_delay(300);
tls_i2c_write_byte(0x81,1);
tls_i2c_wait_ack();
temp=tls_i2c_read_byte(1,0);
temp=(temp<<8)&0xFF00;
temp|=tls_i2c_read_byte(1,0);
check = tls_i2c_read_byte(0,1);
//printf("\nread byte=%x : %x\n",temp,check);
I2cTemperature = (float)temp / 65536.0 * 175.72 - 46.85;
printf("temperature=%f\n",I2cTemperature);
return temp;
}
读取湿度
u16 SHT20_ReadHumidity(void)
{
u16 temp=0;
u8 check;
//printf("\nread addr=%x\n",ReadAddr);
tls_i2c_write_byte(0x80,1);
tls_i2c_wait_ack();
tls_i2c_write_byte(0xF5,0);
tls_i2c_wait_ack();
tls_os_time_delay(2);
tls_i2c_stop();
tls_os_time_delay(300);
tls_i2c_write_byte(0x81,1);
tls_i2c_wait_ack();
temp=tls_i2c_read_byte(1,0);
temp=(temp<<8)&0xFF00;
temp|=tls_i2c_read_byte(1,0);
check = tls_i2c_read_byte(0,1);
//printf("\nread byte=%x : %x\n",temp,check);
I2cHumidity = (float)temp / 65536.0 * 125.0 - 6;
I2cFlag = 1;
printf("humidity=%f\n",I2cHumidity);
return temp;
}
|