|
板凳
楼主 |
发表于 2016-12-16 11:28:02
|
只看该作者
i.MX6Q I2C芯片ADS1110 linux设备文件节点/dev/i2c-0 已解决 源码如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#define I2C_DEV "/dev/i2c-0"
#define CHIP_ADDR 0x48
static int read_I2C(int fd, char buff[], int addr, int count)
{
int res;
if(write(fd,&addr,1)!=1){
printf("write failed\n");
return -1;
}
while(1){
res=read(fd,buff,count);
int lum_value = buff[0]*256 + buff[1];
printf("The lum value is %d\n", lum_value);
usleep(100000);
}
return res;
}
int main()
{
int fd, res;
fd = open(I2C_DEV, O_RDWR);
if(fd < 0){
printf("####i2c test device open failed####\n");
return (-1);
}
res = ioctl(fd,I2C_TENBIT,0);
res = ioctl(fd,I2C_SLAVE,CHIP_ADDR);
char buff[4];
read_I2C(fd, buff, CHIP_ADDR,4);
close(fd);
return 0;
}
|
|