net/i2c.h File Reference

I2C Functions. More...


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)


Detailed Description

I2C Functions.

Author:
Modtronix Engineering
Compiler:
MPLAB C18 v2.10 or higher
HITECH PICC-18 V8.35PL3 or higher

Description

This module contains code for reading and writing devices on the I2C bus.

Define Documentation

#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:

  • I2C_SLAVE_7 I2C Slave mode, 7-bit address
  • I2C_SLAVE_10 I2C Slave mode, 10-bit address
  • I2C_MASTER I2C Master mode

#define I2C_SLEW_OFF   0x80ul

The following defines are used for the slew parameter in the i2cOpen() function:

  • I2C_SLEW_OFF Slew rate disabled for 100kHz mode
  • I2C_SLEW_ON Slew rate enabled for 400kHz mode
  • I2C_SLEW_OFF_SMBUS Slew rate disabled for 100kHz mode, SMBus inputs enabled
  • I2C_SLEW_ON_SMBUS Slew rate enabled for 400kHz mode, SMBus inputs enabled

#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();
  }

Returns:
1 if there is data in the SSP buffer 0 if there is no data in the SSP buffer

 
#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.

Returns:
TRUE if I2C bus is idle and can be used
FALSE if I2C bus is NOT idle. User has to wait until bus is idle before using it.

 
#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


Function Documentation

unsigned char i2cGetByte ( BOOL  ack  ) 

Read single byte from I2C bus

Parameters:
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
Returns:
Contents of SSPBUF register

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.

Parameters:
mode One of the following values defined in i2c.h:
  • I2C_SLAVE_7 I2C Slave mode, 7-bit address
  • I2C_SLAVE_10 I2C Slave mode, 10-bit address
  • I2C_MASTER I2C Master mode
slew One of the following values, defined in i2c.h:
  • I2C_SLEW_OFF Slew rate disabled for 100 kHz mode
  • I2C_SLEW_ON Slew rate enabled for 400 kHz mode
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:
  • 400 KHz at 40 MHz system clock = 24
  • 400 KHz at 20 MHz system clock = 12
  • 400 KHz at 10 MHz system clock = 5

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.

Parameters:
data_out Single data byte for I2C bus
Returns:
Status byte for WCOL detection.
Returns 0 if OK, else 1 if error

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.

Parameters:
data_out Single data byte for I2C bus
Returns:
Status byte for WCOL detection.
Returns 0 if OK, else 1 if error

void i2cTask ( void   ) 

I2C Task Function. Must be called on a regular basis


Generated on Wed Feb 3 12:45:35 2010 for SBC65EC Web Server by  doxygen 1.5.8