Simple DS1621 temperature acquisition.

Dependents:   S3_DS1621

ds1621.cpp

Committer:
dupuyb
Date:
2011-03-05
Revision:
0:d1667b4536dd
Child:
1:bef707045d41

File content as of revision 0:d1667b4536dd:

/***********************************************************
Author: Dupuy Bruno
Date: 5 mars 2001
Version: beta
************************************************************/
#include "ds1621.h"

DS1621::DS1621(I2C* interface, uint8_t address, Serial* pc) {
    if (dbx) dbx->printf("Constructor DS1621\n\r");
    i2c = interface;
    dbx = pc;
}

float DS1621::getTemp(uint8_t address) { // Return degrees C (-55 to +125)
    if (dbx) dbx->printf("DS1621::getTemp DS1621 address=0x%X\n\r",address);
    float temperature;
    uint16_t temp16;
    char temp8[2];
    int8_t data;
    address &= 0x0E;
    data = 0xEE;
    i2c->write(DS1621_Write|address,(char *)&data,sizeof(data));
    data = 0xAA;
    i2c->write(DS1621_Write|address,(char *)&data,sizeof(data));
    i2c->read(DS1621_Read|address, temp8 ,sizeof(temp8), false);
    // Format temperature
    temp16 = temp8[0];
    temp16 = temp16 << 1;
    temp16 = temp16 + (temp8[1] >> 7);
    if ((temp16 & 0x100) == 0) { // +
        temperature = ((float) temp16 / 2);
    } else {  // -
        temp16 = ~temp16 & 0xFF;
        temperature = ((float) temp16 / 2);
        temperature = -1.0 * temperature;
    }
    if (dbx)  dbx->printf("temp8[0]=0x%X, temp8[1]=0x%X, temp16=0x%X\n\r",temp8[0],temp8[1],temp16);
    return temperature;
}