-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ef7f29
commit ad4990e
Showing
5 changed files
with
600 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,48 @@ | ||
ACS71020 default settings | ||
001000101100100000001110 | ||
|
||
# ACS71020 | ||
This library is heavily inspired from [SparkFun_ACS37800_Power_Monitor_Arduino_Library](https://github.com/sparkfun/SparkFun_ACS37800_Power_Monitor_Arduino_Library/tree/main) | ||
|
||
It is a minimal and readapted version. | ||
|
||
- **unlock()** -> Unlocks the device, necessary to write to EPROM. | ||
- **readRegister(uint8_t address, uint32_t &data)** -> Reads 32 bits of data from register through I2C. | ||
- **writeRegister(uint8_t address, uint32_t data)** -> Writes 32 bits to specified registry using I2C bus. | ||
- **readRMS(float &voltage, float ¤t)** -> Reads the voltage (V) and current (A) RMS values from registry 0x20. | ||
- **readRMSAvgSec(float &voltage_avg_sec, float ¤t_avg_sec)** -> Reads voltage (V) and current (I) RMS average values over the last second (register 0x26). | ||
- **readPowerActive(float &pActive)** -> Reads active power (W) from registry 0x21. | ||
- **readPowerActiveAvgSec(float &pActive_avg_sec)** -> Reads active power (W) average value over the last second. | ||
- **readPowerApparent(float &pApparent)** -> Reads apparent power (VA) from registry 0x22. | ||
- **readPowerReactive(float &pReactive)** -> Reads reactive power (VAR) from registry 0x23. | ||
- **readPowerFactor(float &pFactor)** -> Reads power factor from registry 0x24. | ||
|
||
### ACS71020 default settings | ||
0x0B (0x11E0F) | ||
qvo_fine: 15 | ||
sns_fine: 143 | ||
crs_sns: 0 | ||
iavgselen: 0 | ||
|
||
0x0C (0x0) | ||
rms_avg_1: 0 | ||
rms_avg_2: 0 | ||
|
||
0x0D (0x1FE000) | ||
pacc_trim: 0 | ||
ichan_del_en: 0 | ||
chan_del_sel: 16 | ||
fault: 255 | ||
fitdly: 0 | ||
halfcycle_en: 0 | ||
squarewave_en: 0 | ||
|
||
vevent_cycs: 0 | ||
vadc_rate_set: 0 | ||
overvreg: 0 | ||
undervreg: 0 | ||
delaycnt_sel: 0 | ||
|
||
0x1B | ||
00000000001000001000111000001111 | ||
0x00 0x20 0x8E 0x0F | ||
0x208E0F | ||
### Chips settings | ||
|Code|0x0B|0x0C|0x0D| | ||
|--|--|--|--| | ||
| 2239096K|0x22EC0F|0x1F40040|0x3FFFF81| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
#include "ACS71020.h" | ||
|
||
ACS71020::ACS71020() {} | ||
|
||
void ACS71020::begin(uint8_t address, TwoWire &wirePort) { | ||
_i2cAddress = address; | ||
_i2cPort = &wirePort; | ||
} | ||
|
||
ACS71020ERR ACS71020::unlock() { | ||
return writeRegister(0x2F, CUSTOMER_ACCESS_CODE); | ||
} | ||
|
||
ACS71020ERR ACS71020::readRegister(uint8_t address, uint32_t &data) { | ||
_i2cPort->beginTransmission(_i2cAddress); | ||
_i2cPort->write(address); | ||
uint8_t i2cResult = _i2cPort->endTransmission(); | ||
|
||
if (i2cResult != 0) | ||
return (ERR_I2C_ERROR); | ||
|
||
uint8_t toRead = _i2cPort->requestFrom(_i2cAddress, (uint8_t)4); | ||
if (toRead != 4) | ||
return (ERR_I2C_ERROR); | ||
|
||
data = Wire.read(); | ||
data |= Wire.read() << 8; | ||
data |= Wire.read() << 16; | ||
data |= Wire.read() << 24; | ||
|
||
return SUCCESS; | ||
} | ||
|
||
ACS71020ERR ACS71020::writeRegister(uint8_t address, uint32_t data) { | ||
_i2cPort->beginTransmission(_i2cAddress); | ||
_i2cPort->write(address); | ||
|
||
Wire.write(data); | ||
Wire.write(data >> 8); | ||
Wire.write(data >> 16); | ||
Wire.write(data >> 24); | ||
uint8_t i2cResult = _i2cPort->endTransmission(); | ||
|
||
if (i2cResult != 0) | ||
return (ERR_I2C_ERROR); | ||
|
||
return SUCCESS; | ||
} | ||
|
||
ACS71020ERR ACS71020::readRMS(float &voltage, float ¤t) { | ||
REGISTER_20_t store; | ||
ACS71020ERR error = readRegister(REGISTER_VOLATILE_20, store.data.all); | ||
|
||
if (error != SUCCESS) | ||
return error; | ||
|
||
voltage = convertVoltage((float)store.data.bits.vrms); | ||
|
||
current = convertCurrent((float)store.data.bits.irms); | ||
|
||
return SUCCESS; | ||
} | ||
|
||
ACS71020ERR ACS71020::readRMSAvgSec(float &voltage_avg_sec, float ¤t_avg_sec) { | ||
REGISTER_26_t store; | ||
ACS71020ERR error = readRegister(REGISTER_VOLATILE_26, store.data.all); | ||
|
||
if (error != SUCCESS) | ||
return error; | ||
|
||
voltage_avg_sec = convertVoltage((float)store.data.bits.vrmsavgonesec); | ||
|
||
current_avg_sec = convertCurrent((float)store.data.bits.irmsavgonesec); | ||
|
||
return SUCCESS; | ||
} | ||
|
||
ACS71020ERR ACS71020::readPowerActive(float &pActive) { | ||
REGISTER_21_t store; | ||
ACS71020ERR error = readRegister(REGISTER_VOLATILE_21, store.data.all); | ||
|
||
if (error != SUCCESS) | ||
return error; | ||
|
||
int value = store.data.bits.pactive; | ||
if (value & 0x10000) { | ||
value = -(~(value - 1) & 0x1FFFF); | ||
} | ||
|
||
pActive = convertPower((float)value); | ||
|
||
return SUCCESS; | ||
} | ||
|
||
ACS71020ERR ACS71020::readPowerActiveAvgSec(float &pActive_avg_sec) { | ||
REGISTER_28_t store; | ||
ACS71020ERR error = readRegister(REGISTER_VOLATILE_28, store.data.all); | ||
|
||
if (error != SUCCESS) | ||
return error; | ||
|
||
int value = store.data.bits.pactavgonesec; | ||
if (value & 0x10000) { | ||
value = -(~(value - 1) & 0x1FFFF); | ||
} | ||
|
||
pActive_avg_sec = convertPower((float)value); | ||
|
||
return SUCCESS; | ||
} | ||
|
||
ACS71020ERR ACS71020::readPowerApparent(float &pApparent) { | ||
REGISTER_22_t store; | ||
ACS71020ERR error = readRegister(REGISTER_VOLATILE_22, store.data.all); | ||
|
||
if (error != SUCCESS) | ||
return error; | ||
|
||
pApparent = convertPower((float)store.data.bits.papparent); | ||
|
||
return SUCCESS; | ||
} | ||
|
||
ACS71020ERR ACS71020::readPowerReactive(float &pReactive) { | ||
REGISTER_23_t store; | ||
ACS71020ERR error = readRegister(REGISTER_VOLATILE_23, store.data.all); | ||
|
||
if (error != SUCCESS) | ||
return error; | ||
|
||
pReactive = convertPower((float)store.data.bits.pimag); | ||
|
||
return SUCCESS; | ||
} | ||
|
||
ACS71020ERR ACS71020::readPowerFactor(float &pFactor) { | ||
REGISTER_24_t store; | ||
ACS71020ERR error = readRegister(REGISTER_VOLATILE_24, store.data.all); | ||
|
||
if (error != SUCCESS) | ||
return error; | ||
|
||
int value = store.data.bits.pfactor; | ||
if (value & 0x400) { | ||
value |= 0xF800; | ||
} | ||
|
||
pFactor = value / 0x200; | ||
|
||
return SUCCESS; | ||
} | ||
|
||
float ACS71020::convertVoltage(float voltage) { | ||
return(voltage / 0x8000) * _voltageSensingRange; | ||
} | ||
|
||
float ACS71020::convertCurrent(float current) { | ||
return(current / 0x4000) * _currentSensingRange; | ||
} | ||
|
||
float ACS71020::convertPower(float power) { | ||
return(power / 0x8000) * _voltageSensingRange * _currentSensingRange; | ||
} |
Oops, something went wrong.