Riad_ Bajraktarevic
/
27_02_2020_Lab
I2C
I2c.cpp@0:4ae0b37b2729, 2020-02-27 (annotated)
- Committer:
- riad
- Date:
- Thu Feb 27 18:35:29 2020 +0000
- Revision:
- 0:4ae0b37b2729
I2C
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
riad | 0:4ae0b37b2729 | 1 | #include "mbed.h" |
riad | 0:4ae0b37b2729 | 2 | |
riad | 0:4ae0b37b2729 | 3 | DigitalOut myled(LED1); |
riad | 0:4ae0b37b2729 | 4 | I2C i2c (p28, p27); |
riad | 0:4ae0b37b2729 | 5 | |
riad | 0:4ae0b37b2729 | 6 | const int addr = 0x90; |
riad | 0:4ae0b37b2729 | 7 | |
riad | 0:4ae0b37b2729 | 8 | int main() { |
riad | 0:4ae0b37b2729 | 9 | char cmd [2]; |
riad | 0:4ae0b37b2729 | 10 | while(1) { |
riad | 0:4ae0b37b2729 | 11 | cmd [0] = 0x01; |
riad | 0:4ae0b37b2729 | 12 | cmd [1] = 0x00; |
riad | 0:4ae0b37b2729 | 13 | i2c.write(addr, cmd, 2); |
riad | 0:4ae0b37b2729 | 14 | wait (0.5); |
riad | 0:4ae0b37b2729 | 15 | |
riad | 0:4ae0b37b2729 | 16 | cmd[0] = 0x00; |
riad | 0:4ae0b37b2729 | 17 | i2c.write(addr, cmd, 1); |
riad | 0:4ae0b37b2729 | 18 | i2c.read(addr, cmd, 2); |
riad | 0:4ae0b37b2729 | 19 | |
riad | 0:4ae0b37b2729 | 20 | float tmp = (cmd[0]<<8|cmd[1]) / 256.0; |
riad | 0:4ae0b37b2729 | 21 | printf("Temp = %.2f\n", tmp); |
riad | 0:4ae0b37b2729 | 22 | |
riad | 0:4ae0b37b2729 | 23 | } |
riad | 0:4ae0b37b2729 | 24 | } |