2017-09-15 170 views
1

我在Raspberry Pi 3 B +上使用Python 3操作AD5272時出現問題。 AD5272是帶有I2C接口的數字控制變阻器(我的部件具有20 kOhm的電阻)。在Raspberry Pi 3 B +上使用Python 3和smbus2操作AD5272 B +

問題是以下幾點:

A和W端之間的電阻不改變任何位置我試圖建立和周圍爲10Mohm停留。 (默認情況下,當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 

計算機:Rapberry裨3,模型B +

OS: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) 

結果總是相同的:阻力不會改變。用外部歐姆表裝置檢查電阻。

請幫忙!

回答

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的電壓,得到〜5V在0和〜2.5V在1023

我然後與連接到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 
+0

我會盡力做到這一點。現在我認爲這個問題可能是在I2C接口的Python實現中。我注意到一些組件重新調用特定的I2C時鐘速度。 – Iegor

相關問題