Learning I2C

05 Apr 2011

I've read the Technical Spec for the srf08 and what I'd like to do is change the address from the factory default and increase the range of the device. To change the address from 0xE0 to say 0xF2 the spec says write 0xA0, 0xAA, 0xA5, 0xF2 sent in the correct sequence to address 0xE0. For setting the range I have to send a value say 0x24 to the range register. Does the code below - I2C i2c(p9, p10); i2c.write(addr, cmd, 1) basically give me the pieces I need to write to the device? I understand the mbed is a master device so connected devices are slave.

#include "mbed.h"

I2C i2c(p9, p10);        // sda, scl
Serial pc(USBTX, USBRX); // tx, rx

const int addr = 0x70; // define the I2C Address

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

        wait(0.07);              // Could also poll, 65ms is typical

        // Set pointer to location 2 (first echo)
        cmd[0] = 0x2;
        i2c.write(addr, cmd, 1);
        i2c.read(addr, cmd, 2); // read the two-byte echo result

        // print the ranging data to the screen
        float echo = 0.01 * ((cmd[0] << 8) + cmd[1]);
        pc.printf("Range = %.2f\n", echo);
        wait(0.1);
    }
}