主要内容

writeRegister

R2026b

Write to register of I2C device connected to NVIDIA Jetson board

Since R2026a

    Description

    Add-On Required: This feature requires the MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms add-on.

    writeRegister(i2cObj,regAddress,data) writes the data, data, as a uint8 value to the register address, regAddress, of the I2C device i2cObj.

    example

    writeRegister(i2cObj,regAddress,data,dataPrecision) writes in the data type dataPrecision.

    example

    writeRegister(___,registerAddressWidth=width) specifies the register address width of the register to write to. For devices that use two bytes for register addresses, specify width as "2Byte".

    example

    Examples

    collapse all

    This example shows how to write to a specific register of an I2C device connected to an NVIDIA® Jetson™ board.

    Create a jetson object named hwObj, and scan the I2C buses on the Jetson hardware by using the scanI2CBus function. In this example, the Jetson board has two I2C buses, i2c-1 and i2c-8. Each bus has a device at address 0x74.

    hwObj = jetson;
    addresses = scanI2CBus(hwObj);
    addresses{:}
    ans = 
    
      struct with fields:
    
            Bus: 'i2c-1'
        Devices: {'74'}
    
    
    ans = 
    
      struct with fields:
    
            Bus: 'i2c-8'
        Devices: {'74'}

    Create an i2cdev object, i2cObj, connecting to the device on the i2c-1 bus.

    i2cObj = i2cdev(hwObj,"i2c-1",0x74);

    Write the value 42 to the register 7. By default, the Jetson board writes the value as a uint8 data type.

    regAddress = 7;
    writeRegister(i2cObj,regAddress,42);

    To write data as the int16 type, use the writeRegister function with the "int16" option. Enter this code to write the int16 value -3 to address 1.

    writeRegister(i2cObj,1,-3,"int16")

    If the I2C device uses two bytes for register addresses, specify the width argument as "2Byte". For example, if i2cObj represents a connection to a device with two-byte addressing, enter this code to write the value 43 to address 3.

    regAddress = 3;
    writeRegister(i2cObj,regAddress,43,registerAddressWidth="2Byte");
    

    Input Arguments

    collapse all

    Connection to the I2C device, specified as an i2cdev object.

    Register address, specified as a nonnegative integer. The Jetson hardware sends the address by using the number of bytes specified in the width argument. The address can be at most 255 if width is "1Byte" and at most 65335 if width is "2Byte".

    Data to write to the I2C device, specified as a vector.

    Data type, specified as "uint8", "int8", "uint16", "int16", "uint32", "int32", "uint64", or "int64".

    Register address width, specified as "1Byte" or "2Byte". Specify the width based on how many bytes the I2C device uses for addresses.

    Extended Capabilities

    expand all

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    GPU Code Generation
    Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

    Version History

    Introduced in R2026a

    expand all

    See Also

    Objects

    Functions