1

我在 Raspberry Pi 3 B+ 上使用 Python 3 操作 AD5272 时遇到问题。AD5272是带I2C接口的数控变阻器(我的部分电阻为20 kOhm)。

问题如下:

A 和 W 端子之间的电阻不会改变我尝试设置的任何位置,并且保持在 10KOhm 左右。(默认情况下,当 AD5272 开启时,将在那里设置全电阻的一半)。当我从 RDAC 读取电阻时 - 读取零电阻。

这是我的代码:

#!/usr/bin/python3
import smbus2
import time, os

class AD527x ():
    # command bits which are dependant on I2C address of device
    def __init__ (self, bus=1, address=0x2E, resistance = 20000, positions = 1023, reset = False):
        self.bus = smbus2.SMBus(bus)
        self.positions = positions
        self.address = address
        self.resistance = resistance

    def write_position (self, position):
        # sets the rheostat to certain position

        # if wiper position is higher than maximal
        if position > self.positions:
            position = self.positions

        # approximate resistance of rheostat
        resistance = self.resistance*position/self.positions

        # for AD5274 needs to be shifted for 2 digits left
        if self.positions == 256:
            position = position << 2

        print ("Binary representation of position for RDAC : " + bin (position))

        # Writing position is sneding 2 bytes one by one

        # MSB Data: 0 0 C3 C2 C1 C0 D9 D8
        # For writing command bytes: C3 = 0; C2 = 0; C1 = 0; C0 = 1 
        # 0 0  0  0  0  1  ?  ?
        # ? ? - MSB of 10-digit binary representation of wiper position between 0 and  1023
        # two first positions : D9_8 = (position & 0b1100000000) >> 8

        MSB = (1 << 3)  | ((position & 0b1100000000) >> 8)
        print ("MSB : " + bin(MSB))
        # take last 8 bits
        LSB = position & 0b11111111
        print ("LSB : " + bin(LSB))
        print ("All Bytes : " + bin((MSB << 8) +LSB))
        print ("Value : " + str(resistance))

        self.bus.write_i2c_block_data(self.address, 0, [MSB, LSB])
        self.read_position()

    def read_position (self):
        # reads current position of rheostat

        # prepare the circuit to send data
        # MSB Data: 0 0 C3 C2 C1 C0 D9 D8
        # For reading command bytes: C3 = 0; C2 = 0; C1 = 1; C0 = 0
        # 0 0  0  0  1  0  ?  ?
        # ? ? - Doen't matter - Just use zeros
        # LSB - doesn't matter, just using 0b00000000

        MSB = 0b00001000
        LSB = 0b00000000
        self.bus.write_i2c_block_data(self.address, 0, [MSB, LSB])

        #read 2 bytes from RDAC
        a = self.bus.read_byte(self.address)
        time.sleep (0.005)
        b = self.bus.read_byte(self.address)
        value = ((a << 8) | b )
        print (value)
        # take 10 lats bits only
        value = value & 0b1111111111
        print (value)

def main():
    device = AD527x()
    device.write_position(1023)

if __name__ == "__main__":
    main()

输出是:

Binary representation of position for RDAC : 0b1111111111
MSB : 0b1011
LSB : 0b11111111
All Bytes : 0b101111111111
Value : 20000.0
4096
0

电脑:树莓派 3,型号 B+

操作系统:Raspbian 9

Python版本:3.5.3

I2C 封装:smbus2

零件编号:AD5272BRMZ-20

数据表:http: //www.farnell.com/datasheets/1706490.pdf

产品链接:http ://www.newark.com/webapp/wcs/stores/servlet/ProductDisplay?catalogId=10001&langId=-1&urlRequestType=Base&partNumber=52R8114&storeId=10194

我做错了什么以及如何解决?

零件的接线经过三重检查。无论我试图在那里写入什么值,从 RDAC 读取的内容总是相同的。我尝试了两个命令:

self.bus.write_i2c_block_data(self.address, 0, [MSB, LSB])

self.bus.write_byte (self.address, MSB)
self.bus.write_byte (self.address, LSB)

结果总是一样的:阻力没有改变。用外部欧姆计装置检查电阻。

请帮忙!

4

1 回答 1

0

我刚刚遇到了同样的问题。我还没有研究过与 Python 的接口,但这可能会有所帮助。

在我的电路中,AD5272 I2C 使用 GND 寻址,我将端子 A 连接到 VDD (5V),将 W 与 100K 电阻串联到 GND。

首先,我确保设备可以通过 Linux 驱动程序运行,模块应该已经内置。作为根:

$ echo ad5272 0x2f > /sys/bus/i2c/devices/i2c-1/new_device
$ dmesg | tail -n 2
[  711.195428] i2c i2c-1: new_device: Instantiated device ad5272 at 0x2f
[  711.242537] ad_dpot 1-002f: ad5272 1024-Position Digital Potentiometer registered
$ cat /sys/bus/i2c/devices/1-002f/rdac0 
511
$ echo 0 > /sys/bus/i2c/devices/1-002f/rdac0
$ cat /sys/bus/i2c/devices/1-002f/rdac0
0
$ echo 1023 > /sys/bus/i2c/devices/1-002f/rdac0
$ cat /sys/bus/i2c/devices/1-002f/rdac0
1023

测量从 W 到 GND 的电压在 0 时得到 ~5V,在 1023 时得到 ~2.5V。

然后我重复了一个连接到 I2C 总线的逻辑分析仪。写入命令是:

0x5e 0x1c 0x03 0x5e 0x04 0x00
0x5e 0x1c 0x03 0x5e 0x07 0xff

这应该有望提供一个提示,重新阅读数据表中关于“写保护”和“控制寄存器描述”的部分。在我的设置中,我只想使用数字接口而不是存储的内存进行控制,所以我从用户空间复制了一些改动:

sudo i2cset 1 0x2f 0x1c 0x02 b
sudo i2cset 1 0x2f 0x04 0x00 b
sudo i2cset 1 0x2f 0x07 0xff b
于 2017-10-08T16:57:08.207 回答