Main Content

ni845x

NI USB-845x connection

Since R2023a

    Description

    An ni845x object represents a connection to a NI™ USB-8451 or USB-8452 I2C/SPI Interface Device. The NI USB-845x controller can have one or more I2C peripheral devices connected to it. To configure and communicate with a peripheral device on the NI USB-845x controller, you must first create an ni845x object and then use the device function. You can connect to multiple peripheral devices with a single ni845x object. The ni845x object in MATLAB® always has the I2C controller role and cannot be used in the peripheral role.

    Note

    To use the ni845x interface, you must have the Instrument Control Toolbox™ Support Package for National Instruments™ NI-845x I2C/SPI Interface installed. For more information, see Install Instrument Control Toolbox Support Package for National Instruments NI-845x I2C/SPI Interface.

    Creation

    Description

    controller = ni845x(serialNumber) creates a connection to the NI USB-8451 or USB-8452 I2C/SPI Interface Device specified by the serial number serialNumber. You can find the serial number for your NI USB-845x controller by calling ni845xlist.

    The input serialNumber sets the SerialNumber property.

    example

    controller = ni845x(serialNumber,Name=Value) creates a connection to the NI USB-845x controller board and sets properties using one or more name-value arguments. Set the VoltageLevel, EnablePullupResistors, and OutputDriverType properties using name-value arguments as Name1=Value1,...,NameN=ValueN, where Name is the property name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the arguments does not matter. If you do not set the value of any of these properties using name-value arguments, MATLAB sets them to their default value. You can change the values of these properties later using dot notation.

    controller = ni845x, without arguments, attempts to create a connection to the last successfully connected NI USB-845x controller board. If you changed the values of the VoltageLevel, EnablePullupResistors, or OutputDriverType properties in your previous connection, the modified values are maintained when you connect again. If you have not previously connected to an NI USB-845x controller, using this syntax creates a connection to the first available controller in the ni845xlist output.

    example

    Properties

    expand all

    This property is read-only.

    Controller board model, specified as "NI USB-8451" or "NI USB-8452".

    Example: controller.Model returns the model.

    Data Types: string

    This property is read-only.

    Unique alphanumeric identifier of the NI USB-845x controller board, specified as a string scalar. This property can be set only at object creation.

    Example: controller.SerialNumber returns the unique identifier of the NI USB-845x controller.

    Data Types: string

    Since R2024a

    Label for identifying connection, specified as a string. Use Tag to apply a label to a connection that you can use later to access the connection using ni845xfind. Doing so can be useful when you open a connection in one function and use a different function to perform operations on the connection. It is also useful for locating and accessing connections in app callbacks.

    Example: controller.Tag = "pSupply"

    Data Types: string

    This property is read-only.

    Digital pins available on NI USB-845x controller, specified as a string array. Connect your I2C peripheral devices to the controller using these pins. Refer to the NI USB-845x documentation for a description of each pin. Configure and communicate with these pins using configureDigitalPin, readDigitalPin, and writeDigitalPin.

    Example: controller.AvailableDigitalPins returns the digital pins of the NI USB-845x controller.

    Data Types: string

    Voltage level of all pins in volts, specified as 3.3, 1.2, 1.5, 1.8, or 2.5. For USB-8451 controllers, the only possible value is 3.3 and this property is read-only. For USB-8452 controllers, you can set the value to 1.2, 1.5, 1.8, 2.5, or 3.3.

    Example: controller.VoltageLevel = 1.2 sets the voltage level of the USB-8452 pins to 1.2 V.

    If you do not want to use the default voltage level of 3.3 V, set your desired voltage level immediately upon connection by specifying VoltageLevel as a name-value argument during ni845x object creation.

    Example: controller = ni845x(serialNumber,VoltageLevel=1.2); sets the voltage level of the USB-8452 pins to 1.2 V during object creation using a name-value argument.

    Data Types: double

    Setting to enable or disable internal pullup resistors for I2C lines, specified as a numeric or logical 1 (true) or 0 (false). For USB-8451 controllers, the only possible value is false, since it does not have any internal pullup resistors.

    Example: controller.EnablePullupResistors = true enables internal pullup resistors.

    Data Types: logical

    Driver type used when sourcing digital I/O (DIO) signals on NI USB-845x controller, specified as "push-pull" or "open-drain".

    Example: controller.OutputDriverType = "open-drain" sets the driver type to open-drain.

    Data Types: string

    This property is read-only.

    Modes of digital pins of NI USB-845x controller, specified as a table. The table has the columns Pin and Mode. Pin lists the digital pins specified by AvailableDigitalPins and Mode lists the pin mode of each of these pins as input or output.

    When you create an ni845x object, all the pins are input by default. Change pin modes using configureDigitalPin. You can read logic values from input pins using readDigitalPin and send logic level values to output pins using writeDigitalPin.

    Example: controller.DigitalPinModes returns a table with the pin modes of the digital pins on the NI USB-845x controller.

    Data Types: table

    Object Functions

    configureDigitalPinSet digital pin mode on controller
    readDigitalPinRead logic level value of digital pin on controller
    writeDigitalPinWrite logic level value to digital pin on controller
    scanI2CBusScan for I2C peripheral devices connected to controller board

    Examples

    collapse all

    Discover and connect to an NI USB-845x controller board on your machine.

    Use ni845xlist to list all available NI USB-845x controller boards connected to your machine.

    list = ni845xlist
    list = 
    
      1×2 table
    
                 Model        SerialNumber
             _____________    ____________
    
        1    "NI USB-8452"     "01F26E0A" 
    
    

    Use the value of SerialNumber to create a connection to your NI USB-845x.

    controller = ni845x(list.SerialNumber(1))
    controller = 
    
      NI845x with properties:
    
                       Model: "NI USB-8452"
                SerialNumber: "01F26E0A"
        AvailableDigitalPins: ["P0.0"    "P0.1"    "P0.2"    "P0.3"    "P0.4"    "P0.5"    "P0.6"    "P0.7"]
    
      Show all properties, functions

    You can now connect to any I2C peripheral devices on the NI USB-845x controller by using the device function.

    Connect to an NI USB-845x Interface Device and configure the pins on it.

    Connect to the NI USB-845x using its serial number. In this example, the NI USB-845x controller board that is connected to the computer has the serial number 01F26E0A.

    controller = ni845x("01F26E0A");

    View the available digital I/O (DIO) pins on the controller and their respective pin modes by using the DigitalPinModes property.

    controller.DigitalPinModes
    ans =
    
      8×2 table
    
         Pin       Mode  
        ______    _______
    
        "P0.0"    "input"
        "P0.1"    "input"
        "P0.2"    "input"
        "P0.3"    "input"
        "P0.4"    "input"
        "P0.5"    "input"
        "P0.6"    "input"
        "P0.7"    "input"
    

    Configure the DIO(0) pin, P0.0, as output.

    configureDigitalPin(controller,"P0.0","output")

    Since the DIO(0) pin is an output pin, you can now send it a logic high level using writeDigitalPin.

    Connect to an NI USB-845x controller board and scan it for connected I2C peripheral devices.

    Connect to the NI USB-845x using its serial number. In this example, the NI USB-845x controller board that is connected to the computer has a serial number 01F26E0A.

    controller = ni845x("01F26E0A");

    Make sure that your I2C peripheral devices are physically connected to the controller. Scan for connected I2C peripheral devices. The scanI2CBus function returns a list of the I2C addresses of these peripheral devices.

    i2cAddresses = scanI2CBus(controller)
    i2cAddresses = 
    
      1×2 string array
    
        "0x48"    "0x53"

    In this example, two peripheral devices are connected. You can now create a connection to one or more of them using the device function with one of the listed I2C addresses. Refer to your peripheral device's datasheet to determine its I2C address.

    When you use ni485x in a script or at the MATLAB command line, the result is a connection to a USB-845x controller, represented by an object in the MATLAB workspace.

    n1 = ni845x("0180D442")
    n1 = 
    
      NI845x with properties:
    
                       Model: "NI USB-8451"
                SerialNumber: "0180D442"
                         Tag: "Accel"
        AvailableDigitalPins: ["P0.0"    "P0.1"    "P0.2"    "P0.3"    "P0.4"    "P0.5"    "P0.6"    "P0.7"]
    

    When no references to the same connection exist in other variables, you can disconnect the controller by clearing the workspace variable.

    clear(n1)

    Use ni485xfind to confirm that the connection is closed.

    ni845xfind
    ans =
    
         []

    When you have an ni845x connection that exists in the MATLAB workspace or is saved as a class property or app property, the ni845x object might not be accessible in a different function or app callback. In this case, you can use ni845xfind to find and delete the connection.

    N = ni845xfind
    N = 
    
      NI845x with properties:
    
                       Model: "NI USB-8451"
                SerialNumber: "0180D442"
                         Tag: ""
        AvailableDigitalPins: ["P0.0"    "P0.1"    "P0.2"    "P0.3"    "P0.4"    "P0.5"    "P0.6"    "P0.7"]
    

    To close this connection, delete N.

    delete(N)

    This command deletes the ni485x object and disconnects the device. If you want to reconnect to the device, you must create a new interface with ni485x.

    After the deletion, calling ni485xfind confirms that there are no existing connections.

    ni845xfind
    ans =
    
         []

    Note that the variable N is still present in the workspace, but it is now an invalid handle.

    N
    N = 
    
      handle to deleted NI845x

    The variable persists after deletion of the interface because ni845x is a handle object. (For more information about this type of object, see Handle Object Behavior.) You can use clear to remove the invalid handle from the workspace.

    clear N

    Version History

    Introduced in R2023a

    expand all