105 uint16_t cHelp = (~(*bytes)) & 0xFF;
115 cHelp += (~(*bytes)) & 0xFF;
125 else if (length == 1)
126 return sig ? (int8_t)(bytes[0])*lsb : bytes[0] * lsb;
127 else if (length == 2)
129 else if (length == 3)
131 else if (length == 4)
141 double ret = (*unsignedBytes);
147 ret = ret * 256.0 + (*unsignedBytes);
154 if (val < 0x10) Serial.print(
"0");
155 Serial.print(val, HEX);
159 for (uint16_t
i = 0x1000L;
i >= 0x10L;
i =
i >> 4)
160 if (val <
i) Serial.print(
"0");
161 Serial.print(val, HEX);
165 for (uint32_t
i = 0x10000000L;
i >= 0x10L;
i =
i >> 4)
166 if (val <
i) Serial.print(
"0");
167 Serial.print(val, HEX);
178 if (length >
sizeof(int64_t))
182 int64_t int64Val = int64_t(value / lsb);
185 Serial.print(F(
"int64Val=0x"));
188 Serial.println(F(
"bytes:"));
192 for (int8_t
i = length - 1;
i >= 0;
i--)
194 bytes[
i] = int64Val & 0xFF;
198 Serial.print(F(
":"));
201 Serial.print(F(
" (MSB)"));
202 else if (i == length - 1)
203 Serial.print(F(
" (LSB)"));
211 void LTC2947_DoubleToBytes_Test()
213 Serial.print(F(
"LSB:"));
214 while (!Serial.available());
216 Serial.println(lsb, 8);
218 Serial.print(F(
"LSBrshift:"));
219 while (!Serial.available());
221 Serial.println(shift);
225 Serial.println(lsb, 16);
227 Serial.print(F(
"VAL:"));
228 while (!Serial.available());
230 Serial.println(val, 8);
232 Serial.print(F(
"VALlshift:"));
233 while (!Serial.available());
235 Serial.println(shift);
239 Serial.println(val, 16);
250 uint8_t *unsignedBytes = (uint8_t *)malloc(length);
252 memcpy(unsignedBytes, signedBytes, length);
262 return sign ? -absDouble : absDouble;
365 SPI.transfer(address);
367 for (i = 0; i < length; i++)
368 SPI.transfer(values[i]);
381 SPI.transfer(address);
383 for (i = 0; i < length; i++)
384 values[i] = SPI.transfer(0x00);
395 SPI.transfer(address);
407 SPI.transfer(address);
408 value[0] = SPI.transfer(0x00);
433 return ret != 0 ? 1 : 0;
482 return ret != 0 ? 1 : 0;
553 *svlAddr = ((*svlAddr) >> 1) & 0x7F;
570 unsigned long wakeupStart = millis(), wakeupTime;
576 wakeupTime = millis() - wakeupStart;
582 if (wakeupTime > 200)
593 uint8_t currentPageCtrl;
#define bitMaskSetClr(value, bitMask, setNotClr)
#define LTC2947_I2C_ADDR_LL
#define LTC2947_ARA_ERROR
general I2C communication error
int8_t LTC2947_I2CWrBlock(uint8_t slvAddr, uint8_t regAddr, uint8_t length, uint8_t *values)
write byte array via I2C interface
int8_t LTC2947_I2CWrByte(uint8_t slvAddr, uint8_t regAddr, uint8_t value)
write single byte via I2C
#define LTC2947_SPI_READ_CMD
void LTC2947_Read_I_P_V_TEMP_VCC(float *I, float *P, float *V, float *TEMP, float *VCC)
Reads current (I), power (P), voltage (V), temperature (TEMP) and supply voltage (VCC) from the devic...
uint32_t LTC2947_4BytesToUInt32(byte *bytes)
Converts an array of 4 bytes to 32-bit unsigned integer.
static int8_t LTC2947_RD_BYTES(uint8_t REG_ADDR, uint8_t LENGTH, uint8_t *BYTES)
read multiple bytes via I2C/SPI
uint8_t LTC2947_Ara(uint8_t *svlAddr)
Sends the Alert Response address to the I2C bus and reads the response If two or more devices on the ...
void LTC2947_InitSPI()
Initializes the LTC2947 library for SPI mode operation.
boolean LTC2947_Abs(uint8_t *bytes, uint8_t length)
Calculates the absolute value of a signed value with arbitrary number of bytes.
#define output_high(pin)
Set "pin" high.
void LTC2947_Read_C_E_TB(boolean accuSet1, double *C, double *E, double *TB)
Reads charge (C), energy (E) and time (TB) from the device.
#define LTC2947_BM_PGCTL_PAGE
#define LTC2947_BM_GPIOSTATCL_GPI
int32_t LTC2947_3BytesToInt32(byte *bytes)
converts an array of 3 bytes to 32-bit signed integer
void LTC2947_Read_Abs_C_E_TB(boolean accuSet1, double *C, boolean *signC, double *E, boolean *signE, double *TB)
Reads charge (C), energy (E) and time (TB) from the device.
Header File for Linduino Libraries and Demo Code.
#define LTC2947_BM_GPIOSTATCL_GPO
#define LTC2947_ARA_OTHER_RESPONSE
got ARA response from some other I2C slave
void i2c_stop()
Write stop bit to the hardware I2C port.
int8_t LTC2947_I2CRdBlock(uint8_t slvAddr, uint8_t regAddr, uint8_t length, uint8_t *values)
read multiple bytes via I2C
void LTC2947_SetPageSelect(boolean page)
write LTC2947's page control register to selected one of two memory pages
#define LTC2947_ARA_NO_RESPONSE
got NO ARA response
uint16_t LTC2947_2BytesToUInt16(byte *bytes)
Converts an array of 2 bytes to 16-bit unsigned integer.
void LTC2947_SerialPrint8hex(uint8_t val)
Prints a 8-bit value in 2-character hexadecimal format with left padded zeros.
int8_t i2c_start()
Write start bit to the hardware I2C port.
double LTC2947_SignedBytesToDouble(uint8_t *signedBytes, uint8_t length, double lsb)
Converts a signed value of arbitrary number of bytes to a floating point value with the scaling facto...
union LT_union_int32_4bytes data
#define WITH_NACK
Use with i2c_read(WITH_NACK) to read without an acknowledge.
void LTC2947_SerialPrint32hex(uint32_t val)
Prints a 32-bit value in 8-character hexadecimal format with left padded zeros.
boolean LTC2947_SPI_Mode_Enabled
controlled by LTC2947_InitI2C / LTC2947_InitSPI to switch between I2C / SPI mode
int8_t i2c_write(uint8_t data)
Send a data byte to hardware I2C port.
#define output_low(pin)
Set "pin" low.
int16_t LTC2947_2BytesToInt16(byte *bytes)
converts an array of 2 bytes to 16-bit signed integer
boolean LTC2947_GPIO_Read()
reads the current GPIO pin state Make sure LTC2947's page 0 is selected before calling this function...
#define bitMaskSetChk(value, bitMask)
void LTC2947_InitI2C(uint8_t slvAddr)
Initializes the LTC2947 library for I2C mode operation and configures the slave address see defines L...
#define LTC2947_OFFS_TEMP
LT_SPI: Routines to communicate with ATmega328P's hardware SPI port.
uint8_t LTC2947_I2C_Slave_Addr
set by LTC2947_InitI2C to set slave address for I2C operation
#define LTC2947_ALERT_RESP_ADDR
The general alert response address.
#define LTC2947_LSB_VDVCC
void LTC2947_GPIO_SetPinState(uint8_t val)
Sets the level of the output driver on the GPIO pin This has only an effect if the output driver is e...
#define LTC2947_REG_PGCTL
LT_I2C: Routines to communicate with ATmega328P's hardware I2C port.
int8_t LTC2947_SpiRdBlock(uint8_t address, uint8_t length, uint8_t *values)
read array of bytes from the SPI interface
#define bitMaskClrChk(value, bitMask)
int8_t i2c_repeated_start()
Write a repeat start bit to the hardware I2C port.
#define LTC2947_REG_OPCTL
int16_t LTC2947_wake_up()
Wake up LTC2947 from shutdown mode and measure the wakeup time.
void LTC2947_SerialPrint16hex(uint16_t val)
Prints a 16-bit value in 4-character hexadecimal format with left padded zeros.
int32_t LTC2947_4BytesToInt32(byte *bytes)
Converts an array of 4 bytes to 32-bit signed integer.
void LTC2947_SerialPrint64hex(uint64_t uint64Val)
Prints a 64-bit value in 16-character hexadecimal format with left padded zeros.
#define LTC2947_ARA_LTC2947_RESPONSE
got ARA response from LTC2947
#define LTC2947_SPI_WRITE_CMD
int8_t LTC2947_I2CRdByte(uint8_t slvAddr, uint8_t regAddr, uint8_t *value)
read single byte via I2C
uint8_t i2c_read(int8_t ack)
Read a data byte from the hardware I2C port.
void LTC2947_GPIO_PinMode(uint8_t mode)
Enables/disables the output driver on the GPIO pin Make sure LTC2947's page 0 is selected before call...
void LTC2947_DoubleToBytes(double value, double lsb, uint8_t *bytes, uint8_t length)
Converts a floating point number that was scaled with a given LSB to an integer representation that w...
uint32_t LTC2947_3BytesToUInt32(byte *bytes)
converts an array of 3 bytes to 32-bit unsigned integer
LTC2947: LTC2947 a high-precision power and energy monitor with an internal sense resistor supporting...
static int8_t LTC2947_WR_BYTE(uint8_t REG_ADDR, uint8_t VALUE)
write single byte via I2C
int8_t LTC2947_SpiWrByte(uint8_t address, uint8_t value)
write single byte to SPI interface
#define WITH_ACK
Use with i2c_read(WITH_ACK) to read with an acknowledge.
boolean LTC2947_GetCurrentPageSelect()
reads LTC2947's page control register to determine the currently selected memory page ...
double LTC2947_BytesToDouble(uint8_t *bytes, uint8_t length, boolean sig, double lsb)
Converts a signed or unsigned value of arbitrary number of bytes to a floating point number...
double LTC2947_UnsignedBytesToDouble(uint8_t *unsignedBytes, uint8_t length, double lsb)
Converts an unsigned value of arbitrary number of bytes to a floating point value with the scaling fa...
#define LTC2947_REG_GPIOSTATCL
#define LTC2947_BM_GPIOSTATCL_GPOEN
#define LTC2947_ARA_RESPONSE_WO_WR
got ARA response from any slave but wihtout the expected WR bit
static int8_t LTC2947_RD_BYTE(uint8_t REG_ADDR, uint8_t *RESULT)
read single byte via I2C/SPI
int8_t LTC2947_SpiWrBlock(uint8_t address, uint8_t length, uint8_t *values)
writes block (array) of bytes to the SPI interface
int8_t LTC2947_SpiRdByte(uint8_t address, uint8_t *value)
read single byte from SPI interface