Dupuy Bruno / DS1621

Dependents:   S3_DS1621

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ds1621.cpp Source File

ds1621.cpp

00001 /***********************************************************
00002 Author: Dupuy Bruno
00003 Date: 5 mars 2001
00004 Version: beta
00005 ************************************************************/
00006 #include "ds1621.h"
00007 
00008 DS1621::DS1621(I2C* interface, uint8_t address) {
00009     i2c = interface;
00010 }
00011 
00012 float DS1621::read(uint8_t address) { // Return degrees C (-55 to +125)
00013     if (dbx) printf("DS1621::read DS1621 address=0x%X\n\r",address);
00014     float temperature = 0.0;
00015     uint16_t temp16;
00016     char temp8[2];
00017     int8_t data;
00018     if (i2c) {
00019         address &= 0x0E;
00020         data = 0xAA; // Read Temperature [AAh]
00021         i2c->write(DS1621_Write|address,(char *)&data,sizeof(data));
00022         i2c->read(DS1621_Read|address, temp8 ,sizeof(temp8), false);
00023         // Format temperature
00024         temp16 = temp8[0];
00025         temp16 = temp16 << 1;
00026         temp16 = temp16 + (temp8[1] >> 7);
00027         if ((temp16 & 0x100) == 0) { // +
00028             temperature = ((float) temp16 / 2);
00029         } else {  // -
00030             temp16 = ~temp16 & 0xFF;
00031             temperature = ((float) temp16 / 2);
00032             temperature = -1.0 * temperature;
00033         }
00034         if (dbx) printf("DS1621::read temp8[0]=0x%X, temp8[1]=0x%X, temp16=0x%X\n\r",temp8[0],temp8[1],temp16);
00035     } else {
00036         if (dbx) printf("DS1621::read interface I2C is not initialized\n\r");
00037     }
00038     return temperature;
00039 }
00040 
00041 void DS1621::init(uint8_t address) {
00042     if (dbx) printf("DS1621::init DS1621 address=0x%X\n\r",address);
00043     address &= 0x0E;
00044     char data;
00045     data = 0xEE; // Start Convert T [EEh]
00046     i2c->write(DS1621_Write|address,(char *)&data,sizeof(data));
00047 }