Jump to content
  • 0

AD3 - Python script


Joao Paulo

Question

Hi,

I was trying to modify the example test_I2C_CLS-TMP2.py( https://github.com/Digilent/WaveForms-SDK-Getting-Started-PY) to read LM75A from NXP, but I could not get rid of this error.

  • I checked i2c address ( 0x48 7bits or 0x90 bits). I also tried with both

image.thumb.png.9185a3056bf81e5909224dfe335b84dd.png

here is the code not working:

from WF_SDK import device, supplies     # import instruments
from WF_SDK.protocol import i2c         # import protocol instrument
 
from time import sleep          # needed for delays
 
device_name = "Analog Discovery 3"
 
"""-----------------------------------------------------------------------"""
 
# connect to the device
device_data = device.open()
device_data.name = device_name
 
"""-----------------------------------"""
 
# define i2c addresses
TMP2_address = 0x48
 
# initialize the i2c interface on DIO0 and DIO1
i2c.open(device_data, sda=1, scl=0)
 
try:
    # repeat
    while True:

 
        # read the temperature
        message, error = i2c.read(device_data, 2, TMP2_address)   # read 2 bytes
        value = (int(message[0]) << 8) | int(message[1])    # create integer from received bytes
        print(value/256.0) 
 
        # delay 1s
        sleep(1)
 
except KeyboardInterrupt:
    # exit on Ctrl+C
    pass
 
# reset the interface
i2c.close(device_data)
 
# stop and reset the power supplies
supplies_data.master_state = False
supplies.switch(device_data, supplies_data)
supplies.close(device_data)
 
"""-----------------------------------"""
 
# close the connection
device.close(device_data)

In the Waveforms, it was fine. Also, I tried the test_temperature.py and it runs fine too. Then I modified the Digital_i2c.py and it worked fine. Here is the working code. I know it works but I would prefer to use the nice ready modules already written. Any ideas?

"""
   DWF Python Example
   Author:  Digilent, Inc.
   Revision:  2018-07-23

   Requires:                       
       Python 2.7, 3
"""

from ctypes import *
import math
import sys
import time
import struct

if sys.platform.startswith("win"):
    dwf = cdll.LoadLibrary("dwf.dll")
elif sys.platform.startswith("darwin"):
    dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")
else:
    dwf = cdll.LoadLibrary("libdwf.so")

hdwf = c_int()

print("Opening first device")
#dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
# device configuration of index 3 (4th) for Analog Discovery has 16kS digital-in/out buffer
dwf.FDwfDeviceConfigOpen(c_int(-1), c_int(3), byref(hdwf)) 

if hdwf.value == 0:
    print("failed to open device")
    szerr = create_string_buffer(512)
    dwf.FDwfGetLastErrorMsg(szerr)
    print(str(szerr.value))
    quit()

print("Configuring I2C...")

iNak = c_int()

dwf.FDwfDigitalI2cRateSet(hdwf, c_double(1e5)) # 100kHz
dwf.FDwfDigitalI2cSclSet(hdwf, c_int(0)) # SCL = DIO-0
dwf.FDwfDigitalI2cSdaSet(hdwf, c_int(1)) # SDA = DIO-1
dwf.FDwfDigitalI2cClear(hdwf, byref(iNak))
if iNak.value == 0:
    print("I2C bus error. Check the pull-ups.")
    quit()
time.sleep(1)

rgTX = (c_ubyte*1)(0)
rgRX = (c_ubyte*2)()
while True:
    #print("Write and Read with reStart:")
    dwf.FDwfDigitalI2cWriteRead(hdwf, c_int(0x48<<1), rgTX, c_int(1), rgRX, c_int(2), byref(iNak)) # write 1 byte restart and read 16 bytes
    if iNak.value != 0:
        print("NAK "+str(iNak.value))

    print(list(rgRX))

    # Convert temperature data to Celsius
    raw_temperature = struct.unpack('>h', bytes(rgRX))[0]
    temperature_celsius = raw_temperature / 256.0
    print(temperature_celsius)
    time.sleep(1)

dwf.FDwfDeviceCloseAll()

 

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...