/* This is driver/utility program to scan the devices on the NXT port 1 SCANS ON PORT 1 ONLY. V1.2 written by Dr. Nitin Patil 2008(c) mindsensors.com for more info visit www.mindsensors.com */ #define i2cScanVersion 0x00 #define i2cScanDeviceName 0x10 #define i2cScanPort S1 // Connect sensor to this port!! byte replyMsg[10]; ////////////////////////////////////////////////////////////////////////////// // // Read the data from I2C sensor // // ///////////////////////////////////////////////////////////////////////////// byte I2Cscan(byte ScanID ) { byte i2cscanMsg[4]; const byte MsgSize = 0; const byte i2cScanAddress = 1; const byte ReadAddress = 2; // Build the I2C message i2cscanMsg[MsgSize] = 2; i2cscanMsg[i2cScanAddress] = ScanID ; i2cscanMsg[ReadAddress] =i2cScanDeviceName ; replyMsg[0]=0x00; while (nI2CStatus[i2cScanPort] == STAT_COMM_PENDING); // Wait for I2C bus to be ready sendI2CMsg(i2cScanPort, i2cscanMsg[0], 8); // Send the message if(nI2CStatus[i2cScanPort] == ERR_COMM_BUS_ERR)return 0; while (nI2CStatus[i2cScanPort] == STAT_COMM_PENDING); // Wait for I2C bus to be ready readI2CReply(i2cScanPort, replyMsg[0], 8); // read all the six data bytes if(replyMsg[0]==0x00) return 0; return 1; } ////////////////////////////////////////////////////////////////////////////// // // scans the port 1 and Display the results on NXT display // ///////////////////////////////////////////////////////////////////////////// task main() { int i2cScanID; ubyte count; string s2; string s1; int r, i; nI2CBytesReady[i2cScanPort] = 0; SensorType[i2cScanPort] = sensorI2CCustom9V; nxtDisplayTextLine(0,"mindsensors.com"); nxtDisplayTextLine(1,"Name: i2cID(hex)"); nxtDisplayTextLine(2,"nothing found?"); i2cScanID = 0x02; count=1; while(i2cScanID<0xfe) { if(I2Cscan(i2cScanID)==1) { count++; i = 0; s1 = ""; while ( replyMsg[i] != 0x00 ) { r = replyMsg[i]; StringFormat (s2, "%s%c", s1, r); s1 = s2; i++; } StringFormat (s2, "%s: %x", s1, i2cScanID); nxtDisplayTextLine(count,"%s", s2); } i2cScanID += 0x02; } wait10Msec(1000); StopAllTasks(); }