#include //#define CHIP_ADDR 0x20
#define I2C_DEV "/dev/i2c-0"//i2c_dev为i2c adapter创建的别名
//读操作先发Slaveaddr_W+Regaddr_H+Regaddr_L 3个字节来告诉设备操作器件及两个byte参数
//然后发送Slaveaddr_R读数据
static int iic_read(int fd, char buff[], int addr, int count)
{
int res;
char sendbuffer1[2];
sendbuffer1[0]=addr>>8;
sendbuffer1[1]=addr;
write(fd,sendbuffer1,2);
res=read(fd,buff,count);
printf("read %d byte at 0x%x\n", res, addr);
return res;
}
//在写之前,在数据前加两个byte的参数,根据需要解析
static int iic_write(int fd, char buff[], int addr, int count)
{
int res;
int i,n;
static char sendbuffer[100];
memcpy(sendbuffer+2, buff, count);
sendbuffer[0]=addr>>8;
sendbuffer[1]=addr;
res=write(fd,sen