""" Demonstrates I2C communication using a LabJack. The demonstration uses a MCP4661 connected to FIO0/FIO1 for the T7 or FIO4/FIO5 for the T4, and configures the I2C settings. Then a read, write the Digital Potentio registered. """ from random import randrange from labjack import ljm import time # Open first found LabJack #handle = ljm.openS("ANY", "ANY", "ANY") # Any device, Any connection, Any identifier handle = ljm.openS("T7", "ANY", "192.168.3.7") # T7 device, Any connection, Any identifier #handle = ljm.openS("T4", "ANY", "ANY") # T4 device, Any connection, Any identifier #handle = ljm.open(ljm.constants.dtANY, ljm.constants.ctANY, "ANY") # Any device, Any connection, Any identifier info = ljm.getHandleInfo(handle) print("Opened a LabJack with Device type: %i, Connection type: %i,\n" "Serial number: %i, IP address: %s, Port: %i,\nMax bytes per MB: %i" % (info[0], info[1], info[2], ljm.numberToIP(info[3]), info[4], info[5])) deviceType = info[0] # Configure the I2C communication. if deviceType == ljm.constants.dtT4: # Configure FIO4 and FIO5 as digital I/O. ljm.eWriteName(handle, "DIO_INHIBIT", 0xFFFCF) ljm.eWriteName(handle, "DIO_ANALOG_ENABLE", 0x00000) # For the T4, using FIO4 and FIO5 for SCL and SDA pins. FIO0 to FIO3 are # reserved for analog inputs, and digital lines are required. ljm.eWriteName(handle, "I2C_SDA_DIONUM", 5) # SDA pin number = 5 (FIO5) ljm.eWriteName(handle, "I2C_SCL_DIONUM", 4) # SCL pin number = 4 (FIO4) else: # For the T7 and other devices, using FIO0 and FIO1 for the SCL and SDA # pins. ljm.eWriteName(handle, "I2C_SDA_DIONUM", 1) # SDA pin number = 1 (FIO1) ljm.eWriteName(handle, "I2C_SCL_DIONUM", 0) # SCL pin number = 0 (FIO0) # Speed throttle is inversely proportional to clock frequency. 0 = max. ljm.eWriteName(handle, "I2C_SPEED_THROTTLE", 65516) # Speed throttle = 65516 (~100 kHz) # Options bits: # bit 0: 1 = Reset the I2C bus before attempting communication. # bit 1: 0 = Restarts will use a stop and a start, 1 = Restarts will not use a stop. # bit 2: 1 = disable clock stretching. ljm.eWriteName(handle, "I2C_OPTIONS", 2) # ---------------------------------------- MCP4661 ---------------------------------------- print("\n--- MCP4661 - Digital Potensio ---\n") addwDigPot = 0x28 + 1 ljm.eWriteName(handle, "I2C_SLAVE_ADDRESS", addwDigPot) def Read_Status(): # ----------------------- Read Status Register (5h + cmd1:0=Ch --> 5Ch) ----------------------- # x x x x EEWA WL1 WL0 WP # EEWA --> EEPROM Write Active status bit (1 = Write cycling is currently occuring) # WL1 --> WiperLock Status bit for Resistor Network 1 (1 = Write protected) # WL0 --> WiperLock Status bit for Resistor Network 0 (1 = Write protected) # WP --> EEPROM Write Protect Status bit (1 = EEPROM Write protected) print("\n--- MCP4661 - Read Status Register ---\n") numBytesTx = 1 numBytesRx = 1 aBytes = [0x5C] print("%s" % " ".join([("%.0f" % val) for val in aBytes])) ljm.eWriteName(handle, "I2C_NUM_BYTES_TX", numBytesTx) # Set the number of bytes to transmit ljm.eWriteName(handle, "I2C_NUM_BYTES_RX", numBytesRx) # Set the number of bytes to receive ljm.eWriteNameByteArray(handle, "I2C_DATA_TX", numBytesTx, aBytes) ljm.eWriteName(handle, "I2C_GO", 1) # Do the I2C communications. aBytes = ljm.eReadNameByteArray(handle, "I2C_DATA_RX", numBytesRx) print("read %i bytes = %s" % (numBytesRx, " ".join([("%.0f" % val) for val in aBytes]))) jmlhack = ljm.eReadName(handle, "I2C_ACKS") print("Jmlh ACK: %i\n" % jmlhack) # ------------------------- Write Volatile Memory Add (0h, 1h, 4h) ------------------------- # AD3 AD2 AD1 AD0 Cmd1 Cmd0 x D8 --> 4 bit pd hight byte sbg Address memory map, # Cmd1:0 = 00 --> Write # Cmd1:0 = 01 --> Increment # Cmd1:0 = 10 --> Decrement # Cmd1:0 = 11 --> Read # Volatile Wiper0 (00h) = 0xC0 # Volatile Wiper1 (10h) = 0x80 # Volatile TCON (40h) = no change print("\n--- MCP4661 - Write Volatile Wiper0 = 0xC0, Wiper1 = 0x80 ---\n") numBytesTx = 4 numBytesRx = 0 aBytes = [0x00, 0xc0, 0x10, 0x80] # AddMemoryWiper0, ValWiper0, AddMemoryWiper1, ValWiper1 print("%s" % " ".join([("%.0f" % val) for val in aBytes])) ljm.eWriteName(handle, "I2C_NUM_BYTES_TX", numBytesTx) # Set the number of bytes to transmit ljm.eWriteName(handle, "I2C_NUM_BYTES_RX", numBytesRx) # Set the number of bytes to receive ljm.eWriteNameByteArray(handle, "I2C_DATA_TX", numBytesTx, aBytes) aBytes = ljm.eReadNameByteArray(handle, "I2C_DATA_RX", numBytesRx) ljm.eWriteName(handle, "I2C_GO", 1) # Do the I2C communications. print("read %i bytes = %s" % (numBytesRx, " ".join([("%.0f" % val) for val in aBytes]))) jmlhack = ljm.eReadName(handle, "I2C_ACKS") print("Jmlh ACK: %i\n" % jmlhack) ''' numBytesRx = 12 for idx in range(10): print("\n--- %i" % idx) aBytes = [0]*numBytesRx aBytes = ljm.eReadNameByteArray(handle, "I2C_DATA_RX", numBytesRx) print("read %i bytes = %s" % (numBytesRx, " ".join([("%.0f" % val) for val in aBytes]))) jmlhack = ljm.eReadName(handle, "I2C_ACKS") print("Jmlh ACK: %i\n" % jmlhack) ''' # ----------------------- Read Status Register (5h + cmd1:0=Ch --> 5Ch) ----------------------- Read_Status() # ----------------------- WRITE Status Register (5Ch) on High Voltage Mode (A0 = 1) ----------------------- # Add DecrCmd(x8h) IncrCmd(x4h) # 2xh WL0 Enable WL0 disabled # 3xh WL1 Enable WL1 disabled # Fxh WP Enabled WP disabled print("\n--- MCP4661 - DISABLE WL0, WL1, WP ---\n") numBytesTx = 3 numBytesRx = 0 aBytes = [0x28, 0x38, 0xF8] print("%s" % " ".join([("%.0f" % val) for val in aBytes])) ljm.eWriteName(handle, "I2C_NUM_BYTES_TX", numBytesTx) # Set the number of bytes to transmit ljm.eWriteName(handle, "I2C_NUM_BYTES_RX", numBytesRx) # Set the number of bytes to receive ljm.eWriteNameByteArray(handle, "I2C_DATA_TX", numBytesTx, aBytes) aBytes = ljm.eReadNameByteArray(handle, "I2C_DATA_RX", numBytesRx) ljm.eWriteName(handle, "I2C_GO", 1) # Do the I2C communications. print("read %i bytes = %s" % (numBytesRx, " ".join([("%.0f" % val) for val in aBytes]))) jmlhack = ljm.eReadName(handle, "I2C_ACKS") print("Jmlh ACK: %i\n" % jmlhack) # ----------------------- Read Status Register (5h + cmd1:0=Ch --> 5Ch) ----------------------- Read_Status() # ----------------------- Read all memory map ----------------------- print("\n--- MCP4661 - Read all memory map ---\n") numBytesTx = 1 numBytesRx = 32 aBytes = [0x0c] print("%s" % " ".join([("%.0f" % val) for val in aBytes])) ljm.eWriteName(handle, "I2C_NUM_BYTES_TX", numBytesTx) # Set the number of bytes to transmit ljm.eWriteName(handle, "I2C_NUM_BYTES_RX", numBytesRx) # Set the number of bytes to receive ljm.eWriteNameByteArray(handle, "I2C_DATA_TX", numBytesTx, aBytes) aBytes = ljm.eReadNameByteArray(handle, "I2C_DATA_RX", numBytesRx) ljm.eWriteName(handle, "I2C_GO", 1) # Do the I2C communications. print("read %i bytes = %s" % (numBytesRx, " ".join([("%.0f" % val) for val in aBytes]))) jmlhack = ljm.eReadName(handle, "I2C_ACKS") print("Jmlh ACK: %i\n" % jmlhack) # ----------------------- WRITE Non Volatile Wiper0 (20h) & Wiper1 (30h) ----------------------- print("\n--- MCP4661 - Write Non Volatile ---\n") numBytesTx = 4 numBytesRx = 0 aBytes = [0x20, 0xA0, 0x30, 0xA0] print("%s" % " ".join([("%.0f" % val) for val in aBytes])) ljm.eWriteName(handle, "I2C_NUM_BYTES_TX", numBytesTx) # Set the number of bytes to transmit ljm.eWriteName(handle, "I2C_NUM_BYTES_RX", numBytesRx) # Set the number of bytes to receive ljm.eWriteNameByteArray(handle, "I2C_DATA_TX", numBytesTx, aBytes) aBytes = ljm.eReadNameByteArray(handle, "I2C_DATA_RX", numBytesRx) ljm.eWriteName(handle, "I2C_GO", 1) # Do the I2C communications. print("read %i bytes = %s" % (numBytesRx, " ".join([("%.0f" % val) for val in aBytes]))) jmlhack = ljm.eReadName(handle, "I2C_ACKS") print("Jmlh ACK: %i\n" % jmlhack) print("\n--- Please try unplug & plug the USB connector and see the wiper value ---\n") # Close handle ljm.close(handle)