ラズベリーパイpython smbus関数の説明

5405 ワード

ちょうど木莓派を学ぶことを始めて、I 2 Cインタフェースを使う时いくつか问题に出会って、例えばsmbusがあれらの関数とこれらの関数の使用方法があることを理解していないで、ネット上で长い间探しても具体的にsmbus関数を说明するドキュメントを探し当てていないで、今私の整理したいくつかの说明の资料をアーカイブして、一方で自分の后期の学习に便利で、2つの方面はちょうど木莓派に接触して、もっと深くI 2 Cインタフェースの机能を学びたい友达、皆さんの役に立つことを願っています.
使用方法:
まず、プログラムにsmbusモジュールをインポートします.方法は次のとおりです.
#     :
import smbus

#     :
from smbus import SMBus

#    smbus  
b = smbus.SMBus(1)

b = SMBus(1)    # 0    /dev/i2c-0, 1    /dev/i2c-1
                #           I2C   。

#           i2c   
b.read_byte_data(0x2f,0x58)

 
 
function
description
parameters
return value
 
                                                                                SMBus Access
 
write_quick (addr)
Quick transaction.
int addr
long
 
read_byte (addr)
Read Byte transaction.
int addr
long
 
write_byte (addr, val)
Write Byte transaction.
int addr, char val
long
 
read_byte_data (addr, cmd)
Read Byte Data transaction.
int addr, char cmd
long
 
write_byte_data (addr, cmd, val)
Write Byte Data transaction.
int addr, char cmd, char val
long
 
read_word_data (addr, cmd)
Read Word Data transaction.
int addr, char cmd
long
 
write_word_data (addr, cmd, val)
Write Word Data transaction.
int addr, char cmd, int val
long
 
process_call (addr, cmd, val)
Process Call transaction.
int addr, char cmd, int val
long
 
read_block_data (addr, cmd)
Read Block Data transaction.
int addr, char cmd
long [ ]
 
write_block_data (addr, cmd, vals)
Write Block Data transaction.
int addr, char cmd, long [ ]
None
 
block_process_call (addr, cmd, vals)
Block Process Call transaction.
int addr, char cmd, long [ ]
long [ ]
 
                                                                                 I2C Access
 
read_i2c_block_data (addr, cmd)
Block Read transaction.
int addr, char cmd
long [ ]
 
write_i2c_block_data (addr, cmd, vals)
Block Write transaction.
int addr, char cmd, long [ ]
None
関数の説明:
Checking For Connected Devices
At the command prompt type one of these depending on whether you are using the I2C0 or I2C1 port: 
sudo i2cdetect -y 0
//or
sudo i2cdetect -y 1

The 7 bit I2C address of all found devices will be shown (ignoring the R/W bit, so I2C address 0000 0110 is displayed as hex 03).
SMBus (System Management Bus) is a subset from the I2C protocol When writing a driver for an I2C device try to use the SMBus commands if possible (if the device uses only that subset of the I2C protocol) as it makes it possible to use the device driver on both SMBus adapters and I2C adapters.
Note address is the 7 bit address excluding the read/write bit (it will be shifted left 1 bit when added to the read/write bit)
# Send only the read / write bit 
long write_quick(int addr)

# Read a single byte from a device, without specifying a device register. 
long read_byte(int addr)

# Send a single byte to a device 
long write_byte(int addr, char val)

# Read Byte Data transaction. 
long read_byte_data(int addr, char cmd)

# Write Byte Data transaction. 
long write_byte_data(int addr, char cmd, char val)

# Read Word Data transaction. 
long read_word_data(int addr, char cmd)

# Write Word Data transaction. 
long write_word_data(int addr, char cmd, int val)

# Process Call transaction. 
long process_call(int addr, char cmd, int val)

#Read Block Data transaction.  
long[] read_block_data(int addr, char cmd)
    
# Write up to 32 bytes to a device.  This fucntion adds an initial byte indicating the 
# length of the vals array before the valls array.  Use write_i2c_block_data instead! 
write_block_data(int addr,char cmd,long vals[])

# Block Process Call transaction.  
long[] block_process_call(int addr, char cmd, long vals[])
 
   
# I2C Access Functions
# Block Read transaction. 
long[] read_i2c_block_data(int addr, char cmd)

#Block Write transaction. 
write_i2c_block_data(int addr, char cmd, long vals[])


#Code Example

#!/usr/bin/python

import smbus

bus = smbus.SMBus(1)    # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)

DEVICE_ADDRESS = 0x15      #7 bit address (will be left shifted to add the read write bit)
DEVICE_REG_MODE1 = 0x00
DEVICE_REG_LEDOUT0 = 0x1d

#Write a single register
bus.write_byte_data(DEVICE_ADDRESS, DEVICE_REG_MODE1, 0x80)

#Write an array of registers
ledout_values = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
bus.write_i2c_block_data(DEVICE_ADDRESS, DEVICE_REG_LEDOUT0, ledout_values)