www.pudn.com > LPT_I2C_ADuC.rar > I2C_PROTOCOL.c


/**************************************************************************** 
 * 
 * File:                I2C_PROTOCOL.c 
 * 
 * Author:              Fiberxon(Jack Zhou) 
 * 
 * Description:         Base function of the I2C-LPT accessing with EVB 
 * 
 * Time:                2006-Feb-26 
 * 
 * version:				v1.2  ÖØ´ó¸Ä°æ 
 ****************************************************************************/ 
 
 
#include  
#include "I2C_LPT.h" 
#include "I2C_PROTOCOL.h" 
 
 
/*************************************************************************/ 
void I2C_START(void) 
{ 
  SetSCL(1); // scl=1; 
  SetSDA(1); // SDA=1; 
  SetSDA(0); // SDA=0, SCL holds 1; "START" 
  SetSCL(0);//jack 20050803 
  return; 
} 
 
void I2C_WRITE_DEVICE_ADDR(BYTE device_addr, BYTE write_read) 
{ //write_read=0 means for write, 
  //	 	  =1 means for read 
  BYTE byte_temp[8];  
  int ii; 
 
   
  SetSCL(0);//jack 20050803  
  for (ii=1; ii<8; ii++)  
  { byte_temp[ii]= (device_addr >> ii) & 0x01;  } 
  byte_temp[0]=write_read; // write_read flag 
  for (ii=7; ii>=0; ii--) 
  { 
	SetSDA(byte_temp[ii]);  
	SetSCL(1);  
	SetSCL(0); 
  }  
  return; 
} 
 
BOOL I2C_GET_ACK(void) 
{int ii;  
  SetSCL(0);//jack 20050803  
  SetSDA(1); // sda=1; LPT release sda bus; waiting for reading acknowledge signal from EEPROM 
  SetSCL(1); // scl=1; 
  ii=0; 
  while ( GetSDA() && (ii<10) ) {ii++;}// if resceive ack!=0 then countinue to wait 
   
  SetSCL(0); // scl=1,0 
  SetSDA(1); // jack 20050803 
   
  if  (ii>=9) 
  { SetSCL(1); // release SCL 
  } 
  return (ii>=9); 
     
} 
 
void I2C_WRITE_WORD_ADDR(BYTE rom_addr) 
{int ii; 
  BYTE byte_temp[8]; 
   
  SetSCL(0);//jack 20050803  
  for (ii=7; ii>=0; ii--) 
	  byte_temp[ii] = (BYTE)((rom_addr & (0x01 << ii)) >> ii); 
  for (ii=7; ii>=0; ii--) 
  {	SetSDA(byte_temp[ii]); SetSCL(1); SetSCL(0);} /* sda=byte_temp[ii]; scl=1,0 */  
  return ; 
} 
 
void I2C_WRITE_WORD_VALUE(BYTE rom_value) 
{int ii;  
  BYTE byte_temp[8]; 
 
  SetSCL(0);//jack 20050803  
  for (ii=7; ii>=0; ii--) 
	  byte_temp[ii] = (BYTE)((rom_value & (0x01 << ii)) >> ii); 
  for (ii=7; ii>=0; ii--) 
  {	SetSDA(byte_temp[ii]); SetSCL(1); SetSCL(0); /* sda=byte_temp[ii]; scl=1,0 */ } 
  return; 
} 
 
BYTE I2C_READ_WORD_VALUE(void) 
{ BYTE byte_temp=0; int i; 
  SetSCL(0);//jack 20050803  
  for (i=7; i>=0; i--) 
  {	  SetSCL(1); byte_temp = byte_temp | (GetSDA()<1000) return -1; 
	 } 
*/	//must delay for EEPROM internal write. 
  } 
 
  return error; 
} 
 
 
int I2C_BYTEs_WRITE (BYTE device_addr, BYTE rom_StartAddress, int rom_Length, BYTE *rom_value_arr, float T_wait) 
{BYTE rom_value; 
 int rom_addr, error; // if define rom_addr as BYTE, the   for (rom_addr=0; rom_addr<=0xff; rom_addr++)  will be a dead-circle 
 int error_count; 
 int i; 
 
 
   
  // START 
  I2C_START(); 
  
  //device address , for "Write" 
  I2C_WRITE_DEVICE_ADDR(device_addr,0); 
  
  //get acknowledge 
  if (I2C_GET_ACK()) //error with no ack signal from target 
  { return -1; } 
  
  //Word Address=rom_addr high order bit first 
  I2C_WRITE_WORD_ADDR(rom_StartAddress); 
  
  //get acknowledge 
  if (I2C_GET_ACK()) //error with no ack signal from target 
  { return -2; } 
  
   
  for (i=0; i