/*****************************************************************************
 *
 * I2C scanning.
 *
 *****************************************************************************
 * FileName:        I2Cmain.h
 * Dependencies:    
 * Processor:       PIC18F with CAN
 * Compiler:       	C18 02.30.00 or higher
 * Linker:          MPLINK 03.20.01 or higher
 * Company:         Microchip Technology Incorporated
 *
 * Software License Agreement
 *
 * The software supplied herewith by Microchip Technology Incorporated
 * (the "Company") is intended and supplied to you, the Company's
 * customer, for use solely and exclusively with products manufactured
 * by the Company. 
 *
 * The software is owned by the Company and/or its supplier, and is 
 * protected under applicable copyright laws. All rights are reserved. 
 * Any use in violation of the foregoing restrictions may subject the 
 * user to criminal sanctions under applicable laws, as well as to 
 * civil liability for the breach of the terms and conditions of this 
 * license.
 *
 * THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, 
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, 
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 *
 * This is a simple timer function used within the DeviceNet Stack for
 * demonstration.
 * 
 *
 *
 * Author               Date        Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Ross Fosler			08/13/04	...	
 * 
 *****************************************************************************/



// Flags used by the I/O functions
typedef union __I2CFLAGS
{
	unsigned char byte;
	struct __I2CFLAGBITS
	{
		unsigned busy:1;
		unsigned tx_c:1;	// Send command only	
		unsigned tx_cd:1;	// Send command and data
		unsigned rx_d:1;	// Get data
		unsigned i2cif:1;
	}bits;
}I2CFLAGS;

typedef union __I2CSTATES
{
	unsigned char byte;
	struct __I2CSTATEBITS
	{
		unsigned start:1;
		unsigned addr:1;
		unsigned data1:1;
		unsigned data2:1;
		unsigned ack:1;
		unsigned stop:1;
	}bits;
}I2CSTATES;


extern UCHAR 		_uI2CAddr;
extern UCHAR 		_uI2CCmd;
extern UCHAR 		_uI2CData;
extern I2CFLAGS		_bI2CFlags;
extern I2CSTATES	_bI2CStates;


void I2C_Init(UCHAR bitrate);
void I2C_ProcessEvents(void);
void I2C_WriteCmd(UCHAR uI2CAddr, UCHAR uI2Cmd);
void I2C_WriteCmdAndData(UCHAR uI2CAddr, UCHAR uI2Cmd, UCHAR uI2CData);
void I2C_ReadData(UCHAR uI2CAddr);




#define	I2C_Read()			_uI2CData
#define I2C_IsReady()		(_bI2CFlags.bits.i2cif)
#define I2C_ClrReady()		_bI2CFlags.bits.i2cif = 0
#define I2C_IsBusy()		(_bI2CFlags.bits.busy)

