| #define | I2C_MASTER 8 |
| #define | I2C_SLAVE_10 7 |
| #define | I2C_SLAVE_7 6 |
| #define | I2C_SLEW_OFF 0xC0 |
| #define | I2C_SLEW_ON 0x00 |
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 | i2cPutNack() {SSPCON2_ACKDT=1; SSPCON2_ACKEN=1;} |
| #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 | i2cWaitForIdle() while ((SSPCON2 & 0x1F) | (SSPSTAT_R_W)) |
| #define | i2cWasAckReceived() (!SSPCON2_ACKSTAT) |
| #define | SSPENB 0x20 |
Functions | |
| unsigned char | i2cGetByte (void) |
| void | i2cOpen (unsigned char mode, unsigned char slew, unsigned char baud) |
| unsigned char | i2cPutByte (unsigned char data_out) |
| #define I2C_MASTER 8 |
| #define I2C_SLAVE_10 7 |
| #define I2C_SLAVE_7 6 |
The following defines are used for the mode parameter in the i2cOpen() function:
| #define I2C_SLEW_OFF 0xC0 |
The following defines are used for the slew parameter in the i2cOpen() function:
| #define I2C_SLEW_ON 0x00 |
| #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 i2cPutNack | ( | ) | {SSPCON2_ACKDT=1; SSPCON2_ACKEN=1;} |
Generate bus Not ACK condition.
| #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 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 0x20 |
Enable serial port and configures SCK, SDO, SDI
| unsigned char i2cGetByte | ( | void | ) |
Read single byte from I2C bus
| 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.
| data_out | Single data byte for I2C bus |
1.4.7