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

📄 i2c.c

📁 标准 C语言 I2C程序
💻 C
📖 第 1 页 / 共 2 页
字号:

        while(ucBufLen--) // loop to burst read
        {
            *pBuf = i2c_ReceiveByte(ucBufLen); // receive byte

            pBuf++; // next byte pointer
        } // while

        break;
    } // while

    i2c_Stop();
}
//*******************************************
// * 函数名称:Initial516AD														
// * 内容简介:MCU内部初始化
// * 函数变量:无
// *				  :
// *	返回值	  :    无
//*******************************************
void i2c_WriteTBL(Byte deviceID, Word addr, Byte *buffer, Byte count)
{ 
  i2cBurstWriteBytes( deviceID, addr,buffer, count);
}

/////////////////////////////////////////////////////////////////
// I2C read current bytes from device.
//
// Arguments: ucSlaveAdr - slave address
//            pBuf - pointer of buffer
//            ucBufLen - length of buffer
/////////////////////////////////////////////////////////////////
//*******************************************
// * 函数名称:Initial516AD														
// * 内容简介:MCU内部初始化
// * 函数变量:无
// *				  :
// *	返回值	  :    无
//*******************************************
void i2cBurstCurrent_Write(Byte ucSlaveAdr, Byte *pBuf, Byte ucBufLen)
{
    Byte ucDummy; // loop dummy
    // if(ucSlaveAdr== MST726A)
	 	//return;
    ucDummy = I2C_ACCESS_DUMMY_TIME;
    while(ucDummy--)
    {
        if (i2c_AccessStart(ucSlaveAdr, I2C_WRITE) == FALSE)    	
            continue;

        while(ucBufLen--) // loop to burst read
        {
          i2c_SendByte(*pBuf); // send byte
		
            pBuf++; // next byte pointer
        }// while 
       break;
    } // while
		
            i2c_Stop();    
}
/////////////////////////////////////////////////////////////////
// I2C write current bytes from device.
//
// Arguments: ucSlaveAdr - slave address
//            pBuf - pointer of buffer
//            ucBufLen - length of buffer
/////////////////////////////////////////////////////////////////
//*******************************************
// * 函数名称:Initial516AD														
// * 内容简介:MCU内部初始化
// * 函数变量:无
// *				  :
// *	返回值	  :    无
//*******************************************
void i2cBurstCurrent_Read(Byte ucSlaveAdr, Byte *pBuf, Byte ucBufLen)
{
    Byte ucDummy; // loop dummy
      
    ucDummy = I2C_ACCESS_DUMMY_TIME;
    while(ucDummy--)
    {
        if (i2c_AccessStart(ucSlaveAdr, I2C_READ) == FALSE)
            continue;

        while(ucBufLen--) // loop to burst read
        {
            *pBuf = i2c_ReceiveByte(ucBufLen); // receive byte

            pBuf++; // next byte pointer
        } // while

        break;
    } // while

    i2c_Stop();
}
//*******************************************
// * 函数名称:Initial516AD														
// * 内容简介:MCU内部初始化
// * 函数变量:无
// *				  :
// *	返回值	  :    无
//*******************************************
void i2cWriteByte(Byte deviceID, Byte addr, Byte value)
{ 
  i2c_WriteTBL(deviceID, addr, &value, 1);
}



//*******************************************
// * 函数名称:Initial516AD														
// * 内容简介:MCU内部初始化
// * 函数变量:无
// *				  :
// *	返回值	  :    无
//*******************************************
/////////////////////////////////////////
// Set I2C SCL pin high/low.
//
// Arguments: bSet - high/low bit
/////////////////////////////////////////
void i2cSetSCL_Chk_Tsc2003(bit bSet)
{
    uchar ucDummy; // loop dummy

	    i2cSetSCL_Tsc2003(bSet); // set SCL pin

	    if (bSet == _HIGH) // if set pin high
	    {
	        ucDummy = I2C_CHECK_PIN_DUMMY; // initialize dummy
	        while ((i2cSCL_PIN_Status_Tsc2003() == _LOW) && (ucDummy--)) ; // check SCL pull high
	    }
   
}

