Lets face it I am a terrible blogger, but no one reads this anyway so who cares.
Anyway recently I had some serious grief getting a Devantech CMPS03 digital compass to work with a Lego NXT. The info doesn't appear to be anywhere else on line and I couldn't find a decent I2C example in nxc.
Here is some basic code showing how to get it working.
#define cmpAddr 0xC0 // Machine address of the compass
#define cmpPort S1 // Port that the compass is plugged in to
byte bufLSWrite[] = {cmpAddr, 0x01, 0xC1}; // Written to the compass... (Compass, give me data from register 1, now)
// Take a compass reading
int compassReading()
{
byte r;
byte outbuf[1];
byte byteNumber;
int result;
byteNumber = 1; // Read byte 1
while (I2CCheckStatus(cmpPort) == STAT_COMM_PENDING);
if (I2CBytes(cmpPort, bufLSWrite, byteNumber, outbuf))
{ r = outbuf[0];
}
else
{ r = -1;
}
result = r;
return result;
}
task main()
{
int reading;
int counter = 0;
SetSensorLowspeed(cmpPort);
while( true )
{ reading = compassReading();
NumOut( 1, LCD_LINE1, counter++, false );
NumOut( 1, LCD_LINE3, reading, false );
Wait(1000);
}
}
This is actually just a stepping stone to getting the sensor to work via NXT++ from my netbook but I'll post that code later.
Monday, February 14, 2011
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment