Simple DS1621 temperature acquisition.

Dependents:   S3_DS1621

ds1621.cpp

Committer:
dupuyb
Date:
2011-03-07
Revision:
1:bef707045d41
Parent:
0:d1667b4536dd
Child:
2:3160631567f2

File content as of revision 1:bef707045d41:

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

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

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 = 0.0;
    uint16_t temp16;
    char temp8[2];
    int8_t data;
    if (i2c) {
        address &= 0x0E;
        data = 0xEE; // Start Convert T [EEh]
        i2c->write(DS1621_Write|address,(char *)&data,sizeof(data));
        data = 0xAA; // Read Temperature [AAh]
        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("DS1621::getTemp temp8[0]=0x%X, temp8[1]=0x%X, temp16=0x%X\n\r",temp8[0],temp8[1],temp16);
    } else {
       if (dbx)  dbx->printf("DS1621::getTemp interfave I2C is not initialized\n\r");
    }
    return temperature;
}