Hi!
I'm trying to interface the Lego NXT ultrasonic sensor with an atmega168 microcontroller, using avrlib. I can't get it to work properly though. The sensor is attached to the microcontroller this way:
pin1 -> ~8V
pin2 -> GND
pin3 -> GND
pin4 -> 5V
pin5 -> pin28 of atmega168 (SCL)
pin6 -> pin27 of atmega168 (SDA)
The code used to initialize I2C:
i2cInit();
i2cSetBitrate(10);
The following code is used to get the sensor type & read measurements:
void get_sensor_type(void)
{
u08 data[8] = {0};
data[0] = 0x10;
i2cMasterSendNI(0x02, 1, data);
i2cMasterReceiveNI(0x02, 8, data);
...
}
u08 nxt_ultrasonic_scan(void)
{
u08 data[8] = {0};
data[0] = 0x42;
i2cMasterSendNI(0x02, 1, data);
i2cMasterReceiveNI(0x02, 1, data);
return data[0];
}
I modified the avrlib functions a bit, to see whether I'm getting ACKs properly. It seems i2cMasterSendNI() function always succeeds, I get an ACK for all the commands. However, i2cMasterReceiveNI() always fails at the point where I'm sending SLA+R. Depending on the configuration, one of the following things happen:
* The i2cWaitForComplete() function after sending SLA+R never returns
* The i2cWaitForComplete() function does return, but with TW_BUS_ERROR error
* The i2cWaitForComplete() function does return, but with TW_MR_SLA_NACK error
Additionally, I've tried:
* Adding delays (from 200us to 500ms) after each read/write.
* Adding external pull-ups to the board (82K)
* To generate REPEATED START, I've tried sending all combinations of START, STOP and STOP/START. None of them work. STOP/START always generates TW_MR_SLA_NACK error as described above. Plan STOP / START always generate a TW_BUS_ERROR or a never returning i2cWaitForComplete() after sending SLA+R.
So basically, it seems I'm able to send stuff to the device, but unable to read anything from it. Has somebody here been able to interface the NXT Ultrasonic sensor with an external microcontroller? Help needed here.