I2C Hello World, I2C Example

Fork of I2C_HelloWorld_Mbed by Mbed

Use

The mbed I2C API uses 8 bit addressing and will auto append the 0 or 1 for read/write mode. Please keep in mind that every I2C set up has its own quirks so this example may not work out of the box for your sensor / application. Make sure to check the data sheet for your part for the timing / register access specification.

API

API reference.

Import librarymbed

No documentation found.
Committer:
mbedAustin
Date:
Sun Sep 21 19:01:55 2014 +0000
Revision:
2:ab0e4b55d7da
Parent:
0:f76c26307f9a
Child:
3:df6232c70efd
added notes on use for clarity. made it obvious we use 8bit addressing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:f76c26307f9a 1 #include "mbed.h"
mbed_official 0:f76c26307f9a 2
mbed_official 0:f76c26307f9a 3 // Read temperature from LM75BD
mbed_official 0:f76c26307f9a 4
mbedAustin 2:ab0e4b55d7da 5 I2C i2c(I2C_SDA , I2C_SCL );
mbed_official 0:f76c26307f9a 6
mbedAustin 2:ab0e4b55d7da 7 const int addr7bit = 0x48; // 7 bit I2C address
mbedAustin 2:ab0e4b55d7da 8 const int addr8bit = 0x48 << 1; // 8bit I2C address, 0x90
mbed_official 0:f76c26307f9a 9
mbed_official 0:f76c26307f9a 10 int main() {
mbed_official 0:f76c26307f9a 11 char cmd[2];
mbed_official 0:f76c26307f9a 12 while (1) {
mbed_official 0:f76c26307f9a 13 cmd[0] = 0x01;
mbed_official 0:f76c26307f9a 14 cmd[1] = 0x00;
mbedAustin 2:ab0e4b55d7da 15 i2c.write(addr8bit, cmd, 2);
mbed_official 0:f76c26307f9a 16
mbed_official 0:f76c26307f9a 17 wait(0.5);
mbed_official 0:f76c26307f9a 18
mbed_official 0:f76c26307f9a 19 cmd[0] = 0x00;
mbedAustin 2:ab0e4b55d7da 20 i2c.write(addr8bit, cmd, 1);
mbedAustin 2:ab0e4b55d7da 21 i2c.read( addr8bit, cmd, 2);
mbed_official 0:f76c26307f9a 22
mbed_official 0:f76c26307f9a 23 float tmp = (float((cmd[0]<<8)|cmd[1]) / 256.0);
mbed_official 0:f76c26307f9a 24 printf("Temp = %.2f\n", tmp);
mbed_official 0:f76c26307f9a 25 }
mbed_official 0:f76c26307f9a 26 }