struggling with C & The I2C Hello World Example

11 Apr 2010

Hello Co-users of the MBED nxp,

I have the ambition to learn and comprehend C whilst building my first project.

My first experiments involves a I2C Ultrasonic Range Finder (SRF02).

The Example code works fine for me, but the problem is that i do not understand the whole code..

my isue is with this part:

int main() {
char cmd[2];
while(1) {
cmd[0] = 0x0;            // pointer to command register
cmd[1] = 0x51;           // Start ranging, results in cm
i2c.write(addr, cmd, 2); // Send command string

 

The line " char cmd[2];"  does it create the two char's with cmd[0] and cmd[1]?

and witch one is send on the write command.?  and the 2 that is send as data what does this do?

The rest of the code can be found here: http://mbed.org/handbook/I2C

These thing are propably basic C knowledge, but i can't find any clear explanation for this..

I hope one of you people will help me, Many thanks in advance.!

 

Ramsy.

 

 

 



11 Apr 2010

Ramsey,

The line char cmd[2] creates an array or place holders for two chars cmd[0] and cmd[1].

In the i2c.write(addr,cmd,2) function, the "cmd" variable is defined as a pointer to your 2 char cmd array. You are sending or passing a pointer to the 2 chars.

 

Bob

11 Apr 2010

Thank you Bob,

After your explanation and some experimenting i think i grasp it.

And have a starting point for searching info about arrays..

 

Ramsy