You are here

Simple I2C Master Module Example

The following example shows how to operate with I2C master module. You can find the complete example in the “..\Program Files\Diolan\DLN\examples\c_cpp\examples\simple” folder after DLN setup package installation.

C/C++
#include "..\..\..\common\dln_generic.h"
#include "..\..\..\common\dln_i2c_master.h"
#pragma comment(lib, "..\\..\\..\\bin\\dln.lib")


int _tmain(int argc, _TCHAR* argv[])
{
	// Open device
	HDLN device;
	DlnOpenUsbDevice(&device);

	// Set frequency
	uint32_t frequency;
	DlnI2cMasterSetFrequency(device, 0, 100000, &frequency);
	// Enable I2C master
	uint16_t conflict;
	DlnI2cMasterEnable(device, 0, &conflict);

	// Prepare output buffer
	uint8_t output[8], input[8];
	for (int i = 0; i < 8; i++) output[i] = i;
	// Write bytes 
	DlnI2cMasterWrite(device, 0, 0x50, 1, 0, 8, output);

	// Read bytes
	DlnI2cMasterRead(device, 0, 0x50, 1, 0, 8, input);
	// Print input data
	for (int i = 0; i < 8; i++) printf("%02x ", input[i]);

	// Disable I2C master
	DlnI2cMasterDisable(device, 0);
	// Close device
	DlnCloseHandle(device);
	return 0;
}

  • Line 1:#include "..\..\..\common\dln_generic.h"

    The dln_generic..h header file declares functions and data structures for the generic interface.

  • Line 2:#include "..\..\..\common\dln_i2c_master.h"

    The dln_i2c_master.h header file declares functions and data structures for the I2C master interface.

  • Line 3:#pragma comment(lib, "..\\..\\..\\bin\\dln.lib")

    Use dln.lib library while project linking.

  • Line 10:DlnOpenUsbDevice(&device);

    The function establishes the connection with the DLN adapter. This application uses the USB connectivity of the adapter. For additional options, refer to the Device Opening & Identification section.

  • Line 14:DlnI2cMasterSetFrequency(device, 0, 100000, &frequency);

    This function sets frequency on the I2C bus. It is set in Hertz. Any frequency value can be provided to the function, but only device compatible frequency will be set. You can read the actual frequency value by providing pointer to the unsigned 32-bit integer variable. You can read more about I2C bus speed and frequency by navigating to I2C Speed and Frequency section.

  • Line 17:DlnI2cMasterEnable(device, 0, &conflict);

    This function enables I2C master module.

  • Line 21:for (int i = 0; i < 8; i++) output[i] = i;

    Fill output array with the values from 0 to 8. It will be used as data buffer for sending it via I2C bus.

  • Line 23: DlnI2cMasterWrite(device, 0, 0x50, 1, 0, 8, output);

    This function sends provided data buffer via I2C bus to connected I2C slave device. To send data properly to slave device it is required to provide also slave device address and memory address. You can read more about I2C addressing at I2C Addresses.

  • Line 26: DlnI2cMasterRead(device, 0, 0x50, 1, 0, 8, &input);

    This function reads data from the I2C slave device. The parameters are almost similar to the data writing process.

  • Line 28: for (int i = 0; i < 8; i++) printf("%02x ", input[i]);

    Print to console data, which was read from the I2C slave device.

  • Line 31: DlnI2cMasterDisable(device, 0);

    Disable I2C master port.

  • Line 33: DlnCloseHandle(device);

    Closing handle to the previously opened DLN-series adapter.

Rating: 
No votes yet

User login