2.54.14 I2Cx_BusScan Function
C
/* x = I2C instance number */
/* I2C master mode */
bool I2Cx_BusScan(uint16_t start_addr, uint16_t end_addr, void* pDevicesList, uint8_t* nDevicesFound)
Summary
Scans number of slaves in the bus.
Description
This function scans the I2C bus to detect devices within a specified address range. It attempts communication with each address from start_addr to end_addr and records the addresses of devices that acknowledge the request. Detected device addresses are stored in a user-provided buffer - pDevicesList, and the total number of devices found is updated in a nDevicesFound variable. The function returns false if pDevicesList and/or nDevicesFound is null or an error occurs. On success, it returns true after completing the scan.
Precondition
None
Parameters
Param | Description |
---|---|
start_addr | The starting address of the target devices to scan. |
end_addr | The ending address of the target devices to scan. |
pDevicesList | A pointer to a user provided buffer where detected device address will be stored. |
nDevicesFound | A pointer to a variable where number of detected devices will be updated. |
Returns
true - pDevicesList and nDevicesFound are not null, and the scan completes sucessfully.
false - pDevicesList and nDevicesFound is null, or an error occurs during the scan
Example
uint8_t listOfDevicesFound[20] = {0}; uint8_t numDevicesFound = 0; bool status = I2C0_BusScan(0x10, 0x6E, (void*)listOfDevicesFound, &numDevicesFound); if (status == true) { //numDevicesFound indicates the number of devices found on the bus //listOfDevicesFound contains the address of the I2C target devices discovered during the bus scan }
Remarks
None.