www.pudn.com > HT1621Control.rar > i2c_drv.c


/*C************************************************************************** 
* NAME:         I2C_drv.c 
*---------------------------------------------------------------------------- 
* Copyright (c) 2006 Atmel. 
*---------------------------------------------------------------------------- 
* RELEASE:      C51 Sample       
* REVISION:     1.00      
*---------------------------------------------------------------------------- 
* 
*****************************************************************************/ 
#include "system\reg51.h" 
#include "system\compiler.h" 
#include "system\config.h" 
#include "Driver\io_def.h" 
#include "Driver\i2c_drv.h" 
 
/*F************************************************************************** 
* NAME: I2c_ReadByte 
*---------------------------------------------------------------------------- 
* PARAMS: 
* 
* return: 
*---------------------------------------------------------------------------- 
* PURPOSE: 从24c02的地址address中读取一个字节数据 
*---------------------------------------------------------------------------- 
* EXAMPLE: 
*---------------------------------------------------------------------------- 
* NOTE: 
*---------------------------------------------------------------------------- 
* REQUIREMENTS: 
*****************************************************************************/ 
Byte I2c_ReadByte (Byte address) 
{ 
	Byte bTemp = 0x00; 
 
	I2c_start(); 
	I2c_getLSB(I2C_ID0); 
	I2c_getLSB(address); 
	I2c_start(); 
	I2c_getLSB(I2C_ID1); 
	bTemp = I2c_scanLSB(); 
	I2c_ack(); 
	I2c_stop(); 
	return(bTemp); 
} 
 
/*F************************************************************************** 
* NAME: I2c_WriteByte 
*---------------------------------------------------------------------------- 
* PARAMS: 
* 
* return: 
*---------------------------------------------------------------------------- 
* PURPOSE: 向24c02的address地址中写入一字节数据info 
*---------------------------------------------------------------------------- 
* EXAMPLE: 
*---------------------------------------------------------------------------- 
* NOTE: 
*---------------------------------------------------------------------------- 
* REQUIREMENTS: 
*****************************************************************************/ 
void I2c_WriteByte (Byte address, Byte bByte) 
{ 
	 I2c_start(); 
	 I2c_getLSB(address); 
	 I2c_getLSB(bByte); 
	 I2c_stop(); 
} 
 
/*F************************************************************************** 
* NAME: I2c_scanLSB 
*---------------------------------------------------------------------------- 
* PARAMS: 
* 
* return: 
*---------------------------------------------------------------------------- 
* PURPOSE:  
*---------------------------------------------------------------------------- 
* EXAMPLE: 
*---------------------------------------------------------------------------- 
* NOTE: 
*---------------------------------------------------------------------------- 
* REQUIREMENTS: 
*****************************************************************************/ 
Byte I2c_scanLSB (void) 
{ 
	Byte i; 
	char bByte = 0x00,temp; 
 
	for (i=0; i<8; i++){ 
		ISCL = High; 
		I2c_wait(); 
       	if (DTS_DIN){ 
			temp = 0x01; 
   			temp <<= i; 
       		bByte |= temp; 
			} 
		ISCL = Low; 
		I2c_wait(); 
		} 
	return (bByte); 
 
} 
 
/*F************************************************************************** 
* NAME: I2c_getLSB 
*---------------------------------------------------------------------------- 
* PARAMS: 
* 
* return: 
*---------------------------------------------------------------------------- 
* PURPOSE:  
*---------------------------------------------------------------------------- 
* EXAMPLE: 
*---------------------------------------------------------------------------- 
* NOTE: 
*---------------------------------------------------------------------------- 
* REQUIREMENTS: 
*****************************************************************************/ 
void I2c_getLSB (Byte bByte) 
{ 
	Byte i; 
 
	for (i=0; i<8; i++)  
	{ 
        if (bByte & 0x80) ISDA = High; 
        else ISDA = Low; 
        bByte <<= 1; 
		I2c_wait(); 
      	ISCL = High; 
		I2c_wait(); 
		ISCL = Low; 
		I2c_wait(); 
	} 
	ISDA = High; 
	I2c_wait(); 
	ISCL = High; 
	I2c_wait(); 
	I2c_wait(); 
	ISCL = Low; 
	I2c_wait(); 
} 
 
/*F************************************************************************** 
* NAME: I2c_start 
*---------------------------------------------------------------------------- 
* PARAMS: 
* 
* return: 
*---------------------------------------------------------------------------- 
* PURPOSE:  
*---------------------------------------------------------------------------- 
* EXAMPLE: 
*---------------------------------------------------------------------------- 
* NOTE: 
*---------------------------------------------------------------------------- 
* REQUIREMENTS: 
*****************************************************************************/ 
void I2c_start (void) 
{ 
	ISDA = High; 
	ISCL = High; 
	I2c_wait(); 
	ISDA = Low; 
	I2c_wait(); 
	ISCL = Low; 
} 
 
/*F************************************************************************** 
* NAME: I2c_stop 
*---------------------------------------------------------------------------- 
* PARAMS: 
* 
* return: 
*---------------------------------------------------------------------------- 
* PURPOSE:  
*---------------------------------------------------------------------------- 
* EXAMPLE: 
*---------------------------------------------------------------------------- 
* NOTE: 
*---------------------------------------------------------------------------- 
* REQUIREMENTS: 
*****************************************************************************/ 
void I2c_stop (void) 
{ 
	ISDA = Low; 
	I2c_wait(); 
	ISCL = High; 
	I2c_wait(); 
	ISDA = High; 
	I2c_wait(); 
} 
 
/*F************************************************************************** 
* NAME: I2c_ack 
*---------------------------------------------------------------------------- 
* PARAMS: 
* 
* return: 
*---------------------------------------------------------------------------- 
* PURPOSE:  
*---------------------------------------------------------------------------- 
* EXAMPLE: 
*---------------------------------------------------------------------------- 
* NOTE: 
*---------------------------------------------------------------------------- 
* REQUIREMENTS: 
*****************************************************************************/ 
void I2c_ack (void) 
{ 
	ISDA = Low; 
	I2c_wait(); 
	ISDA = High; 
	I2c_wait(); 
} 
 
/*F************************************************************************** 
* NAME: I2c_init 
*---------------------------------------------------------------------------- 
* PARAMS: 
* 
* return: 
*---------------------------------------------------------------------------- 
* PURPOSE:  
*---------------------------------------------------------------------------- 
* EXAMPLE: 
*---------------------------------------------------------------------------- 
* NOTE: 
*---------------------------------------------------------------------------- 
* REQUIREMENTS: 
*****************************************************************************/ 
void I2c_init (void) 
{ 
	ISDA = High; 
	I2c_wait(); 
	ISCL = High; 
	I2c_wait(); 
} 
 
/*F************************************************************************** 
* NAME: I2c_wait 
*---------------------------------------------------------------------------- 
* PARAMS: 
* 
* return: 
*---------------------------------------------------------------------------- 
* PURPOSE:  
*---------------------------------------------------------------------------- 
* EXAMPLE: 
*---------------------------------------------------------------------------- 
* NOTE: 
*---------------------------------------------------------------------------- 
* REQUIREMENTS: 
*****************************************************************************/ 
void I2c_wait (void) 
{ 
	char i; 
	for(i=0; i<2; i++){ 
		i = i; 
		} 
}