- Timestamp:
- 10/19/07 01:35:46 (15 months ago)
- Location:
- ds1624
- Files:
-
- 2 modified
-
ds1624.cpp (modified) (3 diffs)
-
ds1624.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
ds1624/ds1624.cpp
r7 r12 19 19 { 20 20 Wire.beginTransmission(DS1624_ADDR); 21 Wire.send(DS1624_CONFIG );21 Wire.send(DS1624_CONFIG_ADDR); 22 22 Wire.send(DS1624_CONFIGS); 23 23 Wire.endTransmission(); 24 24 25 delay( 10);25 delay(5); 26 26 27 Wire.beginTransmission(DS1624_ADDR); 28 Wire.send(DS1624_CONFIG); 29 Wire.endTransmission(); 30 31 Wire.requestFrom(DS1624_ADDR, 1); 32 27 if (DS1624_CONFIGS == DS1624_CONFIG_CONT) { 28 startConversion(); 29 } 30 } 31 32 void DS1624::startConversion(void) 33 { 33 34 Wire.beginTransmission(DS1624_ADDR); 34 35 Wire.send(DS1624_SMPL_START); 35 36 Wire.endTransmission(); 37 38 #ifdef DS1624_HANDLE_BOOT_DELAY 39 delay(1000); 40 #endif 41 } 36 42 37 #ifdef DS1624_HANDLE_BOOT_DELAY 38 delay(1500); 39 #endif 43 void DS1624::stopConversion(void) 44 { 45 Wire.beginTransmission(DS1624_ADDR); 46 Wire.send(DS1624_SMPL_STOP); 47 Wire.endTransmission(); 40 48 } 41 49 … … 46 54 Wire.send(DS1624_READ_TEMP); 47 55 Wire.endTransmission(); 56 57 delay(5); 48 58 49 59 Wire.requestFrom(DS1624_ADDR, 2); … … 59 69 return res; 60 70 } 71 72 #ifdef DS1624_USE_EEPROM 73 void DS1624::writeData(uint8_t *data) 74 { 75 writeData(data, DS1624_EEPROM_SIZE, 0); 76 } 77 78 void DS1624::writeData(uint8_t *data, uint8_t dataLength) 79 { 80 writeData(data, dataLength, 0); 81 } 82 83 void DS1624::writeData(uint8_t *data, uint8_t dataLength, uint8_t memoryPosition) 84 { 85 // We can only write one page at a time! 86 if (memoryPosition%DS1624_EEPROM_PAGE != 0) return; 87 88 // Make sure we don't overlap 89 if (dataLength == 0 || dataLength > (DS1624_EEPROM_SIZE - memoryPosition)) return; 90 91 uint8_t buffer[DS1624_EEPROM_PAGE]; 92 uint8_t pagePosition = 0; 93 uint8_t pageSize = 0; 94 uint8_t buffLen = 0; 95 96 stopConversion(); 97 98 for (uint16_t pageStart = 0; pageStart < dataLength; pageStart+=DS1624_EEPROM_PAGE) 99 { 100 if (pageStart+DS1624_EEPROM_PAGE < dataLength || dataLength%DS1624_EEPROM_PAGE == 0) 101 pageSize = DS1624_EEPROM_PAGE; 102 else 103 pageSize = dataLength%DS1624_EEPROM_PAGE; 104 105 pagePosition = 0; 106 while (pagePosition < pageSize) 107 { 108 buffer[pagePosition] = data[pageStart+pagePosition]; 109 110 pagePosition++; 111 } 112 113 writePage(buffer, pageSize, (memoryPosition + pageStart)); 114 115 } 116 117 startConversion(); 118 119 return; 120 } 121 122 void DS1624::writePage(uint8_t *data, uint8_t dataLength, uint8_t pagePosition) 123 { 124 if (dataLength == 0 || dataLength > DS1624_EEPROM_PAGE) return; 125 126 Wire.beginTransmission(DS1624_ADDR); 127 Wire.send(DS1624_ACCESS_MEM); 128 Wire.send(pagePosition); 129 130 Wire.send(data, dataLength); 131 132 Wire.endTransmission(); 133 134 // Wait for the DS1624 to finish writing the eeprom 135 delay(50); 136 } 137 138 void DS1624::readData(uint8_t *data) 139 { 140 return readData(data, DS1624_EEPROM_SIZE, 0); 141 } 142 143 void DS1624::readData(uint8_t *data, uint8_t dataLength) 144 { 145 return readData(data, dataLength, 0); 146 } 147 148 void DS1624::readData(uint8_t *data, uint8_t dataLength, uint8_t memoryPosition) 149 { 150 // Make sure we don't overlap 151 if (dataLength == 0 || dataLength > (DS1624_EEPROM_SIZE - memoryPosition)) return; 152 153 uint8_t pagePosition; 154 for (uint16_t pagePosition = 0; pagePosition < dataLength; pagePosition++) 155 { 156 // Send the access memory command along with the starting position 157 Wire.beginTransmission(DS1624_ADDR); 158 Wire.send(DS1624_ACCESS_MEM); 159 Wire.send((int)(memoryPosition + pagePosition)); 160 Wire.endTransmission(); 161 162 // Wait for the DS1624 to set the position pointer 163 delay(5); 164 165 Wire.requestFrom(DS1624_ADDR, 1); 166 167 while (!Wire.available()); 168 169 data[pagePosition] = Wire.receive(); 170 } 171 172 return; 173 } 174 #endif -
ds1624/ds1624.h
r7 r12 9 9 10 10 /** 11 * Registers and bit mask definition11 * Sampling configurations 12 12 **/ 13 #define DS1624_CONFIG 0xAC14 #define DS1624_CONFIG_DONE B1000000015 13 #define DS1624_CONFIG_1SHOT B00000001 16 14 #define DS1624_CONFIG_CONT B00000000 17 15 18 // Select your config here!16 // Select either 1shot or continous themometer mode 19 17 #define DS1624_CONFIGS DS1624_CONFIG_CONT 20 18 19 // DS1624 Commands 20 #define DS1624_CONFIG_ADDR 0xAC 21 21 #define DS1624_READ_TEMP 0xAA 22 22 #define DS1624_SMPL_START 0xEE … … 24 24 #define DS1624_ACCESS_MEM 0x17 25 25 26 // Bit masks 27 #define DS1624_CONFIG_DONE B10000000 28 29 // Size of the EEPROM memory in bytes 30 #define DS1624_EEPROM_SIZE 255 /* There are actually 256 bytes, but the twi functions only support reading 255 at a time!*/ 31 #define DS1624_EEPROM_PAGE 8 32 26 33 class DS1624 27 34 { 28 35 public: 29 36 DS1624(); 37 38 /** 39 * Init: runs the initializationr outine 40 **/ 30 41 void Init(void); 42 43 /** 44 * readTemp: reads the temperature and returns 45 * a 16 byte integer in which the 2 highest bytes 46 * are used as (int8_t)degrees and the 2 lowest 47 * are used (uint8_t)decimals 48 **/ 31 49 uint16_t readTemp(void); 50 51 /** 52 * Start conversion, used at startup in 53 * continous mode, or before a read 54 * in 1shot mode 55 **/ 56 void startConversion(void); 57 58 /** 59 * stopConversion: stops the temperature acquisition 60 **/ 61 void stopConversion(void); 62 /** 63 * EEPROM Support functions 64 **/ 65 #ifdef DS1624_USE_EEPROM 66 /** 67 * writeData: writes data to the eeprom 68 * pass it an array of uint8_t containing the values to write, 69 * the start position (must be %8 == 0) and the length. 70 * The two last can be omnited. 71 **/ 72 void writeData(uint8_t *, uint8_t, uint8_t); 73 void writeData(uint8_t *, uint8_t); 74 void writeData(uint8_t *); 75 76 /** 77 * writeData: writes data to the eeprom 78 * pass it an array of uint8_t to store the result, 79 * the start position and the length. The two last can be omnited 80 **/ 81 void readData(uint8_t *, uint8_t, uint8_t); 82 void readData(uint8_t *, uint8_t); 83 void readData(uint8_t *); 84 #endif 32 85 private: 86 #ifdef DS1624_USE_EEPROM 87 /** 88 * writePage: writes a single page (or less) to the eeprom 89 * pass it an array of uint8_t to store the result, 90 * the start position and the length. 91 **/ 92 void writePage(uint8_t *, uint8_t, uint8_t); 93 #endif 33 94 34 95 };