大家好,我这里是一个较为完整的I2C程序,但是有一点
不明白,为什么还有个一个BOOL i2cA_clock( void )函数
呢,我从前写汇编时可没有用这个CLOCK的
void delay_5us(void)
{
_nop_();
}
//---------------------------------------------------------
void i2cAStartA( void )
{
_sdaA = HIGH ;
_sclA = HIGH ;
delay_5us() ;
_sdaA = LOW ;
delay_5us() ;
_sclA = LOW ;
}
void i2cAStop( void )
{
delay_5us() ;
_sclA = LOW ;
_sdaA = LOW ;
delay_5us() ;
_sclA = HIGH ;
delay_5us() ;
_sdaA = HIGH ;
}
/*---------------------------------------------------------
void i2cAInit( void )
{
i2cAStop() ;
}
*/
//---------------------------------------------------------
BOOL i2cA_clock( void )//?????????????????
{
BOOL sample ;
_sclA = HIGH ;
delay_5us() ;
sample = _sdaA ;
_sclA = LOW ;
delay_5us() ;
return ( sample ) ;
}
//---------------------------------------------------------
void i2cANotAck(void)
{
/*receive state, ack set by master*/
_sdaA = HIGH ;
i2cA_clock() ;
}
//---------------------------------------------------------
void i2cAGiveAck(void)
{
//receive state, ack set by master
_sdaA = LOW ;
i2cA_clock() ;
_sdaA = HIGH ;
}
//-----------------------------------------------------------------------------
UINT8 i2cAReceive(void)
{
/*Read ack.*/
UINT8 value = 0 ;
register UINT8 i ;
for ( i=0 ; i<8 ; i++ ) {
value *= 2 ;
_sdaA = HIGH ;
if (i2cA_clock())
value++ ;
}
return ( value ) ;
}
//-----------------------------------------------------------------------------
BOOL i2cATransmit(UINT8 value)
{
register UINT8 i ;
// send the eight data bits
for ( i=0 ; i<8 ; i++ )
{
_sdaA = (bit)( value & 0x80 ) ;
value = value << 1 ;
i2cA_clock() ;
}
// solicit ACK.
_sdaA = HIGH ;
return (!i2cA_clock()) ;
}