⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2c.c

📁 51汇编程序实例
💻 C
字号:
#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 );                //  write addr
    bError = _CS_IIC_GetACK();              
    if ( bError )
        return FALSE;
        
    for ( idx = 0; idx < count; idx++ )
    {
        _CS_IIC_TxByte(*pBuffer++);         //  write Data
        bError = _CS_IIC_GetACK();         
        if (bError )
        	return FALSE;
    }

    _CS_IIC_STOP();                     // STOP condition
    return TRUE ;
}

//  *********************************************************************
//  Function    :   HAL_EEPROM_Write
//  Description :   Issue one EEPROM write action -- write one BYTE once time
//  Arguments   :   bAddr, the desired address for EEPROM
//                  bValue, the desired value for EEPROM write
//  Return      :   none
//  Side Effect :
//  *********************************************************************
BIT HAL_EEPROM_Write(BYTE bAddr, BYTE bValue)
{
    BYTE   TempR[1];

   TempR[0]= bValue;
    if(_CS_I2C_Write ( I2C_ID_EEPROM, bAddr , 1 ,(BYTE *) TempR ) )
		return TRUE;
	else
		return FALSE;
}


// More 8 byte every time    have 8 byte/page limite
//  *********************************************************************
//  Function    :   HAL_EEPROM_PWrite
//  Description :   Issue a sequence of EEPROM write action
//  Arguments   :   bAddr, the desired address for EEPROM
//                  pbValue, a point to BYTE array, which stores
//                           the desird values
//                  bCount, the number of the desired values, the max is 8
//  Return      :   Bool,   if not successfully execute must do it again 
//					or tell "it is error"
//  Side Effect :   
//  *********************************************************************
BIT    HAL_EEPROM_PWrite(BYTE bAddr, BYTE* pbValue, BYTE bCount)
{
    BYTE   i,j,k;
    
    j = bCount/( (BYTE)PAGE_NUMBER );
    k = bCount%( (BYTE)PAGE_NUMBER );

    for(i=0;i<j;i++)
    {
    	if(!(_CS_I2C_Write(I2C_ID_EEPROM,bAddr+(i*8),8,pbValue+(i*8))))
    	{
    		i--;
    		continue;
    	}
    }
    if(k)//REMAIN data <> 0
    {
        while(!(_CS_I2C_Write(I2C_ID_EEPROM,bAddr+(i*8),k,pbValue+i*8)));
    }    
	return TRUE;
}

//  *********************************************************************
//  Function    :   HAL_EEPROM_Read
//  Description :   Issue one EEPROM read action
//  Arguments   :   bAddr, the desired address for EEPROM
//  Return      :   the value for specified EEPROM's address
//  Side Effect :
//  *********************************************************************
BIT HAL_EEPROM_Read(BYTE bAddr,BYTE *pbVal )
{
    BYTE temp[1];
   
    if(_CS_I2C_Read (I2C_ID_EEPROM, bAddr, 1,(BYTE *) temp))
    {   
    	*pbVal = temp[0];
    	return TRUE ;
    }	
    else
        return FALSE;
}

//  do not have 8 byte/page limit
//  *********************************************************************
//  Function    :   HAL_EEPROM_PRead
//  Description :   Issue a sequence of EEPROM read action
//  Arguments   :   bAddr, the desired address for EEPROM
//                  bCount, the number for the desired values
//                  pbValue, a point to BYTE array, which will store
//                           the return value
//  Return      :
//  Side Effect :
//  *********************************************************************
BIT    HAL_EEPROM_PRead(BYTE bAddr, BYTE bCount, BYTE* pbValue )
{
   BYTE   i,j,k;
    j = bCount/( (BYTE)PAGE_NUMBER );
    k = bCount%( (BYTE)PAGE_NUMBER );

    for(i=0;i<j;i++)
    {
    	if(!(_CS_I2C_Read(I2C_ID_EEPROM,bAddr+(i*8),8,pbValue+(i*8))))
    	{
    		i--;
    		continue;
    	}
    }
    if(k)//REMAIN data <> 0
    {
        while(!(_CS_I2C_Read(I2C_ID_EEPROM,bAddr+(i*8),k,pbValue+i*8)));
    }    
	return TRUE;
}

//  *********************************************************************
void __delay(BYTE  i)
{
	BYTE j;
	while(i)
	{
		for(j=0;j<200;j++)
			NOP;
	i--;			
	}
}
//BIT 	  judge_password()
//{
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -