You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
3.5 KiB

extern "C" {
#include <Wire/Wire.h>
}
#include "ds1624.h"
DS1624::DS1624()
{
#ifdef WIRE_LIB_SCAN_MOD
/*if (!Wire.isStarted())*/ Wire.begin();
#else
Wire.begin();
#endif
}
DS1624 TEMP = DS1624();
void DS1624::Init(void)
{
Wire.beginTransmission(DS1624_ADDR);
Wire.send(DS1624_CONFIG_ADDR);
Wire.send(DS1624_CONFIGS);
Wire.endTransmission();
delay(5);
if (DS1624_CONFIGS == DS1624_CONFIG_CONT) {
startConversion();
}
}
void DS1624::startConversion(void)
{
Wire.beginTransmission(DS1624_ADDR);
Wire.send(DS1624_SMPL_START);
Wire.endTransmission();
#ifdef DS1624_HANDLE_BOOT_DELAY
delay(1000);
#endif
}
void DS1624::stopConversion(void)
{
Wire.beginTransmission(DS1624_ADDR);
Wire.send(DS1624_SMPL_STOP);
Wire.endTransmission();
}
uint16_t DS1624::readTemp(void)
{
uint16_t res = 0;
Wire.beginTransmission(DS1624_ADDR);
Wire.send(DS1624_READ_TEMP);
Wire.endTransmission();
delay(5);
Wire.requestFrom(DS1624_ADDR, 2);
while (!Wire.available());
res = (uint8_t) (Wire.receive())<<8;
while (!Wire.available());
res |= (uint8_t) Wire.receive();
return res;
}
#ifdef DS1624_USE_EEPROM
void DS1624::writeData(uint8_t *data)
{
writeData(data, DS1624_EEPROM_SIZE, 0);
}
void DS1624::writeData(uint8_t *data, uint8_t dataLength)
{
writeData(data, dataLength, 0);
}
void DS1624::writeData(uint8_t *data, uint8_t dataLength, uint8_t memoryPosition)
{
// We can only write one page at a time!
if (memoryPosition%DS1624_EEPROM_PAGE != 0) return;
// Make sure we don't overlap
if (dataLength == 0 || dataLength > (DS1624_EEPROM_SIZE - memoryPosition)) return;
uint8_t buffer[DS1624_EEPROM_PAGE];
uint8_t pagePosition = 0;
uint8_t pageSize = 0;
uint8_t buffLen = 0;
stopConversion();
for (uint16_t pageStart = 0; pageStart < dataLength; pageStart+=DS1624_EEPROM_PAGE)
{
if (pageStart+DS1624_EEPROM_PAGE < dataLength || dataLength%DS1624_EEPROM_PAGE == 0)
pageSize = DS1624_EEPROM_PAGE;
else
pageSize = dataLength%DS1624_EEPROM_PAGE;
pagePosition = 0;
while (pagePosition < pageSize)
{
buffer[pagePosition] = data[pageStart+pagePosition];
pagePosition++;
}
writePage(buffer, pageSize, (memoryPosition + pageStart));
}
startConversion();
return;
}
void DS1624::writePage(uint8_t *data, uint8_t dataLength, uint8_t pagePosition)
{
if (dataLength == 0 || dataLength > DS1624_EEPROM_PAGE) return;
Wire.beginTransmission(DS1624_ADDR);
Wire.send(DS1624_ACCESS_MEM);
Wire.send(pagePosition);
Wire.send(data, dataLength);
Wire.endTransmission();
// Wait for the DS1624 to finish writing the eeprom
delay(50);
}
void DS1624::readData(uint8_t *data)
{
return readData(data, DS1624_EEPROM_SIZE, 0);
}
void DS1624::readData(uint8_t *data, uint8_t dataLength)
{
return readData(data, dataLength, 0);
}
void DS1624::readData(uint8_t *data, uint8_t dataLength, uint8_t memoryPosition)
{
// Make sure we don't overlap
if (dataLength == 0 || dataLength > (DS1624_EEPROM_SIZE - memoryPosition)) return;
uint8_t pagePosition;
for (uint16_t pagePosition = 0; pagePosition < dataLength; pagePosition++)
{
// Send the access memory command along with the starting position
Wire.beginTransmission(DS1624_ADDR);
Wire.send(DS1624_ACCESS_MEM);
Wire.send((int)(memoryPosition + pagePosition));
Wire.endTransmission();
// Wait for the DS1624 to set the position pointer
delay(5);
Wire.requestFrom(DS1624_ADDR, 1);
while (!Wire.available());
data[pagePosition] = Wire.receive();
}
return;
}
#endif