Simple DS1621 temperature acquisition.
ds1621.cpp@3:d6310fd6df8e, 2011-03-21 (annotated)
- Committer:
- dupuyb
- Date:
- Mon Mar 21 16:51:22 2011 +0000
- Revision:
- 3:d6310fd6df8e
- Parent:
- 2:3160631567f2
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:d6310fd6df8e | 8 | DS1621::DS1621(I2C* interface, uint8_t address) { |
dupuyb | 0:d1667b4536dd | 9 | i2c = interface; |
dupuyb | 0:d1667b4536dd | 10 | } |
dupuyb | 0:d1667b4536dd | 11 | |
dupuyb | 3:d6310fd6df8e | 12 | float DS1621::read(uint8_t address) { // Return degrees C (-55 to +125) |
dupuyb | 3:d6310fd6df8e | 13 | if (dbx) printf("DS1621::read DS1621 address=0x%X\n\r",address); |
dupuyb | 1:bef707045d41 | 14 | float temperature = 0.0; |
dupuyb | 0:d1667b4536dd | 15 | uint16_t temp16; |
dupuyb | 0:d1667b4536dd | 16 | char temp8[2]; |
dupuyb | 0:d1667b4536dd | 17 | int8_t data; |
dupuyb | 1:bef707045d41 | 18 | if (i2c) { |
dupuyb | 1:bef707045d41 | 19 | address &= 0x0E; |
dupuyb | 1:bef707045d41 | 20 | data = 0xAA; // Read Temperature [AAh] |
dupuyb | 1:bef707045d41 | 21 | i2c->write(DS1621_Write|address,(char *)&data,sizeof(data)); |
dupuyb | 1:bef707045d41 | 22 | i2c->read(DS1621_Read|address, temp8 ,sizeof(temp8), false); |
dupuyb | 1:bef707045d41 | 23 | // Format temperature |
dupuyb | 1:bef707045d41 | 24 | temp16 = temp8[0]; |
dupuyb | 1:bef707045d41 | 25 | temp16 = temp16 << 1; |
dupuyb | 1:bef707045d41 | 26 | temp16 = temp16 + (temp8[1] >> 7); |
dupuyb | 1:bef707045d41 | 27 | if ((temp16 & 0x100) == 0) { // + |
dupuyb | 1:bef707045d41 | 28 | temperature = ((float) temp16 / 2); |
dupuyb | 1:bef707045d41 | 29 | } else { // - |
dupuyb | 1:bef707045d41 | 30 | temp16 = ~temp16 & 0xFF; |
dupuyb | 1:bef707045d41 | 31 | temperature = ((float) temp16 / 2); |
dupuyb | 1:bef707045d41 | 32 | temperature = -1.0 * temperature; |
dupuyb | 1:bef707045d41 | 33 | } |
dupuyb | 3:d6310fd6df8e | 34 | if (dbx) printf("DS1621::read temp8[0]=0x%X, temp8[1]=0x%X, temp16=0x%X\n\r",temp8[0],temp8[1],temp16); |
dupuyb | 1:bef707045d41 | 35 | } else { |
dupuyb | 3:d6310fd6df8e | 36 | if (dbx) printf("DS1621::read interface I2C is not initialized\n\r"); |
dupuyb | 0:d1667b4536dd | 37 | } |
dupuyb | 0:d1667b4536dd | 38 | return temperature; |
dupuyb | 3:d6310fd6df8e | 39 | } |
dupuyb | 3:d6310fd6df8e | 40 | |
dupuyb | 3:d6310fd6df8e | 41 | void DS1621::init(uint8_t address) { |
dupuyb | 3:d6310fd6df8e | 42 | if (dbx) printf("DS1621::init DS1621 address=0x%X\n\r",address); |
dupuyb | 3:d6310fd6df8e | 43 | address &= 0x0E; |
dupuyb | 3:d6310fd6df8e | 44 | char data; |
dupuyb | 3:d6310fd6df8e | 45 | data = 0xEE; // Start Convert T [EEh] |
dupuyb | 3:d6310fd6df8e | 46 | i2c->write(DS1621_Write|address,(char *)&data,sizeof(data)); |
dupuyb | 0:d1667b4536dd | 47 | } |