zoukankan      html  css  js  c++  java
  • ubuntu下使用quick2wire控制RespberryPi2的I2C

    首先,开启树莓派的I2C驱动:

      查看I2C驱动是否已经被加载:ls /dev -l | grep i2c,如果有形如 i2c-x 的显示结果表明驱动已经加载,否则驱动没有加载,需要进行如下操作:

        修改配置文件:/etc/modprobe.d/raspi-blacklist.conf(如果存在的话)

        去掉 “#blacklist i2c-bcm2708” 前的注释符号#

        重启树莓派,可以使用命令(sudo reboot)

        加载驱动: sudo modprobe i2c-dev

        设置读写属性:sudo chmod o+rw /dev/i2c*

    安装python2.7:

      quick2wire本身支持python3,但因为很多程序写在python2.7下,所以我添加了quick2wire对python2.7的支持

      sudo apt-get install python

    获取quick2wire代码:

      创建一个本地目录,假设名字是q2w:

      进入该目录 cd q2w

      获取quick2wire-gpio-admin

        git clone https://github.com/quick2wire/quick2wire-gpio-admin.git

        cd quick2wire-gpio-admin

        make && make install

      获取quick2wire-python-api

        原始版本(只支持python3):https://github.com/quick2wire/quick2wire-python-api.git

        支持python2.7的版本:https://github.com/freason/quick2wire-python-api.git

        这里使用支持python2.7的版本:

        git clone https://github.com/freason/quick2wire-python-api.git

        cd quick2wire-python-api

               git checkout support_python2

        python setup.py install --user

    使用:

      请参见:

        https://github.com/quick2wire/quick2wire-python-api/blob/master/README.md

        https://github.com/quick2wire/quick2wire-python-api/blob/master/doc/getting-started-with-i2c.md

      首先用i2cdetect确定slave设备的地址:

        i2cdetect x(其中x为/dev/i2c-x中的x),这条命令会返回类似如下的数据:

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1e -- 
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    70: -- -- -- -- -- -- -- --                        
    

      1e为slave设备的地址,我使用的设备是HMC5883,三轴陀螺仪

      简单的python代码:

    import quick2wire.i2c as i2c
    
    addr = 0x1e
    regConf = 0x02
    regRd = 0x03
    
    bus = i2c.I2CMaster()
    bus.transaction(i2c.writing_bytes(addr, regConf, 0x00))
    read_results = bus.transaction(i2c.writing_bytes(addr, regRd), i2c.reading(address, 6))
    for i in r[0]:                                                          
        print(ord(i))

      该代码会有如下输出:

    254
    168
    255
    109
    255
    73

      

  • 相关阅读:
    堆排序
    如何在.Net中使用MongoDB
    二叉树遍历 C#
    对C# 中Readonly的再认识
    对C# 构造函数的理解
    TypeScript学习: 九、TypeScript的泛型
    TypeScript学习: 八、TypeScript的属性接口用法封装ajax
    锚点跳转不改变History
    设计模式笔记—代理模式
    小程序开发二:上手第一个小程序
  • 原文地址:https://www.cnblogs.com/astreye/p/5091764.html
Copyright © 2011-2022 走看看