#include<reg51.h>
#include<define.h>
#include<i2c.h>
void __CS_IIC_Initial( void );
void _CS_Set_sda_high( void );
void _CS_Set_sda_low ( void );
void _CS_Set_sck_high( void );
void _CS_Set_sck_low ( void );
BIT _CS_IIC_GetACK(void);
void _CS_IIC_SetACk(void);
void _CS_IIC_SetNAk(void);
void _CS_IIC_START(void);
void _CS_IIC_STOP(void);
void _CS_IIC_TxByte(BYTE);
BYTE _CS_IIC_RxByte(void);
BIT _CS_I2C_Read ( BYTE,BYTE,BYTE,BYTE *);
BIT _CS_I2C_Write( BYTE,BYTE,BYTE,BYTE *);
BIT HAL_EEPROM_Write (BYTE,BYTE);
BIT HAL_EEPROM_PWrite( BYTE , BYTE* , BYTE );
BIT HAL_EEPROM_Read ( BYTE ,BYTE *);
BIT HAL_EEPROM_PRead( BYTE , BYTE , BYTE* );
void __delay(BYTE);
//BIT judge_password(void);
//# [19]EEPROM read/write function part begin
// ------------------------------------------------------------------
void _CS_Set_sda_high()
{
__sbCSsda=1;
NOP;
NOP;
NOP;
NOP;
}
void _CS_Set_sda_low()
{
__sbCSsda=0;
NOP;
NOP;
NOP;
NOP;
}
void _CS_Set_sck_high()
{
__sbCSsck=1;
NOP;
NOP;
NOP;
NOP;
}
void _CS_Set_sck_low()
{
__sbCSsck=0;
NOP;
NOP;
NOP;
NOP;
}
// -------------------------------------------------------------------
// __CS_IIC_Initial
// initial I2C bus before use
// look for SDA high in each cycle while SCL is high
//--------------------------------------------------------------------
void __CS_IIC_Initial(void)
{
_CS_Set_sck_low();
_CS_IIC_STOP();
__delay(DELAY_10mS);
}
// -------------------------------------------------------------------
// _CS_IIC_GetACK
// 1) The acknowledge-related clock pulse is generated by the master.
// 2) The transmitter (Tx) release the SDA line (HIGH) during the
// acknowledge clock pulse.
// 3) The receiver (Rx) must pull down the SDA line during the
// acknowledge clock pulse so that it remains stable LOW during
// the HIGH period of this clock pulse.
//
// Return:
// LOW if OK
// -------------------------------------------------------------------
BIT _CS_IIC_GetACK (void)
{
BIT bResult;
_CS_Set_sda_high();
//__sbCSsda=1;
_CS_Set_sck_high();
bResult = __sbCSsda;
_CS_Set_sck_low();
return ( bResult );
} // _CS_IIC_GetACK
// -------------------------------------------------------------------
// _CS_IIC_SetACK
// __ ____
// SDA: \________/
// ____
// SCL: ____/ \____
//
// -------------------------------------------------------------------
void _CS_IIC_SetACK (void)
{
_CS_Set_sck_low();
_CS_Set_sda_low();
_CS_Set_sck_high();
_CS_Set_sck_low();
} // _SC_IIC_SetACK
// ---------------------------------------------------------------------
// _CS_IIC_SetNAK
// ____
// SCK: ___/ \____
// _____________
// SDA:
// -------------------------------------------------------------------
void _CS_IIC_SetNAK (void)
{
_CS_Set_sck_low();
_CS_Set_sda_high();
_CS_Set_sck_high();
_CS_Set_sck_low();
} // _CS_IIC_SetNAK
// -------------------------------------------------------------------
// _CS_IIC_START - START condition (SDA falling edge).
// ____
// SDA: \_____
// _____
// SCL: \__
//
// -------------------------------------------------------------------
void _CS_IIC_START (void)
{
NOP;
NOP;
NOP;
NOP;
_CS_Set_sda_high();
_CS_Set_sck_high();//prepare for start condition
_CS_Set_sda_low();
_CS_Set_sck_low();
} // _CS_IIC_START
// -------------------------------------------------------------------
// _CS_IIC_STOP - STOP condition (SDA rising edge).
// ____ __
// SDA: ____\_____/
// __ _______
// SCL: __\___/
//
// -------------------------------------------------------------------
void _CS_IIC_STOP (void)
{
NOP;
NOP;
NOP;
NOP;
_CS_Set_sck_low();
_CS_Set_sda_low();//prepare for stop condition
_CS_Set_sck_high();
_CS_Set_sda_high();
__delay(DELAY_10mS);//delay time from stop to start time must >10mS
} // CS_IIC_STOP
// -------------------------------------------------------------------
// _CS_IIC_TxByte - Parallel serial conversion.
// __ ________
// SDA: __X_state__
// ________
// SCL: _____/ \__
//
// -------------------------------------------------------------------
void _CS_IIC_TxByte (BYTE bValue)
{
BYTE i ;
for(i = 0 ; i < 8 ; i++)
{
if( bValue & 0x80)
_CS_Set_sda_high();
else
_CS_Set_sda_low();
_CS_Set_sck_high();
_CS_Set_sck_low();
bValue = bValue << 1 ;
}
} //_CS_IIC_TxByte
// -------------------------------------------------------------------
// _CS_IIC_RxByte -
// ______
// SDA: --<_read_>--
// _________
// SCL: __/ \__
//
// -------------------------------------------------------------------
BYTE _CS_IIC_RxByte (void)
{
BYTE bResult;
BYTE i;
bResult = 0x00; // clear value //
_CS_Set_sda_high();
for ( i=0;i<8;i++) // Read all bits //
{
_CS_Set_sck_high();
bResult <<=1; // Set Result to correct value //
if ( __sbCSsda ) // Sampling SDA input level //
bResult |= 0x01;
else
bResult &= 0xFE;
_CS_Set_sck_low();
}
return( bResult );
} // _IIC_RxByte
// *********************************************************************
// Function : _CS_I2C_Read
// Description :
// Arguments : DevSel : Device ID
// addr : read address
// Return : data in the address
// Side Effect :
// *********************************************************************
BIT _CS_I2C_Read ( BYTE DevSel, BYTE addr, BYTE count, BYTE *val)
{
BIT bError;
_CS_IIC_START(); // START condition
_CS_IIC_TxByte(DevSel);
bError = _CS_IIC_GetACK();
if ( bError )
return FALSE;
_CS_IIC_TxByte(addr);
bError = _CS_IIC_GetACK();
if ( bError )
return FALSE;
_CS_IIC_START(); // START condition
_CS_IIC_TxByte((BYTE)(DevSel|0x01));
bError = _CS_IIC_GetACK();
if ( bError )
return FALSE;
while(count)
{
*val=_CS_IIC_RxByte();
val++;
count--;
if(count)
_CS_IIC_SetACK();
}
_CS_IIC_SetNAK ();
_CS_IIC_STOP(); // STOP condition
return TRUE;
}
// *********************************************************************
// Function : _CS_I2C_Write
// Description :
// Arguments : DevSel : Device ID
// AS pin '0' : ID is 40h
// AS pin '1' : ID is 42h
// * pBuffer : Buffer pointer
// but normal is not more than 8)
// count : size of buffer normal is less than 8
// Return :
// Side Effect :
// *********************************************************************
BIT _CS_I2C_Write ( BYTE DevSel, BYTE addr ,BYTE count ,BYTE *pBuffer )
{
BYTE idx;
BIT bError;
_CS_IIC_START(); // START condition
_CS_IIC_TxByte ( DevSel ); // write DevEel adress
bError = _CS_IIC_GetACK();
if ( bError )
return FALSE;
_CS_IIC_TxByte ( addr ); //