MA82G5D16 IIC 写入
void I2C_Write(uCHAR Dev_Addr, uCHAR Reg_Addr, uCHAR ucData)
{
uINT cnt = 0;
SICON |= STA;
SICON &= ~SI;
cnt = 0;
while( (SICON&SI) != SI)
{
cnt++;
if(cnt > _TWSI_TIME_MAX_)
{
TWI0_Err = 1;
goto TWI0W_End;
}
}
SICON &= ~STA;
SIDAT = Dev_Addr; // send device address
SICON &= ~SI;
cnt = 0;
while( (SICON&SI) != SI)
{
cnt++;
if(cnt > _TWSI_TIME_MAX_)
{
TWI0_Err = 1;
goto TWI0W_End;
}
}
SIDAT = Reg_Addr; // send register address
SICON &= ~SI;
cnt = 0;
while( (SICON&SI) != SI)
{
cnt++;
if(cnt > _TWSI_TIME_MAX_)
{
TWI0_Err = 1;
goto TWI0W_End;
}
}
SIDAT = ucData; // send data
SICON &= ~SI;
cnt = 0;
while( (SICON&SI) != SI)
{
cnt++;
if(cnt > _TWSI_TIME_MAX_)
{
TWI0_Err = 1;
goto TWI0W_End;
}
}
SICON |= STO;
SICON &= ~SI;
cnt = 0;
while( (SICON&STO) != STO)
{
cnt++;
if(cnt > _TWSI_TIME_MAX_)
{
TWI0_Err = 1;
goto TWI0W_End;
}
}
TWI0W_End:
_nop_();
}
|