Defines | |
#define | i2cBusCollision() PIR2_BCLIF |
#define | i2cClose() (SSPCON1 &= 0xDF) |
#define | i2cDataRdy() (SSPSTAT_BF) |
#define | i2cIsIdle() (((SSPCON2 & 0x1F) | (SSPSTAT_R_W)) ? FALSE : TRUE) |
#define | i2cPutAck() {SSPCON2_ACKDT=0; SSPCON2_ACKEN=1;} |
#define | i2cPutAckAndWait() {SSPCON2_ACKDT=0; SSPCON2_ACKEN=1;while(SSPCON2_ACKEN); /*Wait till finished*/} |
#define | i2cPutNack() {SSPCON2_ACKDT=1; SSPCON2_ACKEN=1;} |
#define | i2cPutNackAndWait() {SSPCON2_ACKDT=1; SSPCON2_ACKEN=1; while(SSPCON2_ACKEN); /*Wait till finished*/} |
#define | i2cPutStart() (SSPCON2_SEN=1) |
#define | i2cPutStartAndWait() {SSPCON2_SEN=1; while ( SSPCON2_SEN );} |
#define | i2cPutStop() (SSPCON2_PEN=1) |
#define | i2cPutStopAndWait() {SSPCON2_PEN=1; while(SSPCON2_PEN);} |
#define | i2cRestart() (SSPCON2_RSEN=1) |
#define | i2cRestartAndWait() {SSPCON2_RSEN=1; while (SSPCON2_RSEN);} |
#define | i2cWaitForIdle() while ((SSPCON2 & 0x1F) | (SSPSTAT_R_W)) |
#define | i2cWasAckReceived() (!SSPCON2_ACKSTAT) |
#define | SSPENB 0x20ul |
#define | I2C_MASTER 8ul |
#define | I2C_SLAVE_10 7ul |
#define | I2C_SLAVE_7 6ul |
#define | I2C_SLEW_OFF 0x80ul |
#define | I2C_SLEW_OFF_SMBUS 0xC0ul |
#define | I2C_SLEW_ON 0x00ul |
#define | I2C_SLEW_ON_SMBUS 0x40ul |
Functions | |
unsigned char | i2cGetByte (BOOL ack) |
void | i2cOpen (unsigned char mode, unsigned char slew, unsigned char baud) |
unsigned char | i2cPutByte (unsigned char data_out) |
unsigned char | i2cPutByteAndWait (unsigned char data_out) |
void | i2cTask (void) |
#define I2C_MASTER 8ul |
#define I2C_SLAVE_10 7ul |
#define I2C_SLAVE_7 6ul |
The following defines are used for the mode parameter in the i2cOpen() function:
#define I2C_SLEW_OFF 0x80ul |
The following defines are used for the slew parameter in the i2cOpen() function:
#define I2C_SLEW_OFF_SMBUS 0xC0ul |
#define I2C_SLEW_ON 0x00ul |
#define I2C_SLEW_ON_SMBUS 0x40ul |
#define i2cBusCollision | ( | ) | PIR2_BCLIF |
Test if a bus collision has occured. Return TRUE if it did, else FALSE
#define i2cClose | ( | ) | (SSPCON1 &= 0xDF) |
Disable I2C module
#define i2cDataRdy | ( | ) | (SSPSTAT_BF) |
Checks if the SSP buffer contains any data that can be read.
Example:
if (i2cDataRdy()) { var = i2cGetByte(); }
#define i2cIsIdle | ( | ) | (((SSPCON2 & 0x1F) | (SSPSTAT_R_W)) ? FALSE : TRUE) |
Checks if the I2C bus is IDLE, and available to be used. The i2cIsIdle() function is required since the hardware I2C peripheral does not allow for spooling of bus sequences. The I2C peripheral must be in an IDLE state before an I2C operation can be initiated or a write collision will be generated.
#define i2cPutAck | ( | ) | {SSPCON2_ACKDT=0; SSPCON2_ACKEN=1;} |
Generate bus ACK condition
#define i2cPutAckAndWait | ( | ) | {SSPCON2_ACKDT=0; SSPCON2_ACKEN=1;while(SSPCON2_ACKEN); /*Wait till finished*/} |
Generate bus ACK condition, and waits until it is finished
#define i2cPutNack | ( | ) | {SSPCON2_ACKDT=1; SSPCON2_ACKEN=1;} |
Generate bus Not ACK condition.
#define i2cPutNackAndWait | ( | ) | {SSPCON2_ACKDT=1; SSPCON2_ACKEN=1; while(SSPCON2_ACKEN); /*Wait till finished*/} |
Generate bus Not ACK condition, and waits until it is finished
#define i2cPutStart | ( | ) | (SSPCON2_SEN=1) |
Generate bus start condition
#define i2cPutStartAndWait | ( | ) | {SSPCON2_SEN=1; while ( SSPCON2_SEN );} |
Generate bus start condition, and waits until it is finished
#define i2cPutStop | ( | ) | (SSPCON2_PEN=1) |
Generate bus stop condition
#define i2cPutStopAndWait | ( | ) | {SSPCON2_PEN=1; while(SSPCON2_PEN);} |
Generate bus stop condition, and waits until it is finished
#define i2cRestart | ( | ) | (SSPCON2_RSEN=1) |
Generate bus restart condition
#define i2cRestartAndWait | ( | ) | {SSPCON2_RSEN=1; while (SSPCON2_RSEN);} |
Generate bus restart condition, and waits until it is finished
#define i2cWaitForIdle | ( | ) | while ((SSPCON2 & 0x1F) | (SSPSTAT_R_W)) |
Waits until the I2C bus is IDLE, and available to be used. The i2cWaitForIdle() function is required since the hardware I2C peripheral does not allow for spooling of bus sequences. The I2C peripheral must be in an IDLE state before an I2C operation can be initiated or a write collision will be generated.
#define i2cWasAckReceived | ( | ) | (!SSPCON2_ACKSTAT) |
Tests if an ACK was received from the slave. Returns true if it was, else false.
#define SSPENB 0x20ul |
Define SSP defines for some processors where it is not done Enable serial port and configures SCK, SDO, SDI
unsigned char i2cGetByte | ( | BOOL | ack | ) |
Read single byte from I2C bus
ack | Indicates if a ACK is sent after reading the byte. During sequencial read the master will usually send an ACK after each byte read except the last byte. Not sending the ack will indicate to the slave that this is the end of sequential read |
void i2cOpen | ( | unsigned char | mode, | |
unsigned char | slew, | |||
unsigned char | baud | |||
) |
This function resets the SSP module to the POR state and then configures the module for Master/Slave mode and the selected slew rate.
mode | One of the following values defined in i2c.h:
| |
slew | One of the following values, defined in i2c.h:
| |
baud | The baud rate of the I2C unit when configured for I2C Master mode. This is the value that will be assigned to the SSPADD register. It is calculated as follows: SSPADD = ( ((CLOCK_FREQ / BAUD) / 4) - 1 ) For example, if the CPU clock frequency is 40MHz and we need a I2C baud rate of 400KHz: SSPADD = ( ((40,000,000/400,000) / 4) - 1 ) = 24 Some common values for SSPADD are:
|
unsigned char i2cPutByte | ( | unsigned char | data_out | ) |
This routine writes a single byte to the I2C bus, and waits until the bus is idle before returning.
data_out | Single data byte for I2C bus |
unsigned char i2cPutByteAndWait | ( | unsigned char | data_out | ) |
This routine writes a single byte to the I2C bus, and waits until the bus is idle before returning.
data_out | Single data byte for I2C bus |
void i2cTask | ( | void | ) |
I2C Task Function. Must be called on a regular basis