Simple DS1621 temperature acquisition.

Dependents:   S3_DS1621

Committer:
dupuyb
Date:
Sat Mar 05 20:35:01 2011 +0000
Revision:
0:d1667b4536dd
Child:
1:bef707045d41
Version beta

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dupuyb 0:d1667b4536dd 1 /***********************************************************
dupuyb 0:d1667b4536dd 2 Author: Dupuy Bruno
dupuyb 0:d1667b4536dd 3 Date: 5 mars 2001
dupuyb 0:d1667b4536dd 4 Version: beta
dupuyb 0:d1667b4536dd 5 ************************************************************/
dupuyb 0:d1667b4536dd 6 #include "ds1621.h"
dupuyb 0:d1667b4536dd 7
dupuyb 0:d1667b4536dd 8 DS1621::DS1621(I2C* interface, uint8_t address, Serial* pc) {
dupuyb 0:d1667b4536dd 9 if (dbx) dbx->printf("Constructor DS1621\n\r");
dupuyb 0:d1667b4536dd 10 i2c = interface;
dupuyb 0:d1667b4536dd 11 dbx = pc;
dupuyb 0:d1667b4536dd 12 }
dupuyb 0:d1667b4536dd 13
dupuyb 0:d1667b4536dd 14 float DS1621::getTemp(uint8_t address) { // Return degrees C (-55 to +125)
dupuyb 0:d1667b4536dd 15 if (dbx) dbx->printf("DS1621::getTemp DS1621 address=0x%X\n\r",address);
dupuyb 0:d1667b4536dd 16 float temperature;
dupuyb 0:d1667b4536dd 17 uint16_t temp16;
dupuyb 0:d1667b4536dd 18 char temp8[2];
dupuyb 0:d1667b4536dd 19 int8_t data;
dupuyb 0:d1667b4536dd 20 address &= 0x0E;
dupuyb 0:d1667b4536dd 21 data = 0xEE;
dupuyb 0:d1667b4536dd 22 i2c->write(DS1621_Write|address,(char *)&data,sizeof(data));
dupuyb 0:d1667b4536dd 23 data = 0xAA;
dupuyb 0:d1667b4536dd 24 i2c->write(DS1621_Write|address,(char *)&data,sizeof(data));
dupuyb 0:d1667b4536dd 25 i2c->read(DS1621_Read|address, temp8 ,sizeof(temp8), false);
dupuyb 0:d1667b4536dd 26 // Format temperature
dupuyb 0:d1667b4536dd 27 temp16 = temp8[0];
dupuyb 0:d1667b4536dd 28 temp16 = temp16 << 1;
dupuyb 0:d1667b4536dd 29 temp16 = temp16 + (temp8[1] >> 7);
dupuyb 0:d1667b4536dd 30 if ((temp16 & 0x100) == 0) { // +
dupuyb 0:d1667b4536dd 31 temperature = ((float) temp16 / 2);
dupuyb 0:d1667b4536dd 32 } else { // -
dupuyb 0:d1667b4536dd 33 temp16 = ~temp16 & 0xFF;
dupuyb 0:d1667b4536dd 34 temperature = ((float) temp16 / 2);
dupuyb 0:d1667b4536dd 35 temperature = -1.0 * temperature;
dupuyb 0:d1667b4536dd 36 }
dupuyb 0:d1667b4536dd 37 if (dbx) dbx->printf("temp8[0]=0x%X, temp8[1]=0x%X, temp16=0x%X\n\r",temp8[0],temp8[1],temp16);
dupuyb 0:d1667b4536dd 38 return temperature;
dupuyb 0:d1667b4536dd 39 }