简体   繁体   English

Python i2c write_bus_data用法

[英]Python i2c write_bus_data usage

8I have a number of 4 digit seven segment displays that I am trying to control using Beaglebone Black (running Ubuntu) and i2c. 8我有一些4位数的七段显示器,我试图用Beaglebone Black(运行Ubuntu)和i2c来控制。

The SSD's are Byvac BV4614's and the full datasheet is available here . SSD是Byvac BV4614,完整的数据表可在此处获得

I have wired up the circuit correctly using pins P9_19 and P9_20 on the Beaglebone. 我已经使用Beaglebone上的引脚P9_19和P9_20正确连接了电路。 I have included pullup resistors and am using a i2c logic converter for added safety. 我已经包含了上拉电阻,我正在使用i2c逻辑转换器以增加安全性。

I have verified the device using i2cdetect (it's 0x31 which is correct) and the device powers up nicely and enters it's i2c mode. 我已经使用i2cdetect(它的0x31是正确的)验证了设备,并且设备很好地启动并进入它的i2c模式。

However I do not understand how to read or write data to the device using Python SMBbus. 但是我不明白如何使用Python SMBbus读取或写入数据到设备。 The SSD manual says SSD手册说

The format used by this device consists of a command, this is a number, followed by other bytes depending on that command. 此设备使用的格式包括一个命令,这是一个数字,后跟其他字节,具体取决于该命令。 The method of writing to the device using the I2C protocol follows a consistent format, typically: Where S-Addr is the start condition followed by the device address (0x62). 使用I2C协议写入器件的方法遵循一致的格式,通常是:其中S-Addr是起始条件,后跟器件地址(0x62)。 Command is one of the commands given in the table. 命令是表中给出的命令之一。 Data is one or more bytes and Stop is the stop condition. 数据是一个或多个字节,Stop是停止条件。

(on 7-bit addressing the device is at 0x31 which is what I'm using). (在7位寻址设备处于0x31,这就是我正在使用的)。

So if for example I wanted to get the SSD to display a number on digit 1 the manual says I have to do 因此,如果我想让SSD在数字1上显示一个数字,手册说我必须这样做

Command 5 - Name: Send number to digit - Format:<S-addr><5><digit><byte><Stop> 命令5 - 名称:将数字发送到数字 - 格式:<S-addr> <5> <数字> <字节> <停止>

My question is, how do I write that command using Python? 我的问题是,如何使用Python编写该命令? I think i'm looking for something like this 我想我正在寻找这样的东西

import smbus
b = smbus.SMBus(1)
b.write_byte_data(0x31, 0x35, 0x30, 0x38)

which I'd like to mean "send command 5 (0x35) to device 0x31, digit 0 (0x30) and display the number 8 (0x38)" but wrrite_byte_data does not accept that number of arguments. 我想说的是“将命令5(0x35)发送到设备0x31,数字0(0x30)并显示数字8(0x38)”但wrrite_byte_data不接受该数量的参数。 I have also tried using write_i2c_block_data() which looks more hopeful but again I cannot work out how to use the functions correctly. 我也尝试过使用write_i2c_block_data()看起来更有希望,但我再也无法弄清楚如何正确使用这些函数。

I feel that I am facing a BCK problem here but any help would be appreciated. 我觉得我在这里遇到了BCK的问题,但是任何帮助都会受到赞赏。

edit Ok, I have tried 编辑好的,我试过了

b.write_block_data(0x31, 5, [8, 0x38] ) b.write_block_data(0x31,5,[8,0x38])

which makes the number 8 appear on digit 3 这使得数字8出现在数字3上

b.write_block_data(0x31, 5, [2] ) b.write_block_data(0x31,5,[2])

makes 2 appear on digit 2. I've not yet worked out how to place anything on digit 1 or 4 yet. 使2出现在数字2上。我还没有弄清楚如何在数字1或4上放置任何东西。

b.write_block_data(0x31, 4, [44]) # just entering random stuff now b.write_block_data(0x31,4,[44])#现在正在输入随机内容

increases the brightness and then makes the SSD unresponsive. 增加亮度,然后使SSD无响应。

So I'm getting closer, 所以我越走越近了

edit and SOLVED 编辑并解决

In the end it was ridiculously easy. 最后它非常简单。

b.write_i2c_block_data(0x31, 5, [0, 8]) # write number 8 to digit 0
b.write_i2c_block_data(0x31, 5, [4, 5]) # write number 5 to digit 4 etc etc

According to http://www.raspberry-projects.com/pi/programming-in-python/i2c-programming-in-python/using-the-i2c-interface-2 the function write_block_data is not very good. 根据http://www.raspberry-projects.com/pi/programming-in-python/i2c-programming-in-python/using-the-i2c-interface-2 ,函数write_block_data不是很好。

write_block_data(int addr,char cmd,long vals[]) write_block_data(int addr,char cmd,long vals [])

Write up to 32 bytes to a device. 最多可写入32个字节到设备。 This function adds an initial byte indicating the length of the vals array before the valls array. 此函数添加一个初始字节,指示valls数组之前的vals数组的长度。 Use write_i2c_block_data instead! 请改用write_i2c_block_data!

b.write_i2c_block_data(0x31, 5, [0, 8]) # write number 8 to digit 0
b.write_i2c_block_data(0x31, 5, [4, 5]) # write number 5 to digit 4 etc etc

Solved the issue for me! 解决了我的问题! All other commands in the spec now function as intended! 规范中的所有其他命令现在都按预期运行!

The page at http://www.raspberry-projects.com/pi/programming-in-python/i2c-programming-in-python/using-the-i2c-interface-2 informed me that the function I was using was not ideal. http://www.raspberry-projects.com/pi/programming-in-python/i2c-programming-in-python/using-the-i2c-interface-2上的页面告诉我,我使用的功能不是理想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM