[Description]
Location = "Xicor (X)\X24C01";
Info = " Module for programming 2-Wire Serial EEPROMs",
	" memory types: Xicor X24C01",
	" ---------------------------------------------------------------------",
	" Bus: I2C.",
	" Size: 128 bytes",
	" ---------------------------------------------------------------------",
	" Supported modes: Read, write, verify.",
	" ---------------------------------------------------------------------",
	" Author: ETL";
Buffer1 = "EEPROM",128;
Picture = "At24c01.bmp";
VCC = 8;
GND = 4;

[Menu]

Read	= "&Read",ReadButton,"Read chip";
Write   = "&Write",WriteButton,"Write chip";
Compare = "&Vertify",CheckButton,"Compare with buffer";
Info    = "&About module",ModuleInfoButton,"Show module info";

[Interface]

[Code]

define		END_ADR 128	
define		SDAOUT	POUT.4
define		SDAIN	PIN.4
define		SCL	POUT.5

//***********************************************************
//*       Read                                             **
//***********************************************************
Proc Read {
 
  var Error;

  print("");print("   Reading chip AT24C01...");
  Init;
  Error = 0;
  while(FDPTR != END_ADR)
  {
	Error = ReadByte(FDPTR);
	if(Error.15 = 1) { break; }
	FDATA = Error;
	FDPTR++;PBPos++;	
  }
  if(Error = 0x8000) {
    print("   Error. Chip not response. Address %d.",FDPTR);
  }
  else  {
    print("   Reading is done. %d bytes were readed.",FDPTR);
  }	
}
//***********************************************************

//***********************************************************
//*       Compare                                          **
//***********************************************************
Proc Compare
{
  var Error,NoEqu;

  print("");print("   Verifying chip AT24C01...");
  Init;
  NoEqu = 0;
  while(FDPTR != END_ADR)
  {
	Error = ReadByte(FDPTR);
	if(Error.15 = 1) { break; }
	if(Error != FDATA) { FSel = 0;NoEqu++; }
	else { FSel = 1; }
	FDPTR++;PBPos++;	
  }
  if(Error = 0x8000) {
    StopI2C;
    print("   Error. Chip not response. Address %d.",FDPTR);
  }
  else  {
    print("   Verifying is done. %d bytes were compared. Numbers of mismatch %d.",FDPTR,NoEqu);
  }	
}

Proc Write {

  var Error;

  print("");print("   Writing chip AT24C01...");
  Init;
  Error = 0;
  while(FDPTR <> END_ADR) {
    Error = WriteByteToAdr(FDPTR,FDATA);
    if(Error <> 0) { break;}
    FDPTR++;PBPos++;
  }
  if(Error <> 0) {
    print("   Error. Chip not response. Address %d",FDPTR);
  }
  else {
    print("   Writing is done. %d bytes were writed.",FDPTR);
  }
}


Proc WriteByteToAdr(Adress,Data) {

  var Error,Temp;

  StartI2C;
  Temp = Adress << 1;
  Temp.0 = 0;
  ShiftByteToI2C(Temp);
  Error = CheckAck;
  if(Error <> 0) {StopI2C; return(Error);}
  ShiftByteToI2C(Data);
  CheckAck;
  StopI2C;
  WaitReady;
  return(0);
}


Proc ShiftByteFromI2C
{
  var Counter,Byte;
  SDAOUT = 1; Byte = 0; Counter = 8;
  while(Counter <> 0) {
    Byte = Byte << 1; SCL = 1; Byte.0 = SDAIN; SCL = 0; 
    Counter--; 
  }
  return(Byte);
}

Proc StartI2C { SDAOUT = 0; SCL = 0; } 

Proc StopI2C { SCL = 1; SDAOUT = 1; } 

Proc ReadByte(Address)
{
	var Temp,Error;

	StartI2C;
	Temp = Address << 1;
	Temp.0 = 1;
	ShiftByteToI2C(Temp);
	Error = CheckAck;
	if(Error.15 = 1) { return(Error); }
	Temp = ShiftByteFromI2C;
	Error = CheckAck;
	//if(Error.15 = 1) { return(Error); }
	StopI2C;
	return(Temp);
}

Proc ShiftByteToI2C(Byte)
{
  var Counter;
  Counter = 8;
  while(Counter != 0) {
    SDAOUT = Byte.7; SCL = 1; SCL = 0; Byte = Byte << 1;
    Counter--;	
  }
  SDAOUT = 0;
}

Proc CheckAck
{
  var Error;
	
    SDAOUT = 1;SCL = 1 ;Error.15 = SDAIN; SCL = 0; SDAOUT = 0;
    return(Error);
}

Proc Init {
  var Counter,Temp;
  PBMax = END_ADR;PBPos = 0;FDPTR = 0;
  POUT = 0;
  SDAOUT = 1;
  Counter = 9;
  while(Counter != 0) {
	SCL = 1;
        if(SDAIN = 1) { break; }
        Counter--;	
  }
}


Proc WaitReady {
	
   Delay(15);

}


Proc Info {
  print("");
  print("  Module for programming 2-Wire(I2C) serial EEPROMs memory types X24C01.");
  print("");
  print("  Next functions are support:");
  print("     reading,");
  print("     writing,");
  print("     verifying.");
  print("");
  print("  Copyright (C) 2004 ETL.");
}

