Ian Wolf
/
TEMPFINAL
Fork of Temperature_Reading1 by
main.cpp@0:1f9fae46bbe1, 2017-08-11 (annotated)
- Committer:
- 212600191
- Date:
- Fri Aug 11 16:20:22 2017 +0000
- Revision:
- 0:1f9fae46bbe1
- Child:
- 1:1de97b1145f3
temperature;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
212600191 | 0:1f9fae46bbe1 | 1 | #include "mbed.h" |
212600191 | 0:1f9fae46bbe1 | 2 | |
212600191 | 0:1f9fae46bbe1 | 3 | // Read temperature from LM75BD |
212600191 | 0:1f9fae46bbe1 | 4 | |
212600191 | 0:1f9fae46bbe1 | 5 | I2C i2c(PB_9 , PB_8 ); |
212600191 | 0:1f9fae46bbe1 | 6 | |
212600191 | 0:1f9fae46bbe1 | 7 | Serial pc (SERIAL_TX, SERIAL_RX); |
212600191 | 0:1f9fae46bbe1 | 8 | |
212600191 | 0:1f9fae46bbe1 | 9 | const int addr7bit = 0x48; // 7 bit I2C address |
212600191 | 0:1f9fae46bbe1 | 10 | const int addr8bit = 0x48 << 1; // 8bit I2C address, 0x90 |
212600191 | 0:1f9fae46bbe1 | 11 | |
212600191 | 0:1f9fae46bbe1 | 12 | int main() { |
212600191 | 0:1f9fae46bbe1 | 13 | pc.baud(250000); |
212600191 | 0:1f9fae46bbe1 | 14 | char cmd[2]; |
212600191 | 0:1f9fae46bbe1 | 15 | while (1) { |
212600191 | 0:1f9fae46bbe1 | 16 | cmd[0] = 0x01; |
212600191 | 0:1f9fae46bbe1 | 17 | cmd[1] = 0x00; |
212600191 | 0:1f9fae46bbe1 | 18 | i2c.write(addr8bit, cmd, 2); |
212600191 | 0:1f9fae46bbe1 | 19 | |
212600191 | 0:1f9fae46bbe1 | 20 | wait(0.5); |
212600191 | 0:1f9fae46bbe1 | 21 | |
212600191 | 0:1f9fae46bbe1 | 22 | cmd[0] = 0x00; |
212600191 | 0:1f9fae46bbe1 | 23 | i2c.write(addr8bit, cmd, 1); |
212600191 | 0:1f9fae46bbe1 | 24 | i2c.read( addr8bit, cmd, 2); |
212600191 | 0:1f9fae46bbe1 | 25 | |
212600191 | 0:1f9fae46bbe1 | 26 | float tmp = (float((cmd[0]<<8)|cmd[1]) / 256.0); |
212600191 | 0:1f9fae46bbe1 | 27 | printf("Temp = %.2f\r\n", tmp); |
212600191 | 0:1f9fae46bbe1 | 28 | } |
212600191 | 0:1f9fae46bbe1 | 29 | } |