Simple DS1621 temperature acquisition.

Dependents:   S3_DS1621

Committer:
dupuyb
Date:
Mon Mar 07 08:12:27 2011 +0000
Revision:
2:3160631567f2
Parent:
1:bef707045d41
Child:
3:d6310fd6df8e
version 1.0

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 i2c = interface;
dupuyb 0:d1667b4536dd 10 dbx = pc;
dupuyb 1:bef707045d41 11 if (dbx) dbx->printf("Constructor DS1621\n\r");
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 1:bef707045d41 16 float temperature = 0.0;
dupuyb 0:d1667b4536dd 17 uint16_t temp16;
dupuyb 0:d1667b4536dd 18 char temp8[2];
dupuyb 0:d1667b4536dd 19 int8_t data;
dupuyb 1:bef707045d41 20 if (i2c) {
dupuyb 1:bef707045d41 21 address &= 0x0E;
dupuyb 1:bef707045d41 22 data = 0xEE; // Start Convert T [EEh]
dupuyb 1:bef707045d41 23 i2c->write(DS1621_Write|address,(char *)&data,sizeof(data));
dupuyb 1:bef707045d41 24 data = 0xAA; // Read Temperature [AAh]
dupuyb 1:bef707045d41 25 i2c->write(DS1621_Write|address,(char *)&data,sizeof(data));
dupuyb 1:bef707045d41 26 i2c->read(DS1621_Read|address, temp8 ,sizeof(temp8), false);
dupuyb 1:bef707045d41 27 // Format temperature
dupuyb 1:bef707045d41 28 temp16 = temp8[0];
dupuyb 1:bef707045d41 29 temp16 = temp16 << 1;
dupuyb 1:bef707045d41 30 temp16 = temp16 + (temp8[1] >> 7);
dupuyb 1:bef707045d41 31 if ((temp16 & 0x100) == 0) { // +
dupuyb 1:bef707045d41 32 temperature = ((float) temp16 / 2);
dupuyb 1:bef707045d41 33 } else { // -
dupuyb 1:bef707045d41 34 temp16 = ~temp16 & 0xFF;
dupuyb 1:bef707045d41 35 temperature = ((float) temp16 / 2);
dupuyb 1:bef707045d41 36 temperature = -1.0 * temperature;
dupuyb 1:bef707045d41 37 }
dupuyb 1:bef707045d41 38 if (dbx) dbx->printf("DS1621::getTemp temp8[0]=0x%X, temp8[1]=0x%X, temp16=0x%X\n\r",temp8[0],temp8[1],temp16);
dupuyb 1:bef707045d41 39 } else {
dupuyb 2:3160631567f2 40 if (dbx) dbx->printf("DS1621::getTemp interface I2C is not initialized\n\r");
dupuyb 0:d1667b4536dd 41 }
dupuyb 0:d1667b4536dd 42 return temperature;
dupuyb 0:d1667b4536dd 43 }