在linux操作系统下使用ads1115的读写问题
描述:
嵌入linux操作系统,连接在twi接口,addr接地,目的是读取配置寄存器或者转换寄存器的初始值,四路信号都没有接
直接上源码:
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include <sys/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
typedef unsigned char BYTE;
int main()
{
printf("****************************************\n");
//open device file
int file;
int adapter_nr=1;
char filename[20];
snprintf(filename,19,"/dev/i2c-%d",adapter_nr);
file=open(filename,O_RDWR);
if(file<0)
{
printf("open device file failed !\n");
exit(-1);
}
printf("int file=%d\n",file);
//specify device address
int addr=0x48;
int ioctlnum=-1;
ioctlnum=ioctl(file,I2C_SLAVE,addr);
if(ioctlnum<0)
{
printf("specify device address failed !\n");
exit(-1);
}
printf("int ioctlnum=%d\n",ioctlnum);
printf("\n");
/*
****************************************************************
*/
BYTE w_buf[2];
w_buf[0]=0x90;//device address followed a write bit
w_buf[1]=0x01;//pointer address to config register
BYTE r_buf[3];
r_buf[0]=0x91;//device address followed a read bit
r_buf[1]=0x11;//receive read data MSB
r_buf[2]=0x11;//receive read data LSB
struct i2c_msg msg[2];
msg[0].addr=0x48;//device address
msg[0].flags=0;//write flags
msg[0].len=2;//buffer length
msg[0].buf=w_buf;//pointer to buffer
msg[1].addr=0x48;
msg[1].flags=I2C_M_RD;//read flags
msg[1].len=3;
msg[1].buf=r_buf;
struct i2c_rdwr_ioctl_data msgset;
msgset.msgs=msg;
msgset.nmsgs=2;//send twice start signal
int x=ioctl(file,I2C_RDWR,&msgset);
printf("x=%d\n",x);
printf("0x%02x%02x\n",r_buf[1],r_buf[2]);
return 0;
}
pointer register设置为0x01和0x00的返回值都是0x00ff,x=ioctl(file,I2C_RDWR,&msgset);执行后x=2
不知道问题出在哪,求解答
使用示波器量一下I2C两路信号是不是对的,还有时序是不是和spec一致。