View unanswered posts | View active topics It is currently Fri Oct 30, 2020 3:26 am

Reply to topic  [ 2 posts ] 
change to i2c code may be necessary for RobotC v1.05 
Author Message

Joined: Wed Jun 13, 2007 6:18 am
Posts: 1
Location: Australia
Post change to i2c code may be necessary for RobotC v1.05
Just thought a few people might be interested that in response to a bug report I submitted, Dick Swan has advised me that "between 1.02 and 1.05 (RobotC) true "byte" / "char" variable support was added. Previously variables declared as "byte" were allocated 16-bits".

and that

"In preparation for the move to byte variables, the I2C support functions
were designed with parameters that was a 'byte' array (of the 16-bit
kind). With 1.05 release, the allocation is truly 8-bit values".

The implication for some i2c code is that sometimes successive returned byte values are added or shifted to arrive at the final "sensor" value. If the returned value is between 128-255, it will be considered as a negative value of up to -128. To overcome this sort of issue, Dick has suggested the solution to my problem was to: AND a byte variable with 0x00FF.

To give a practical example of what I mean, Scott (starwarslegokid) asked me about a issue between v1.02 qnd v1.05 with the code he was using for mindsensors compass.

What I have suggested is shown as follows: (changes to the original code is shown with comment // change here)

int cmpsHeading(const tSensors CmpsPort)
byte replyMsg[2];

// change here - variable Heading now integer type
int Heading;

byte cmpsMsg[5];
const byte MsgSize = 0;
const byte CmpsAddress = 1;
const byte ReadAddress = 2;
const byte CommandAddress = 2;
const byte Command = 3;

// Build the I2C message
cmpsMsg[MsgSize] = 3;
cmpsMsg[CmpsAddress] = CmpsID ;
cmpsMsg[CommandAddress] = CmpsCommandReg ;
cmpsMsg[Command] = CmpsReadHeading;

while (nI2CStatus[CmpsPort] == STAT_COMM_PENDING); // Wait for I2C bus
to be ready
sendI2CMsg(CmpsPort, cmpsMsg[0], 0); // Send the

cmpsMsg[MsgSize] = 2;
cmpsMsg[CmpsAddress] = CmpsID ;
cmpsMsg[ReadAddress] = CmpsReadResult ;

while (nI2CStatus[CmpsPort] == STAT_COMM_PENDING); // Wait for I2C
bus to be ready
sendI2CMsg(CmpsPort, cmpsMsg[0], 2); // Send the

while (nI2CStatus[CmpsPort] == STAT_COMM_PENDING); // Wait for I2C
bus to be ready
if ( nI2CStatus[CmpsPort] != NO_ERR ) // probably sensor is missing.

readI2CReply(CmpsPort, replyMsg[0], 2);

// change here - remaining few lines changed
if ( replyMsg[0] == -1 ) { // -1 is reserved to report errors, so
Heading = 0; // change it to zero.
else {
Heading = ( 0x00FF & replyMsg[0] );
Heading += ( (0x00FF & replyMsg[1]) <<8 );
return (Heading);


Scott (starwarslegokid) also asked if the byte/char change
affects messages sent to a i2c device and if that will have to be
altered as well?

The answer in the above example is that no changes are required.

Scott (starwarslegokid), please let me know if my suggested changes work ok.

Hope this all helps


Wed Jun 13, 2007 7:31 am
Profile WWW
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Thanks Brett!!!!! :D :D :D

You just helped allot of people out. Iv been stumped about this too lol, it didn't occur to me to change the heading to and int as well. The code works just like it used to. Ill add your name to my code file and make sure that mindsensors is notified of the change needed for the code.

Good luck with your robots B-)


Mmmm Legos B-)

My Robot Projects:

Fri Jun 15, 2007 1:05 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.