very simple program for reading temperature (on chip and external diode) from MAX1617 / TCM1617 using standard I2C

Dependencies:   mbed

Committer:
tiberiu21
Date:
Mon Apr 06 13:43:42 2015 +0000
Revision:
0:00e87008c034
initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tiberiu21 0:00e87008c034 1 /***********************************************************************
tiberiu21 0:00e87008c034 2 * Read temperature by using DS1617
tiberiu21 0:00e87008c034 3 * read external diode temp. between DXP/DXN pin
tiberiu21 0:00e87008c034 4 * read on-chip temperature
tiberiu21 0:00e87008c034 5 * if changed then serial out
tiberiu21 0:00e87008c034 6 * end
tiberiu21 0:00e87008c034 7 * tested on nucleo-F401
tiberiu21 0:00e87008c034 8 ************************************************************************/
tiberiu21 0:00e87008c034 9
tiberiu21 0:00e87008c034 10 #include "mbed.h"
tiberiu21 0:00e87008c034 11
tiberiu21 0:00e87008c034 12
tiberiu21 0:00e87008c034 13 #define MAX1617_RIT (0x00) // internal temperature register
tiberiu21 0:00e87008c034 14 #define MAX1617_RET (0x01) // external, (diode dxp-dxn)temperature register
tiberiu21 0:00e87008c034 15 #define MAX1617_ADDR (0x30) // MAX1617 address (ADD0=0 ADD1=0)
tiberiu21 0:00e87008c034 16 #define MAX1617_OSHT (0x0F) // MAX1617 one shot temperature read command, both channels
tiberiu21 0:00e87008c034 17
tiberiu21 0:00e87008c034 18
tiberiu21 0:00e87008c034 19 I2C i2c(I2C_SDA, I2C_SCL);
tiberiu21 0:00e87008c034 20
tiberiu21 0:00e87008c034 21 DigitalOut myled(LED1);
tiberiu21 0:00e87008c034 22
tiberiu21 0:00e87008c034 23 Serial pc(SERIAL_TX, SERIAL_RX);
tiberiu21 0:00e87008c034 24
tiberiu21 0:00e87008c034 25 int main()
tiberiu21 0:00e87008c034 26 {
tiberiu21 0:00e87008c034 27
tiberiu21 0:00e87008c034 28 char data_read_1[1]; //temp extread holder
tiberiu21 0:00e87008c034 29 char data_read_2[1]; //temp read holder
tiberiu21 0:00e87008c034 30 int rispold_1=0;
tiberiu21 0:00e87008c034 31 int rispold_2=0;
tiberiu21 0:00e87008c034 32
tiberiu21 0:00e87008c034 33
tiberiu21 0:00e87008c034 34 while (1) {
tiberiu21 0:00e87008c034 35 char command_one_shot[1] = {MAX1617_OSHT};
tiberiu21 0:00e87008c034 36 char command_ext_temp[1] = {MAX1617_RET};
tiberiu21 0:00e87008c034 37 char command_int_temp[1] = {MAX1617_RIT};
tiberiu21 0:00e87008c034 38 //I2C transactions
tiberiu21 0:00e87008c034 39 i2c.write(MAX1617_ADDR, command_one_shot , 1, 0); // no stop //one shot temp read
tiberiu21 0:00e87008c034 40 i2c.write(MAX1617_ADDR, command_ext_temp, 1, 1); // no stop //ext temp read
tiberiu21 0:00e87008c034 41 i2c.read(MAX1617_ADDR, data_read_1, 1, 0);
tiberiu21 0:00e87008c034 42 i2c.write(MAX1617_ADDR, command_int_temp, 1, 1); // no stop //int temp read
tiberiu21 0:00e87008c034 43 i2c.read(MAX1617_ADDR, data_read_2, 1, 0);
tiberiu21 0:00e87008c034 44
tiberiu21 0:00e87008c034 45 //
tiberiu21 0:00e87008c034 46 int risp1 = (int)data_read_1[0];
tiberiu21 0:00e87008c034 47 int risp2 = (int)data_read_2[0];
tiberiu21 0:00e87008c034 48 if ((risp1 != rispold_1)|(risp2 != rispold_2))
tiberiu21 0:00e87008c034 49 pc.printf("temp int = %d\n", risp2);
tiberiu21 0:00e87008c034 50 pc.printf("temp ext = %d\n", risp1);
tiberiu21 0:00e87008c034 51 rispold_1 = risp1;
tiberiu21 0:00e87008c034 52 rispold_2 = risp2;
tiberiu21 0:00e87008c034 53 myled = !myled;
tiberiu21 0:00e87008c034 54 wait(0.3);
tiberiu21 0:00e87008c034 55 }
tiberiu21 0:00e87008c034 56
tiberiu21 0:00e87008c034 57 }