| 1 | extern "C" { |
|---|
| 2 | #include <Wire/Wire.h> |
|---|
| 3 | } |
|---|
| 4 | |
|---|
| 5 | #include "ds1624.h" |
|---|
| 6 | |
|---|
| 7 | DS1624::DS1624() |
|---|
| 8 | { |
|---|
| 9 | #ifdef WIRE_LIB_SCAN_MOD |
|---|
| 10 | /*if (!Wire.isStarted())*/ Wire.begin(); |
|---|
| 11 | #else |
|---|
| 12 | Wire.begin(); |
|---|
| 13 | #endif |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | DS1624 TEMP = DS1624(); |
|---|
| 17 | |
|---|
| 18 | void DS1624::Init(void) |
|---|
| 19 | { |
|---|
| 20 | Wire.beginTransmission(DS1624_ADDR); |
|---|
| 21 | Wire.send(DS1624_CONFIG_ADDR); |
|---|
| 22 | Wire.send(DS1624_CONFIGS); |
|---|
| 23 | Wire.endTransmission(); |
|---|
| 24 | |
|---|
| 25 | delay(5); |
|---|
| 26 | |
|---|
| 27 | if (DS1624_CONFIGS == DS1624_CONFIG_CONT) { |
|---|
| 28 | startConversion(); |
|---|
| 29 | } |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | void DS1624::startConversion(void) |
|---|
| 33 | { |
|---|
| 34 | Wire.beginTransmission(DS1624_ADDR); |
|---|
| 35 | Wire.send(DS1624_SMPL_START); |
|---|
| 36 | Wire.endTransmission(); |
|---|
| 37 | |
|---|
| 38 | #ifdef DS1624_HANDLE_BOOT_DELAY |
|---|
| 39 | delay(1000); |
|---|
| 40 | #endif |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | void DS1624::stopConversion(void) |
|---|
| 44 | { |
|---|
| 45 | Wire.beginTransmission(DS1624_ADDR); |
|---|
| 46 | Wire.send(DS1624_SMPL_STOP); |
|---|
| 47 | Wire.endTransmission(); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | uint16_t DS1624::readTemp(void) |
|---|
| 51 | { |
|---|
| 52 | uint16_t res = 0; |
|---|
| 53 | Wire.beginTransmission(DS1624_ADDR); |
|---|
| 54 | Wire.send(DS1624_READ_TEMP); |
|---|
| 55 | Wire.endTransmission(); |
|---|
| 56 | |
|---|
| 57 | delay(5); |
|---|
| 58 | |
|---|
| 59 | Wire.requestFrom(DS1624_ADDR, 2); |
|---|
| 60 | |
|---|
| 61 | while (!Wire.available()); |
|---|
| 62 | |
|---|
| 63 | res = (uint8_t) (Wire.receive())<<8; |
|---|
| 64 | |
|---|
| 65 | while (!Wire.available()); |
|---|
| 66 | |
|---|
| 67 | res |= (uint8_t) Wire.receive(); |
|---|
| 68 | |
|---|
| 69 | return res; |
|---|
| 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 |
|---|