Code:
DrvI2C_Ctrl(I2C_PORT1, 1, 0, 1, 0); //Start I2C
while (DrvI2C_GetStatus(I2C_PORT1) != 0x08);
DrvI2C_WriteData(I2C_PORT1, 0xE0); //Slave Address
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 0);
while (DrvI2C_GetStatus(I2C_PORT1) != 0x18);
DrvI2C_WriteData(I2C_PORT1, 3); //Register 3
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 0);
while (DrvI2C_GetStatus(I2C_PORT1) != 0x28);
DrvI2C_WriteData(I2C_PORT1, 2); //Register 2
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 0);
while (DrvI2C_GetStatus(I2C_PORT1) != 0x28);
// DrvI2C_Ctrl(I2C_PORT1, 0, 1, 1, 0); //Stop I2C
// while (DrvI2C_GetStatus(I2C_PORT1) != 0xF8);
DrvI2C_Ctrl(I2C_PORT1, 1, 0, 1, 0); //Restart I2C
while (DrvI2C_GetStatus(I2C_PORT1) != 0x10);
DrvI2C_WriteData(I2C_PORT1, 0xE1); //Slave Address + 1 = Read Command
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 0);
while (DrvI2C_GetStatus(I2C_PORT1) != 0x40);
uiMSB = (DrvI2C_ReadData(I2C_PORT1)); //Read MSB
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 1); //Clear Int Flag and ACK
while (DrvI2C_GetStatus(I2C_PORT1) != 0x50);
uiLSB = DrvI2C_ReadData(I2C_PORT1); //Read LSB
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 0); //Clear Int Flag and NACK
while (DrvI2C_GetStatus(I2C_PORT1) != 0x58);
DrvI2C_Ctrl(I2C_PORT1, 0, 1, 1, 0); //Stop I2C and Clear Int Flag
Ich kann nur Entfernungen bis exakt 255, also 0xFF messen, danach springt er auf 0 zurück
Lesezeichen