linux基于i2c-tools快速搭建设备读写环境

一.实验环境

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);
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路人 假

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值