openWRT 驱动DHT11备忘

/*************************************************************************
> File Name: chipcap2.c
> Author:daixj
> Mail:fl.service@t-firefly.com
********************************************************************/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#include <unistd.h>
#define I2C_RETRIES 0x0701 /
number of times a device address shouldbe polled when not acknowledging /
#define I2C_TIMEOUT 0x0702 /
set timeout in units of 10 ms /
/
NOTE: Slave address is 7 or 10 bits, but 10-bit addresses
* are NOT supported! (due to code brokenness)
/
#define I2C_SLAVE 0x0703 /
Use this slave address /
#define I2C_SLAVE_FORCE 0x0706 /
Use this slave address, even if it is already in use by a driver! /
#define I2C_TENBIT 0x0704 /
0 for 7 bit addrs, != 0 for 10 bit /
#define I2C_FUNCS 0x0705 /
Get the adapter functionality mask /
#define I2C_RDWR 0x0707 /
Combined R/W transfer (one STOP only) /
#define I2C_PEC 0x0708 /
!= 0 to use PEC with SMBus /
#define I2C_SMBUS 0x0720 /
SMBus transfer /
int main(int argc, char
argv)
{
int file;
int i2c_bus_number = 0;
char filename[20];
int addr = 0x28; /
The I2C DEVICE address for GE ChipCap2
/
unsigned char buf[10];
int i;
snprintf(filename, 19, “/dev/i2c-%d”, i2c_bus_number);
file = open(filename, O_RDWR);
if (file < 0) {
exit(1);
}
if (ioctl(file, I2C_SLAVE, addr) < 0) {
exit(1);
}
if (write(file,buf,1) != 1) {
printf(“Failed to write to the i2c bus.\n”);
}
// Wake the sensor from sleep and trigger a measurement (may take up to 100 mili sec)
usleep(100 * 1000);
if (read(file, buf, 4) != 4) {
printf(“ERROR HANDLING: i2c transaction failed\n”);
} else {// debug
#ifdef DEBUG
printf(“buf[0]: 0x%x\n”, buf[0]);
printf(“buf[1]: 0x%x\n”, buf[1]);
printf(“buf[2]: 0x%x\n”, buf[2]);
printf(“buf[3]: 0x%x\n”, buf[3]);
#endif
}
unsigned short value_umi;
float humidity;
value_umi = buf[0];
value_umi = (value_umi & 0x3F) << 8;
value_umi = value_umi | buf[1];
humidity = value_umi;
humidity = humidity / 163.84;
unsigned short value_temp;
float temperature;
value_temp = buf[2];
value_temp = value_temp << 8;
value_temp = value_temp | buf[3];
value_temp = value_temp & 0xFFFC;
temperature = value_temp;
temperature = ((temperature / 65536) * 165) - 40;
// Prints the temperature and humidity, comma separated, with 2 decimal precision
printf(“temperature:%0.2f,humidity:%0.2f\n”, temperature, humidity);
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值