View unanswered posts | View active topics It is currently Mon Sep 01, 2014 7:27 pm






Reply to topic  [ 2 posts ] 
I2C 
Author Message
Rookie

Joined: Tue Jul 01, 2008 1:47 pm
Posts: 2
Post I2C
Hi everybody.

I'm new in the forums and noob in robotC, I've been working doing easy things, working a little with the encoders,motors, NXT sensors and those kind of things.

But now I've begun to work with Hitechnic's IRSeeker and I've found that is needed to acces to some directions 0x42 to 0x47 to read the sensors signal strength.

Searching in the forum i've found lots of things about this, but much of them talked about I2C and I don´t know what is that or how is it used?

Dou you know where could I find some information about how I2C works (easily explained) or how to read those directions of the sensor?


I copy a program found in this forum where you use I2C, I've tried the program and it works, with some problems:

#define MsgSize 6 // The length of the message you want the NXT to write to the i2c bus
#define DeviceAddress 0x02 // The i2c address of the sensor. With NXT it is usually 0x02
#define DataLocationPtr 0x42 // The address of the Register to start reading from
#define sensorPort S3 // Port the sensor is connected to on the NXT

task main()
{

typedef struct{
byte nMsgSize;
byte nDeviceAddress;
byte nDataLocationPtr;
} TI2C_Output;

SensorType[sensorPort] = sensorI2CCustom;
wait10Msec(5);
nI2CBytesReady[sensorPort]=0;
while(nI2CStatus[sensorPort]== STAT_COMM_PENDING)
wait1Msec(2);

byte replyMessage[MsgSize];

TI2C_Output sOutput;

nxtDisplayTextLine(0, " HiTechnic");
nxtDisplayTextLine(1, " IRSeeker Test");

while(true)
{
nI2CBytesReady[sensorPort] = 0;

sOutput.nMsgSize = MsgSize;
sOutput.nDeviceAddress = DeviceAddress;
sOutput.nDataLocationPtr = DataLocationPtr;

// send the message we have just created to the I2C interface on port "sensorPort"
sendI2CMsg(sensorPort, sOutput.nMsgSize, sizeof(replyMessage));

// wait for the I2C interface to become ready
while (nI2CStatus[sensorPort] == STAT_COMM_PENDING)
wait1Msec(2);

// If there is no error on the port get the data
if (nI2CStatus[sensorPort] == NO_ERR)
{
// read the sensor data from the I2C interface
readI2CReply(sensorPort, replyMessage[0], sizeof(replyMessage));



// get the sensor data from the buffer
int direction = (replyMessage[0]&0x00FF); // Direction data

int st1 = (replyMessage[1] & 0x00FF); // Sensor 1 signal strength
int st2 = (replyMessage[2] & 0x00FF); // Sensor 2 signal strength
int st3 = (replyMessage[3] & 0x00FF); // Sensor 3 signal strength
int st4 = (replyMessage[4] & 0x00FF); // Sensor 4 signal strength
int st5 = (replyMessage[5] & 0x00FF); // Sensor 5 signal strength

//Display the sensor data on the NXT screen
//nxtDisplayTextLine(2, "Port: %d", (int)sensorPort+1); //display Port Sensor is connected to

nxtDisplayTextLine(2, "Dir: %d", direction);//display direction data

nxtDisplayTextLine(3, "s1: %d",st1); //Sensor 1 signal strength
nxtDisplayTextLine(4, "s2: %d",st2); //Sensor 2 signal strength
nxtDisplayTextLine(5, "s3: %d",st3); //Sensor 3 signal strength
nxtDisplayTextLine(6, "s4: %d",st4); //Sensor 4 signal strength
nxtDisplayTextLine(7, "s5: %d",st5); //Sensor 5 signal strength

wait1Msec(125);
}
else
{ //Error on port
PlayTone(5000, 1000);
nxtDisplayTextLine(2, "ERROR ON I2C");
nxtDisplayTextLine(3, "INTERFACE");
}
}
}


The problems are for example that "direction" is suposed to be an integer between 1-9 and it returns any number.

Thanks a lot and sorry for my english.


Tue Jul 01, 2008 2:12 pm
Profile
Expert
User avatar

Joined: Fri Nov 09, 2007 4:51 am
Posts: 121
Location: Hungary, Europe
Post Re: I2C
Depends on what you are interested in and what level.

I2C is a simple and robust protocol to connect digital "intelligent" peripherals to a microcomputer. I2C allows connection of over 100 devices onto one port (bus), where different devices are distinguished by a different address. It is also possible that single (physical) device offers more logical peripherals -- like three axes of a combined acceleration sensor.

For origins and more details http://en.wikipedia.org/wiki/I2c is a good starting point.


Fri Jul 11, 2008 2:13 pm
Profile
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.