I am not familiar with this device but I see Xander has a RobotC driver for it. Given his code it should be trivial to write an equivalent version for NXC. Try this program:
Code: Select all
// Maxim Integrated Products MAX127 default I2C device address
#define MIP_ADDR_MAX127 0x50
int SensorMAX127Value(byte port, byte i2cAddress, byte adChannel)
{
int _chVal = -1;
byte count = 2;
byte msg[], reply[];
// Control byte is calculated as follows:
// start bit (7) + channel number bit-shifted into place
byte ctrlByte = (1 << 7) + (adChannel << 4);
ArrayBuild(msg, i2cAddress, ctrlByte);
if (I2CBytes(port, msg, count, reply))
{
// Convert the bytes into ints
// 1st byte contains bits 11-4 of the channel's value
// 2nd byte contains bits 3-0 of the channel's value, followed by 4 0's
// We'll need to shift the 1st byte left by 4 and the 2nd byte to the right by 4
_chVal = (reply[0] << 4) + (reply[1] >> 4);
}
return _chVal;
}
task main()
{
SetSensorLowspeed(S1);
byte addr = MIP_ADDR_MAX127; // change this if the i2c address is not 0x50
while (true)
{
byte channel = 0;
NumOut(0, LCD_LINE1, SensorMAX127Value(S1, addr, channel++));
NumOut(0, LCD_LINE2, SensorMAX127Value(S1, addr, channel++));
NumOut(0, LCD_LINE3, SensorMAX127Value(S1, addr, channel++));
NumOut(0, LCD_LINE4, SensorMAX127Value(S1, addr, channel++));
NumOut(0, LCD_LINE5, SensorMAX127Value(S1, addr, channel++));
NumOut(0, LCD_LINE6, SensorMAX127Value(S1, addr, channel++));
NumOut(0, LCD_LINE7, SensorMAX127Value(S1, addr, channel++));
NumOut(0, LCD_LINE8, SensorMAX127Value(S1, addr, channel++));
}
}
The I2CSendCommand function is really designed for specific types of sensors which have command registers to which you can write commands. Other than I2CBytes you would normally use I2CWrite, I2CCheckStatus, and I2CRead in 3 steps to start the I2C transaction, wait for it to finish, and then read the data that was sent back to the NXT from the device. I2CBytes wraps all that in a high level API function.
John Hansen