CCS News

Tech Note: New i2c functions for the CCS C Compiler!

Monday 06 May, 2019

The CCS C Compiler has added three new functions to the #use i2c() library when setup as an I2C master for transferring data to and from an I2C slave device, the functions are the i2c_transfer(), i2c_transfer_out() and i2c_transfer_in() functions. These functions are added to the CCS C Compiler to support some new devices whose I2C peripheral is not compatible with the legacy i2c_start(), i2c_write(), i2c_read() and i2c_stop() functions. However, these functions are available for all devices when setup as an I2C master using the hardware peripheral or doing a software I2C. Because these functions are supported on all devices, CCS recommends using them instead of the legacy functions for developing code making the code portable to all devices. Another advantage to using these functions, compared to the legacy functions, is that they perform all the I2C steps in a single function call instead of multiple function calls.

The i2c_transfer_out() function transfers data to a slave device use the following:

ack = i2c_transfer_out(SlaveAddr, wData, Count);

The above causes the master to perform an I2C start, send a write command to the specified slave address, send count bytes to slave, perform an I2C stop and return the acknowledge bit. If the acknowledge bit is an ACK, then the transfer is successful. If at any time the slave device NACKs any of the data bytes, the i2c_trasnfer_out() function immediately performs an I2C stop and return a NACK indicating the transfer was unsuccessful.

The i2c_transfer_in() function transfers data from a slave device as follows:

ack = i2c_transfer_in(SlaveAddr, rData, Count);

The above causes the master to perform an I2C start, send a read command to the specified slave address, read count bytes from slave, perform and I2C stop and return the acknowledge bit. If the acknowledge bit is an ACK, then the transfer is successful. When reading data from the a slave device the only byte of data that the slave acknowledges is the read command. If the slave NACKs the read command the i2c_transfer_in() function immediately performs an I2C stop and return a NACK indicating the transfer was unsuccessful.

The i2c_transfer() function is a combination of both the i2c_transfer_out() and i2c_transfer_in() functions, used to transfer data to a slave device or transfer data to and from a slave device. If used to transfer data to a slave device, it works the same as the i2c_transfer_out() function.

When transferring data to and from a slave device, use the following:

ack = i2c_transfer(SlaveAddr, wData, wCount, rData, rCount);

The above causes the master to perform an I2C start, send a write command to the specified slave address, send count bytes to slave, perform an I2C restart, send a read command to the specified slave address, read count bytes from slave, perform and I2C stop and return the acknowledge bit. If the acknowledge bit is an ACK, then the transfer is successful. As with the i2c_transfer_out() and i2c_transfer_in() function, if the slave NACKs any of the bytes being transferred to it the i2c_transfer() function immediately performs an I2C stop and return a NACK indicating the transfer was unsuccessful.

See 241025.c and ds1631.c in the Drivers folder where the CCS C compiler is installed for a couple examples showing how to use the i2c_transfer() function. The 241025.c driver is for a Microchip 24XX1025 I2C Serial EEPROM with 1024K bits of memory, and the ds1631.c driver is Dallas 1631 I2C temperature sensor.


Like us on Facebook. Follow us on Twitter.

About CCS:

CCS is a leading worldwide supplier of embedded software development tools that enable companies to develop premium products based on Microchip PIC® MCU and dsPIC® DSC devices. Complete proven tool chains from CCS include a code optimizing C compiler, application specific hardware platforms and software development kits. CCS' products accelerate development of energy saving industrial automation, wireless and wired communication, automotive, medical device and consumer product applications. Established in 1992, CCS is a Microchip Premier 3rd Party Partner. For more information, please visit http://www.ccsinfo.com.

PIC® MCU, MPLAB® IDE, MPLAB® ICD2, MPLAB® ICD3 and dsPIC® are registered trademarks of Microchip Technology Inc. in the U.S. and other countries.