Hello World for I2C
mbed 2 and mbed OS 5
This is an mbed 2 example. For an mbed-os example, please see:
Import programI2C_HelloWorld
I2C Hello World, I2C Example
main.cpp@0:f76c26307f9a, 2013-02-14 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Feb 14 17:24:48 2013 +0000
- Revision:
- 0:f76c26307f9a
I2C HelloWorld
Who changed what in which revision?
User | Revision | Line number | New 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 | |
mbed_official | 0:f76c26307f9a | 5 | I2C i2c(p28, p27); |
mbed_official | 0:f76c26307f9a | 6 | |
mbed_official | 0:f76c26307f9a | 7 | const int addr = 0x90; |
mbed_official | 0:f76c26307f9a | 8 | |
mbed_official | 0:f76c26307f9a | 9 | int main() { |
mbed_official | 0:f76c26307f9a | 10 | char cmd[2]; |
mbed_official | 0:f76c26307f9a | 11 | while (1) { |
mbed_official | 0:f76c26307f9a | 12 | cmd[0] = 0x01; |
mbed_official | 0:f76c26307f9a | 13 | cmd[1] = 0x00; |
mbed_official | 0:f76c26307f9a | 14 | i2c.write(addr, cmd, 2); |
mbed_official | 0:f76c26307f9a | 15 | |
mbed_official | 0:f76c26307f9a | 16 | wait(0.5); |
mbed_official | 0:f76c26307f9a | 17 | |
mbed_official | 0:f76c26307f9a | 18 | cmd[0] = 0x00; |
mbed_official | 0:f76c26307f9a | 19 | i2c.write(addr, cmd, 1); |
mbed_official | 0:f76c26307f9a | 20 | i2c.read(addr, cmd, 2); |
mbed_official | 0:f76c26307f9a | 21 | |
mbed_official | 0:f76c26307f9a | 22 | float tmp = (float((cmd[0]<<8)|cmd[1]) / 256.0); |
mbed_official | 0:f76c26307f9a | 23 | printf("Temp = %.2f\n", tmp); |
mbed_official | 0:f76c26307f9a | 24 | } |
mbed_official | 0:f76c26307f9a | 25 | } |