/////////////////////////////////////////
// Set I2C SDA pin high/low
//
// Arguments: bSet - high/low bit
/////////////////////////////////////////
//*******************************************
// * 函数名称:Initial516AD														
// * 内容简介:MCU内部初始化
// * 函数变量:无
// *				  :
// *	返回值	  :    无
//*******************************************
void i2cSetSDA_Chk_Tsc2003(bit bSet)
{
    uchar ucDummy; // loop dummy

	    i2cSetSDA_Tsc2003(bSet); // set SDA pin

	    if (bSet == _HIGH) // if set pin high
	    {
	        ucDummy = I2C_CHECK_PIN_DUMMY; // initialize dummy
	        while ((i2cSDA_PIN_STATUS_Tsc2003() == _LOW) && (ucDummy--)) ; // check SDA pull high
	    }

}
//////////////////////////////////////////////////////////////
// Delay 4us
//////////////////////////////////////////////////////////////
//*******************************************
// * 函数名称:Initial516AD														
// * 内容简介:MCU内部初始化
// * 函数变量:无
// *				  :
// *	返回值	  :    无
//*******************************************
void i2c_Delay_Tsc2003(void)
{
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
}
//////////////////////////////////////////////////////
// I2C start signal.
// <comment>
//  SCL ________
//              \_________
//  SDA _____
//           \____________
//
// Return value: None
//////////////////////////////////////////////////////
//*******************************************
// * 函数名称:Initial516AD														
// * 内容简介:MCU内部初始化
// * 函数变量:无
// *				  :
// *	返回值	  :    无
//*******************************************
bit i2c_Start_Tsc2003()
{
    bit bStatus = TRUE; // success status


    i2cSetSDA_Chk_Tsc2003(_HIGH);
    i2c_Delay_Tsc2003();
    i2cSetSCL_Chk_Tsc2003(_HIGH);
    i2c_Delay_Tsc2003();
		
		    if ((i2cSCL_PIN_Status_Tsc2003() == _LOW) || (i2cSDA_PIN_STATUS_Tsc2003() == _LOW))
		    {
		        bStatus = FALSE;
		    }
		    else
		    {
		        i2cSetSDA_Tsc2003(_LOW);
		        i2c_Delay_Tsc2003();
		        i2cSetSCL_Tsc2003(_LOW);
		    }
  
	
    return bStatus;
}
/////////////////////////////////////////
// I2C stop signal.
// <comment>
//              ____________
//  SCL _______/
//                 _________
//  SDA __________/
/////////////////////////////////////////
//*******************************************
// * 函数名称:Initial516AD														
// * 内容简介:MCU内部初始化
// * 函数变量:无
// *				  :
// *	返回值	  :    无
//*******************************************
void i2c_Stop_Tsc2003()
{
	    i2cSetSCL_Tsc2003(_LOW);
	    i2c_Delay_Tsc2003();
	    i2cSetSDA_Tsc2003(_LOW);
	    i2c_Delay_Tsc2003();
	    i2cSetSCL_Tsc2003(_HIGH);
	    i2c_Delay_Tsc2003();
	    i2cSetSDA_Tsc2003(_HIGH);
	    i2c_Delay_Tsc2003();
}

//////////////////////////////////////////////////////////////////////////
// I2C send byte to device.
//
// Arguments: ucVal - send byte
// Return value: I2C acknowledge bit
//               I2C_ACKNOWLEDGE/I2C_NON_ACKNOWLEDGE
//////////////////////////////////////////////////////////////////////////
//*******************************************
// * 函数名称:Initial516AD														
// * 内容简介:MCU内部初始化
// * 函数变量:无
// *				  :
// *	返回值	  :    无
//*******************************************
bit i2c_SendByte_Tsc2003(uchar ucVal)
{
    uchar    ucMask = 0x80;	   
    bit     bAck; // acknowledge bit

	    while(ucMask)
    	{
	        if (ucVal & ucMask)
	            i2cSetSDA_Chk_Tsc2003(_HIGH);
	        else
	            i2cSetSDA_Chk_Tsc2003(_LOW);
	        i2c_Delay_Tsc2003();
	        i2cSetSCL_Chk_Tsc2003(_HIGH); // clock
	        i2c_Delay_Tsc2003();
	        i2c_Delay_Tsc2003();
	        i2cSetSCL_Tsc2003(_LOW);
	        i2c_Delay_Tsc2003();

	        ucMask >>= 1; // next
	    } // while

	    // recieve acknowledge
	    i2cSetSDA_Tsc2003(_HIGH);
	    i2c_Delay_Tsc2003();
	    i2cSetSCL_Tsc2003(_HIGH);
	    i2c_Delay_Tsc2003();	    
		
	    bAck = i2cSDA_PIN_STATUS_Tsc2003(); // recieve acknowlege
	    i2cSetSCL_Tsc2003(_LOW);

	
    return (bAck);		
}
//////////////////////////////////////////////////////////////////////////
// I2C receive byte from device.
//
// Return value: receive byte
//////////////////////////////////////////////////////////////////////////
//*******************************************
// * 函数名称:Initial516AD														
// * 内容简介:MCU内部初始化
// * 函数变量:无
// *				  :
// *	返回值	  :    无
//*******************************************
uchar ii2c_ReceiveByte_Tsc2003(bit bAck)
{
    uchar    ucReceive = 0;
    uchar    ucMask = 0x80;



		    while(ucMask)
		    {
				i2cSetSDA_Tsc2003(_HIGH);
				i2cSetSCL_Tsc2003(_HIGH);
				i2c_Delay_Tsc2003();
				
				if(i2cSDA_PIN_STATUS_Tsc2003() == _HIGH)
					ucReceive |= ucMask;

				i2cSetSCL_Tsc2003(_LOW);
				i2c_Delay_Tsc2003();

		        ucMask >>= 1;
		    } // while

		    if (bAck) // acknowledge
		        i2cSetSDA_Chk_Tsc2003(I2C_ACKNOWLEDGE);
		    else // non-acknowledge
		        i2cSetSDA_Chk_Tsc2003(I2C_NON_ACKNOWLEDGE);

		    i2c_Delay_Tsc2003();
		    i2cSetSCL_Tsc2003(_HIGH);
		    i2c_Delay_Tsc2003();
		    i2cSetSCL_Tsc2003(_LOW);


    return ucReceive;	  
}

