Read temperature from a TCN75 (or LM75) I2C thermometer
Dependencies: mbed
06_i2c_tcn75
Read temperature from a TCN75 (or LM75) I2C thermometer. I2C address: 1 0 0 1 A2 A2 A0
Hardware requirements:
- FRDM-KL25Z board
- TCN75 (or LM75) thermometer
Wiring scheme:
TCN75 | FRDM-25KLZ |
---|---|
1. SDA | D14 (PTE0) |
2. SCL | D15 (PTE1) |
3. ALERT | (not used) |
4. GND | GND |
5. A2 | GND |
6. A1 | GND |
7. A0 | GND |
8. VCC | 3V3 |
Note: Pins SDA, SCL and Alert must be pulled up by external resistors.
main.cpp@0:84ce5e2a89b1, 2015-11-25 (annotated)
- Committer:
- icserny
- Date:
- Wed Nov 25 07:00:30 2015 +0000
- Revision:
- 0:84ce5e2a89b1
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icserny | 0:84ce5e2a89b1 | 1 | /** 06_i2c_tcn75 |
icserny | 0:84ce5e2a89b1 | 2 | * Read temperature from a TCN75 (or LM75) I2C thermometer |
icserny | 0:84ce5e2a89b1 | 3 | * I2C address: 1 0 0 1 A2 A2 A0 |
icserny | 0:84ce5e2a89b1 | 4 | * |
icserny | 0:84ce5e2a89b1 | 5 | * Hardware requirements: |
icserny | 0:84ce5e2a89b1 | 6 | * - FRDM-KL25Z board |
icserny | 0:84ce5e2a89b1 | 7 | * - TCN75 (or LM75) thermometer |
icserny | 0:84ce5e2a89b1 | 8 | * |
icserny | 0:84ce5e2a89b1 | 9 | * Wiring scheme: |
icserny | 0:84ce5e2a89b1 | 10 | * |
icserny | 0:84ce5e2a89b1 | 11 | * TCN75 FRDM-25KLZ |
icserny | 0:84ce5e2a89b1 | 12 | * ------------------------- |
icserny | 0:84ce5e2a89b1 | 13 | * 1. SDA D14 (PTE0 alias I2C_SDA) |
icserny | 0:84ce5e2a89b1 | 14 | * 2. SCL D15 (PTE1 alias I2C_SCL) |
icserny | 0:84ce5e2a89b1 | 15 | * 3. ALERT -- (not used) |
icserny | 0:84ce5e2a89b1 | 16 | * 4. GND GND |
icserny | 0:84ce5e2a89b1 | 17 | * 5. A2 GND |
icserny | 0:84ce5e2a89b1 | 18 | * 6. A1 GND |
icserny | 0:84ce5e2a89b1 | 19 | * 7. A0 GND |
icserny | 0:84ce5e2a89b1 | 20 | * 8. VCC 3V3 |
icserny | 0:84ce5e2a89b1 | 21 | */ |
icserny | 0:84ce5e2a89b1 | 22 | |
icserny | 0:84ce5e2a89b1 | 23 | #include "mbed.h" |
icserny | 0:84ce5e2a89b1 | 24 | I2C i2c(I2C_SDA, I2C_SCL); // Arduino compatible pins |
icserny | 0:84ce5e2a89b1 | 25 | const int addr = 0x90; // TCN75 adress: 0x48<<1 |
icserny | 0:84ce5e2a89b1 | 26 | |
icserny | 0:84ce5e2a89b1 | 27 | int main() |
icserny | 0:84ce5e2a89b1 | 28 | { |
icserny | 0:84ce5e2a89b1 | 29 | char cmd[2]; // data buffer |
icserny | 0:84ce5e2a89b1 | 30 | printf("\r\nTCN75 I2C thermometer\r\n"); |
icserny | 0:84ce5e2a89b1 | 31 | cmd[0] = 0x01; // Pointer to CONFIG register |
icserny | 0:84ce5e2a89b1 | 32 | cmd[1] = 0x00; // Data for CONFOG register (Normal operation, comparator mode) |
icserny | 0:84ce5e2a89b1 | 33 | i2c.write(addr, cmd, 2); // Send Address/comman and two bytes |
icserny | 0:84ce5e2a89b1 | 34 | while (1) { |
icserny | 0:84ce5e2a89b1 | 35 | wait(1); |
icserny | 0:84ce5e2a89b1 | 36 | cmd[0] = 0x00; // Pointer to TEMP register |
icserny | 0:84ce5e2a89b1 | 37 | i2c.write(addr, cmd, 1); // Write adress/command byte, then register address |
icserny | 0:84ce5e2a89b1 | 38 | i2c.read(addr, cmd, 2); // Read 2 bytes from TEMP register |
icserny | 0:84ce5e2a89b1 | 39 | float temp = cmd[0]<<8|cmd[1]; |
icserny | 0:84ce5e2a89b1 | 40 | printf("Temperatue = %.2f C\r\n", temp/256); |
icserny | 0:84ce5e2a89b1 | 41 | } |
icserny | 0:84ce5e2a89b1 | 42 | } |