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

📄 i2c.c

📁 s1d13716的源码 windiws ce 或8位单片机
💻 C
📖 第 1 页 / 共 2 页
字号:
		{
			ret = PrvStartI2C(gSlaveDevice);
			if (ret)
			{
				ret = PrvWriteI2CByte(SubAddress++, FALSE);
				if (ret)
					ret = PrvWriteI2CByte(*pBytes++, i == (nBytes - 1));
			}
			else if (gLastError == I2C_ERR_SLAVE_NACK_DATA)
				gLastError = I2C_ERR_SLAVE_NACK_ID;

			PrvStopI2C();						// Must always be able to STOP the I2C bus!
		}
	}
	else										// Auto Increment set
	{
		ret = PrvStartI2C(gSlaveDevice);
		if (ret)
		{
			for (i = 0; i < nBytes; i++)
			{
				ret = PrvWriteI2CByte(*pBytes, i == (nBytes - 1));
				if (FALSE == ret)
					break;
				pBytes++;
			}
		}
		else if (gLastError == I2C_ERR_SLAVE_NACK_DATA)
			gLastError = I2C_ERR_SLAVE_NACK_ID;

		PrvStopI2C();					// Must always be able to STOP the I2C bus!
	}

	return ret;
}


//---------------------------------------------------------------------------
//  FUNCTION:	i2cWrite2Bytes()
//
//  DESCRIPTION:
//		Write 2 bytes of data to the I2C bus.
//
//  PARAMETERS:
//		Byte1	- first 8-bit value to write to the I2C bus
//		Byte2	- second 8-bit value to write tot the I2C bus
//
//  RETURNS: 
//		TRUE	- if the function is successful in writing the bytes
//				  to the I2C bus.
//		FALSE	- if there is an error during the transfer.
//				- Call 12cGetLastError() or 12cGetLastErrorText() for
//				  more information on the error.
//---------------------------------------------------------------------------
Boolean i2cWrite2Bytes( UInt8 Byte1, UInt8 Byte2 )
{
	UInt8 aBytes[2];

	aBytes[0] = Byte1;
	aBytes[1] = Byte2;

	return i2cWriteBytes(aBytes, 2);
}


//---------------------------------------------------------------------------
//  FUNCTION:	i2cReadBytes()
//
//  DESCRIPTION:
//		Reads a string of bytes from the I2C bus and places them in a buffer.
//
//  PARAMETERS:
//		pSubAddress	-
//		pBytes		- 
//		nBytes		- Maximum number of bytes to read from the device
//
//  RETURNS: 
//		TRUE	- if the function is successful in read the bytes stream
//				  from the I2C bus.
//		FALSE	- if there is an error during the transfer.
//				- Call 12cGetLastError() or 12cGetLastErrorText() for
//				  more information on the error.
//---------------------------------------------------------------------------
Boolean i2cReadBytes( const UInt8 *pSubAddress, UInt8 *pBytes, int nBytes )
{
	int i;
	Boolean ret = TRUE;

	gLastError = I2C_ERR_NONE;

	if (pSubAddress)
	{
		ret = PrvStartI2C(gSlaveDevice);
		if (ret)
		{
			ret = PrvWriteI2CByte(*pSubAddress, FALSE);
			if (ret && !gfRepeatedStart)
				PrvStopI2C();								// Repeated Start condition not supported
		}
		else if (gLastError == I2C_ERR_SLAVE_NACK_DATA)
			gLastError = I2C_ERR_SLAVE_NACK_ID;
	}

	if (ret)
	{
		ret =  PrvStartI2C((UInt8)(gSlaveDevice | 0x01));	// This may be a repeated-START condition.
		if (ret)
		{
			for (i = 1; i <= nBytes; i++)
			{
				ret = PrvReadI2CByte(pBytes, (i != nBytes));
				if (!ret)
					break;
				pBytes++;
			}
		}
		else if (gLastError == I2C_ERR_SLAVE_NACK_DATA)
			gLastError = I2C_ERR_SLAVE_NACK_ID;

	}
	PrvStopI2C();											// Must always be able to STOP the I2C bus!

	return ret;
}

//===========================================================================

