Jump to main content

##

# 2.129.3 TWIHSx_Write Function

## C

    /* x = TWIHS instance number */

    /* TWIHS master mode */
    bool TWIHSx_Write(uint16_t address, uint8_t *pdata, size_t length)

## Summary

Writes data to the TWIHS slave

## Description

This function writes data to a TWIHS slave on the bus. The function will attempt to write length number of bytes from pdata buffer to a slave whose address is specified by address. The TWIHS Master will generate a Start condition, write the data and then generate a Stop Condition. If the slave NAKs the request or a bus error was encountered on the bus, the transfer is terminated. The application can call the TWIHSx_ErrorGet function to know the cause of the error. The function is non-blocking. It initiates bus activity and returns immediately. The transfer is then completed in the peripheral interrupt. A transfer request cannot be placed when another transfer is in progress. Calling the write function when another function is already in progress will cause the function to return false. If a callback was registered, the library will call the registered callback function when the transfer has completed.

## Precondition

TWIHSx_Initialize must have been called for the associated TWIHS instance.

## Parameters

Param | Description
--- | ---
address | 7-bit / 10-bit slave address.
pdata | pointer to source data buffer that contains the data to be transmitted
length | Number of bytes to write

## Returns

true - The request was placed successfully and the bus activity was initiated.

false - The request fails, if there was already a transfer in progress when this function was called.

## Example

    uint8_t myData [NUM_BYTES] = {'1', '0', ' ', 'B', 'Y', 'T', 'E', 'S', '!', '!',};

        if(!TWIHS1_Write( SLAVE_ADDR, &myData[0], NUM_BYTES ))
        {
            // error handling
        }

## Remarks

None