Main Content

readRegister

Read data from I2C peripheral device register

Since R2023a

    Description

    data = readRegister(peripheral,registerAddress) reads a single value of data stored in the specified register memory address registerAddress of the I2C peripheral device peripheral and returns the data as a uint8 value. This operation is done in a single I2C transaction.

    example

    data = readRegister(peripheral,registerAddress,count) reads the specified number of values count from contiguous registers starting at registerAddress and returns the data as a uint8 value.

    data = readRegister(___,datatype) reads data in the form specified by datatype.

    Examples

    collapse all

    Connect to an I2C peripheral device on an NI™ USB-845x controller and read data from a register 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");

    Connect to an I2C peripheral device on the controller board. In this example, the peripheral has an address of 0x48.

    peripheral = device(controller,I2CAddress="0x48");

    Read two bytes of data from register address 0.

    data = readRegister(controller,0,2);
    ans =
    
         0     0
    

    Input Arguments

    collapse all

    I2C peripheral device, specified as a device object.

    Example: readRegister(peripheral,registerAddress) reads from a register on the I2C peripheral device object peripheral connected to an Aardvark™ or NI USB-845x controller.

    Register address to read from on the I2C peripheral device, specified as a number. Refer to the peripheral device documentation or data sheet for more information about valid register addresses.

    Example: readRegister(peripheral,0) reads from a register at address 0.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Number of values to read from continuous registers starting at registerAddress, specified as a positive integer.

    Example: readRegister(peripheral,registerAddress,5) reads five values.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Size and format of each value, specified as a character vector or string scalar. datatype determines the number of bytes to read for each value and the interpretation of those bytes as a MATLAB® data type.

    Example: readRegister(peripheral,registerAddress,"uint32") reads data as uint32.

    Data Types: char | string

    Version History

    Introduced in R2023a