//---------------------------------------------------------------------------
//  FUNCTION:	PrvStartI2C()
//
//  DESCRIPTION:
//		This routine sends an I2C Start sequence with data.
//
//  RETURNS: 
//		TRUE
//
//	NOTES:
//		So far, in every case I've seen, the data transmitted with START has
//		always been the slave ID.
//		- Is this always the case?
//---------------------------------------------------------------------------
static Boolean PrvStartI2C( UInt8 Data )
{
	halWriteReg8(REG0088_I2CWRITE,     Data);	// Set the data value to write.
	halWriteReg8(REG0084_I2CSTARTSTOP, 0x03);	// Set for Start with Data
	halWriteReg8(REG0082_I2CCOMMAND,   0x01);	// Go

	halWriteReg8(REG0084_I2CSTARTSTOP, 0x00);	// Set the REG[0084] value for data writes

	// Check for ACK/NAK (but first wait for the TIP bit to clear)
	while (halReadReg8(REG0080_I2CSTATUS) & 0x08)
		NULL;
	if (0x02 == (halReadReg8(REG0080_I2CSTATUS) & 0x02))
	{
		gLastError = I2C_ERR_SLAVE_NACK_DATA;
		return FALSE;
	}

	return TRUE;
}


//---------------------------------------------------------------------------
//  FUNCTION:	PrvStopI2C()
//
//  DESCRIPTION:
//		This routine sends an I2C Stop command to the I2C bus.
//
//		Issuing a STOP when the I2C bus is already stopped will generate
//		an error (REG[0080] bit 6). To prevent this condition from
//		occuring this routine looks at I2C Busy REG[0080] bit 2. If the
//		I2C is not busy then the STOP is not set.
//
//  RETURNS: 
//		TRUE
//---------------------------------------------------------------------------
static Boolean PrvStopI2C( void )
{
	if (0x04 == (halReadReg8(REG0080_I2CSTATUS) & 0x04))
	{
		// Set STOP without data and execute the command
		halWriteReg8(REG0084_I2CSTARTSTOP, 0x05);
		halWriteReg8(REG0082_I2CCOMMAND,   0x01);
	}

	return TRUE;
}


//---------------------------------------------------------------------------
//  FUNCTION:	PrvWriteI2CByte()
//
//  DESCRIPTION:
//		This routine sends one byte over the I2C bus.
//
//  PARAMETERS:
//		Data	- the eight bit value to send over I2C
//
//  RETURNS: 
//		TRUE	- if the receiving device returns an ACK in response to
//				  receiving the data.
//		FALSE	- if the receiving decice returns a NAK.
//---------------------------------------------------------------------------
static Boolean PrvWriteI2CByte( UInt8 Data, Boolean LastByte )
{
	Boolean RetVal = TRUE;

	// Write the data to the data register and start the transfer.
	halWriteReg8(REG0088_I2CWRITE, Data);		// Write the data to the data register
	if (LastByte)
		halWriteReg8(REG0084_I2CSTARTSTOP, 0x01);
	else
		halWriteReg8(REG0084_I2CSTARTSTOP, 0x00);
	halWriteReg8(REG0082_I2CCOMMAND, 0x01);		// Go

	// Get ACK/NAK
	// Wait for I2C TIP bit to go low then read the I2CRxAck bit
	while (halReadReg8(REG0080_I2CSTATUS) & 0x08)
		NULL;
	if (0x02 == (halReadReg8(REG0080_I2CSTATUS) & 0x02))
	{
		gLastError = I2C_ERR_SLAVE_NACK_DATA;
		RetVal = FALSE;
	}

	return RetVal;
}


//---------------------------------------------------------------------------
//  FUNCTION:	PrvReadI2CByte()
//
//  DESCRIPTION:
//		Reads one byte from the I2C bus.
//
//  PARAMETERS:
//
//  RETURNS: 
//---------------------------------------------------------------------------
static Boolean PrvReadI2CByte( UInt8 *pData, Boolean ACK )
{
	if (ACK)
		halWriteReg8(REG0082_I2CCOMMAND, 0x03);
	else
	{
		halWriteReg8(REG0084_I2CSTARTSTOP, 0x01);
		halWriteReg8(REG0082_I2CCOMMAND, 0x07);
	}

	// Wait for I2C TIP bit to go low then read the data register.
	while (halReadReg8(REG0080_I2CSTATUS) & 0x08)
		NULL;
	*pData = halReadReg8(REG0086_I2CREAD);

	// ****
	// RELE - We should be able to read some sort of transfer status here.
	//        Find out from Shon what the bits to read are.
	// ****
	
	return TRUE;
}

⌨️ 快捷键说明

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