标记下模拟IIC驱动代码
#if 1
///////////////////////////////////////////////////////////////////////////////////
//////I2C sound
/////////////////////////////////////////
// Set I2C SCL pin high/low.
//
// Arguments: bSet - high/low bit
/////////////////////////////////////////
void i2cSetSCL_tw8823(BYTE bSet)
{
if(bSet) GPIO_SetBits(P_TW8823_SCL_PORT, P_TW8823_SCL);
else GPIO_ResetBits(P_TW8823_SCL_PORT, P_TW8823_SCL);
}
/////////////////////////////////////////
// Set I2C SDA pin high/low
//
// Arguments: bSet - high/low bit
/////////////////////////////////////////
void i2cSetSDA_tw8823(BYTE bSet)
{
if(bSet) GPIO_SetBits(P_TW8823_SDA_PORT, P_TW8823_SDA);
else GPIO_ResetBits(P_TW8823_SDA_PORT, P_TW8823_SDA);
}
//////////////////////////////////////////////////////////////
// Delay 4us
//////////////////////////////////////////////////////////////
#define i2c_Delay_tw8823()
//{
//delay_10us();
//delay_5us();
//delay_1us();
//delay_1us();
//delay_1us();
//}
//////////////////////////////////////////////////////
// I2C start signal.
//
// SCL ________
// \_________
// SDA _____
// \____________
//
// Return value: None
//////////////////////////////////////////////////////
BYTE i2c_Start_tw8823(void)
{
BYTE bStatus = TRUE; // success status
i2cSetSDA_tw8823(_HIGH);
i2c_Delay_tw8823();
i2cSetSCL_tw8823(_HIGH);
i2c_Delay_tw8823();
if ((i2cSCL_PIN_STATUS_tw8823() == _LOW) || (i2cSDA_PIN_STATUS_tw8823() == _LOW))
{
bStatus = FALSE;
}
else
{
i2cSetSDA_tw8823(_LOW);
i2c_Delay_tw8823();
i2cSetSCL_tw8823(_LOW);
i2c_Delay_tw8823();
}
return bStatus;
}
/////////////////////////////////////////
// I2C stop signal.
//
// ____________
// SCL _______/
// _________
// SDA __________/
/////////////////////////////////////////
void i2c_Stop_tw8823(void)
{
/* Configure I2C1 pins: SCL and SDA */
i2cSetSCL_tw8823(_LOW);
i2c_Delay_tw8823();
i2cSetSDA_tw8823(_LOW);
i2c_Delay_tw8823();
i2cSetSCL_tw8823(_HIGH);
i2c_Delay_tw8823();
i2cSetSDA_tw8823(_HIGH);
i2c_Delay_tw8823();
}
//////////////////////////////////////////////////////////////////////////
// I2C send byte to device.
//
// Arguments: ucVal - send byte
// Return value: I2C acknowledge bit
// I2C_ACKNOWLEDGE/I2C_NON_ACKNOWLEDGE
//////////////////////////////////////////////////////////////////////////
BYTE i2c_SendByte_tw8823(BYTE ucVal)
{
BYTE ucMask = 0x80;
BYTE bAck=FALSE; // acknowledge bit
BYTE i;
while(ucMask)
{
if (ucVal & ucMask)
i2cSetSDA_tw8823(_HIGH);
else
i2cSetSDA_tw8823(_LOW);
i2c_Delay_tw8823();
i2cSetSCL_tw8823(_HIGH); // clock
i2c_Delay_tw8823();
i2cSetSCL_tw8823(_LOW);
i2c_Delay_tw8823();
ucMask >>= 1; // next
} // while
// recieve acknowledge
i2cSetSDA_tw8823(_HIGH);
i2c_Delay_tw8823();
i2cSetSCL_tw8823(_HIGH);
i2c_Delay_tw8823();
////////////////////////////////////////////////////////////
///ACK check
for(i=0;i<100;i++)
{
if(i2cSDA_PIN_STATUS_tw8823()==0)
{
break;
}
i2c_Delay_tw8823();
}
//
if(i<100)
//
i=9;
//
else
//
i=0;
bAck = i2cSDA_PIN_STATUS_tw8823(); // recieve acknowlege
///////////////////////////////////////////////////////////
i2cSetSCL_tw8823(_LOW);
return (bAck);
}
//////////////////////////////////////////////////////////////////////////
// I2C receive byte from device.
//
// Return value: receive byte
//////////////////////////////////////////////////////////////////////////
BYTE i2c_ReceiveByte_tw8823(BYTE bAck)
{
BYTE ucReceive = 0;
BYTE ucMask = 0x80;
i2cSetSDA_tw8823(_HIGH);
while(ucMask)
{
i2cSetSCL_tw8823(_HIGH);
i2c_Delay_tw8823();
if(GPIO_ReadInputDataBit(P_TW8823_SDA_PORT, P_TW8823_SDA) == _HIGH)
ucReceive |= ucMask;
i2cSetSCL_tw8823(_LOW);
i2c_Delay_tw8823();
ucMask >>= 1;
} // while
if (bAck) // acknowledge
i2cSetSDA_tw8823(I2C_ACKNOWLEDGE);
else // non-acknowledge
i2cSetSDA_tw8823(I2C_NON_ACKNOWLEDGE);
i2c_Delay_tw8823();
i2cSetSCL_tw8823(_HIGH);
i2c_Delay_tw8823();
i2cSetSCL_tw8823(_LOW);
return ucReceive;
}
//////////////////////////////////////////////////////////////////////////
// I2C access start.
//
// Arguments: ucSlaveAdr - slave address
// trans_t - I2C_TRANS_WRITE/I2C_TRANS_READ
//////////////////////////////////////////////////////////////////////////
BYTE i2c_AccessStart_tw8823(BYTE ucSlaveAdr, I2C_Direction trans_t)
{
BYTE 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_tw8823();
if (i2c_Start_tw8823() == FALSE)
continue;
if (i2c_SendByte_tw8823(ucSlaveAdr) == I2C_ACKNOWLEDGE) // check acknowledge
return TRUE;
//printf("ucSlaveAdr====%x", ucSlaveAdr);
// i2c_Stop_tw8823();
// Delay_1ms(1);
}
return FALSE;
}
/////////////////////////////////////////////////////////////////
// I2C write bytes to device.
//
// Arguments: ucSlaveAdr - slave address
// ucSubAdr - sub address
// pBuf - pointer of buffer
// ucBufLen - length of buffer
/////////////////////////////////////////////////////////////////
BOOL i2cWrite_Sub_tw8823(BYTE ucSlaveAdr, BYTE ucSubAdr, BYTE *pBuf, BYTE ucBufLen)
{
BYTE ucDummy; // loop dummy
//BYTE i;
//i2c_Stop_tw8823();
ucDummy = I2C_ACCESS_DUMMY_TIME;
while(ucDummy--)
{
if (i2c_AccessStart_tw8823(ucSlaveAdr, I2C_WRITE) == FALSE)
{
continue;
}
if (i2c_SendByte_tw8823(ucSubAdr) == I2C_NON_ACKNOWLEDGE) // check non-acknowledge
continue;
while(ucBufLen--) // loop of writting data
{
i2c_SendByte_tw8823(*pBuf); // send byte
pBuf++; // next byte pointer
} // while
break;
} // while
i2c_Stop_tw8823();
if(ucDummy==0)
{
return FALSE;
}
else
{
return TRUE;
}
}
/////////////////////////////////////////////////////////////////
// I2C read bytes from device.
//
// Arguments: ucSlaveAdr - slave address
// ucSubAdr - sub address
// pBuf - pointer of buffer
// ucBufLen - length of buffer
/////////////////////////////////////////////////////////////////
BOOL i2cRead_Sub_tw8823(BYTE ucSlaveAdr, BYTE ucSubAdr, BYTE *pBuf, BYTE ucBufLen)
{
BYTE ucDummy; // loop dummy
//i2c_Stop_tw8823();
ucDummy = I2C_ACCESS_DUMMY_TIME;
while(ucDummy--)
{
if (i2c_AccessStart_tw8823(ucSlaveAdr, I2C_WRITE) == FALSE)
continue;
if (i2c_SendByte_tw8823(ucSubAdr) == I2C_NON_ACKNOWLEDGE) // check non-acknowledge
continue;
if (i2c_AccessStart_tw8823(ucSlaveAdr, I2C_READ) == FALSE)
continue;
while(ucBufLen--) // loop to burst read
{
*pBuf = i2c_ReceiveByte_tw8823(ucBufLen); // receive byte
pBuf++; // next byte pointer
} // while
break;
} // while
i2c_Stop_tw8823();
if(ucDummy==0)
{
return FALSE;
}
else
{
return TRUE;
}
}
void WriteTW88(Byte addr,Byte b)
{
Byte buff[1];
buff[0]=b;
i2cWrite_Sub_tw8823(TW8823_ADDRESS,addr,buff,1);
}
Byte ReadTW88(Byte addr)
{
Byte buff[1];
i2cRead_Sub_tw8823(TW8823_ADDRESS,addr,buff,1);
return buff[0];
}
void WriteTW88_Page(WORD addr,Byte b)
{
WriteTW88( 0xff, HIBYTE(addr ));
WriteTW88(LOBYTE(addr),b);
}
Byte ReadTW88_Page(WORD addr)
{
WriteTW88( 0xff, HIBYTE(addr) );
return ReadTW88(LOBYTE(addr));
}
void WriteRadioFM(Byte addr,Byte b)
{
Byte buff[1];
buff[0]=b;
i2cWrite_Sub_tw8823(RadioFM_ADDRESS,addr,buff,1);
}
Byte ReadRadioFM(Byte addr)
{
Byte buff[1];
i2cRead_Sub_tw8823(RadioFM_ADDRESS,addr,buff,1);
return buff[0];
}
#endif