一.实验环境
1.硬件平台
· arm开发板(使用i2c line 4)
· i2c接口的imu
2.软件环境
· ubuntu 16.04
· i2c-tools 3.1.2
二.实验步骤
1.提取所依赖的i2c-tools文件
· i2c-dev.h
· 2cbusses.h
· 2cbusses.c
2.调用必要头文件
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "i2c-dev.h"
#include "i2cbusses.h"
3.调用api接口实现数据读写
void main()
{
char *end;
char filename[20];
int i2cbus, address=0x6b, file, res;
char *busline = "4";
int pec = 0;
//查找i2c总线4
i2cbus = lookup_i2c_bus(busline);
//打开设备
file = open_i2c_dev(i2cbus, filename, sizeof(filename), 0);
if (file < 0)
{
printf("open dev error < 0\r\n");
}
//设置从器件地址
set_slave_addr(file, address, force);
//从0x0f寄存器读取一个字节数据
res = i2c_smbus_read_byte_data(file, 0x0f);
printf("read = %d \r\n", res);
//往0x10寄存器写一个字节数据0x20
res = i2c_smbus_write_byte_data(file, 0x10, 0x20);
//关闭设备
close(file);
}