I2C Hello World, I2C Example
Fork of I2C_HelloWorld_Mbed by
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
main.cpp@5:afb2d7fd50ad, 2017-06-23 (annotated)
- Committer:
- sarahmarshy
- Date:
- Fri Jun 23 18:11:39 2017 -0500
- Revision:
- 5:afb2d7fd50ad
- Parent:
- 3:df6232c70efd
"Update mbed-os"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 3:df6232c70efd | 1 | /* mbed Example Program |
mbedAustin | 3:df6232c70efd | 2 | * Copyright (c) 2006-2014 ARM Limited |
mbedAustin | 3:df6232c70efd | 3 | * |
mbedAustin | 3:df6232c70efd | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbedAustin | 3:df6232c70efd | 5 | * you may not use this file except in compliance with the License. |
mbedAustin | 3:df6232c70efd | 6 | * You may obtain a copy of the License at |
mbedAustin | 3:df6232c70efd | 7 | * |
mbedAustin | 3:df6232c70efd | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbedAustin | 3:df6232c70efd | 9 | * |
mbedAustin | 3:df6232c70efd | 10 | * Unless required by applicable law or agreed to in writing, software |
mbedAustin | 3:df6232c70efd | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbedAustin | 3:df6232c70efd | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbedAustin | 3:df6232c70efd | 13 | * See the License for the specific language governing permissions and |
mbedAustin | 3:df6232c70efd | 14 | * limitations under the License. |
mbedAustin | 3:df6232c70efd | 15 | */ |
mbed_official | 0:f76c26307f9a | 16 | #include "mbed.h" |
mbed_official | 0:f76c26307f9a | 17 | |
mbed_official | 0:f76c26307f9a | 18 | // Read temperature from LM75BD |
mbed_official | 0:f76c26307f9a | 19 | |
mbedAustin | 2:ab0e4b55d7da | 20 | I2C i2c(I2C_SDA , I2C_SCL ); |
mbed_official | 0:f76c26307f9a | 21 | |
mbedAustin | 2:ab0e4b55d7da | 22 | const int addr7bit = 0x48; // 7 bit I2C address |
mbedAustin | 2:ab0e4b55d7da | 23 | const int addr8bit = 0x48 << 1; // 8bit I2C address, 0x90 |
mbed_official | 0:f76c26307f9a | 24 | |
mbed_official | 0:f76c26307f9a | 25 | int main() { |
mbed_official | 0:f76c26307f9a | 26 | char cmd[2]; |
mbed_official | 0:f76c26307f9a | 27 | while (1) { |
mbed_official | 0:f76c26307f9a | 28 | cmd[0] = 0x01; |
mbed_official | 0:f76c26307f9a | 29 | cmd[1] = 0x00; |
mbedAustin | 2:ab0e4b55d7da | 30 | i2c.write(addr8bit, cmd, 2); |
mbed_official | 0:f76c26307f9a | 31 | |
mbed_official | 0:f76c26307f9a | 32 | wait(0.5); |
mbed_official | 0:f76c26307f9a | 33 | |
mbed_official | 0:f76c26307f9a | 34 | cmd[0] = 0x00; |
mbedAustin | 2:ab0e4b55d7da | 35 | i2c.write(addr8bit, cmd, 1); |
mbedAustin | 2:ab0e4b55d7da | 36 | i2c.read( addr8bit, cmd, 2); |
mbed_official | 0:f76c26307f9a | 37 | |
mbed_official | 0:f76c26307f9a | 38 | float tmp = (float((cmd[0]<<8)|cmd[1]) / 256.0); |
mbed_official | 0:f76c26307f9a | 39 | printf("Temp = %.2f\n", tmp); |
mbed_official | 0:f76c26307f9a | 40 | } |
mbed_official | 0:f76c26307f9a | 41 | } |