//////////////////////////////////////////////////////////////////////////
// I2C access start.
//
// Arguments: ucSlaveAdr - slave address
//            trans_t - I2C_TRANS_WRITE/I2C_TRANS_READ
//////////////////////////////////////////////////////////////////////////
//*******************************************
// * 函数名称:Initial516AD														
// * 内容简介:MCU内部初始化
// * 函数变量:无
// *				  :
// *	返回值	  :    无
//*******************************************
bit i2c_AccessStart_Tsc2003(uchar ucSlaveAdr, I2C_Direction trans_t)
{
    uchar ucDummy; // loop dummy

    if (trans_t == I2C_READ) // check i2c read or write
        ucSlaveAdr = I2C_DEVICE_ADR_READ(ucSlaveAdr); // read
    else
        ucSlaveAdr = I2C_DEVICE_ADR_WRITE(ucSlaveAdr); // write

    ucDummy = I2C_ACCESS_DUMMY_TIME;
    while (ucDummy--)
    {    
	i2c_Delay_Tsc2003();
        if (i2c_Start_Tsc2003() == FALSE)
            continue;

        if (i2c_SendByte_Tsc2003(ucSlaveAdr) == I2C_ACKNOWLEDGE) // check acknowledge
            return TRUE;
		//printf("ucSlaveAdr====%x", ucSlaveAdr);
        i2c_Stop_Tsc2003();
        Delay_1ms(1);
    }

    return FALSE;		
}


/////////////////////////////////////////////////////////////////
// I2C read current bytes from device.
//
// Arguments: ucSlaveAdr - slave address
//            pBuf - pointer of buffer
//            ucBufLen - length of buffer
/////////////////////////////////////////////////////////////////
//*******************************************
// * 函数名称:Initial516AD														
// * 内容简介:MCU内部初始化
// * 函数变量:无
// *				  :
// *	返回值	  :    无
//*******************************************
void i2cBurstCurrent_Write_Tsc2003(uchar ucSlaveAdr, uchar *pBuf, uchar ucBufLen)
{
    uchar ucDummy; // loop dummy
      
    ucDummy = I2C_ACCESS_DUMMY_TIME;
    while(ucDummy--)
    {
        if (i2c_AccessStart_Tsc2003(ucSlaveAdr, I2C_WRITE) == FALSE)
            continue;

        while(ucBufLen--) // loop to burst read
        {
            i2c_SendByte_Tsc2003(*pBuf); // send byte

            pBuf++; // next byte pointer
        } // while
       break;
    } // while
            i2c_Stop_Tsc2003();    
}
/////////////////////////////////////////////////////////////////
// I2C write current bytes from device.
//
// Arguments: ucSlaveAdr - slave address
//            pBuf - pointer of buffer
//            ucBufLen - length of buffer
/////////////////////////////////////////////////////////////////
//*******************************************
// * 函数名称:Initial516AD														
// * 内容简介:MCU内部初始化
// * 函数变量:无
// *				  :
// *	返回值	  :    无
//*******************************************
void i2cBurstCurrent_Read_Tsc2003(uchar ucSlaveAdr, uchar *pBuf, uchar ucBufLen)
{
    uchar ucDummy; // loop dummy
      
    ucDummy = I2C_ACCESS_DUMMY_TIME;
    while(ucDummy--)
    {
        if (i2c_AccessStart_Tsc2003(ucSlaveAdr, I2C_READ) == FALSE)
            continue;

        while(ucBufLen--) // loop to burst read
        {
            *pBuf = ii2c_ReceiveByte_Tsc2003(ucBufLen); // receive byte

            pBuf++; // next byte pointer
        } // while

        break;
    } // while

    i2c_Stop_Tsc2003();
}

  

⌨️ 快捷键说明

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