net/i2c.h File Reference


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 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 Documentation

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

  • 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   0xC0
 

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

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

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


Function Documentation

unsigned char i2cGetByte void   ) 
 

Read single byte from I2C bus

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.

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


Generated on Wed Feb 22 12:39:25 2006 for SBC68EC Web Server by  doxygen 1.4.5