Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ebf1e9d

Browse files
committedJan 21, 2021
Initial commit
0 parents  commit ebf1e9d

File tree

8 files changed

+810
-0
lines changed

8 files changed

+810
-0
lines changed
 

‎ISSUE_TEMPLATE.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
### Subject of the issue
2+
Describe your issue here.
3+
4+
### Your workbench
5+
* What platform are you using?
6+
* What version of the device are you using? Is there a firmware version?
7+
* How is the device wired to your platform?
8+
* How is everything being powered?
9+
* Are there any additional details that may help us help you?
10+
11+
### Steps to reproduce
12+
Tell us how to reproduce this issue. Please post stripped down example code demonstrating your issue to a gist.
13+
14+
### Expected behaviour
15+
Tell us what should happen
16+
17+
### Actual behaviour
18+
Tell us what happens instead

‎LICENSE.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
SparkFun License Information
2+
============================
3+
4+
SparkFun uses two different licenses for our files — one for hardware and one for code.
5+
6+
Hardware
7+
---------
8+
9+
**SparkFun hardware is released under [Creative Commons Share-alike 4.0 International](http://creativecommons.org/licenses/by-sa/4.0/).**
10+
11+
Note: This is a human-readable summary of (and not a substitute for) the [license](http://creativecommons.org/licenses/by-sa/4.0/legalcode).
12+
13+
You are free to:
14+
15+
Share — copy and redistribute the material in any medium or format
16+
Adapt — remix, transform, and build upon the material
17+
for any purpose, even commercially.
18+
The licensor cannot revoke these freedoms as long as you follow the license terms.
19+
Under the following terms:
20+
21+
Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
22+
ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
23+
No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
24+
Notices:
25+
26+
You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation.
27+
No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material.
28+
29+
30+
Code
31+
--------
32+
33+
**SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).**
34+
35+
The MIT License (MIT)
36+
37+
Copyright (c) 2020 SparkFun Electronics
38+
39+
Permission is hereby granted, free of charge, to any person obtaining a copy
40+
of this software and associated documentation files (the "Software"), to deal
41+
in the Software without restriction, including without limitation the rights
42+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43+
copies of the Software, and to permit persons to whom the Software is
44+
furnished to do so, subject to the following conditions:
45+
46+
The above copyright notice and this permission notice shall be included in all
47+
copies or substantial portions of the Software.
48+
49+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
55+
SOFTWARE.

‎README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
SparkFun ACS37800 Power Monitor Arduino Library
2+
===========================================================
3+
[![SparkX ACS37800 Power Meter (Qwiic) (SPX-nnnnn)]()](https://www.sparkfun.com/products/nnnnn)
4+
5+
[*SparkX ACS37800 Power Meter (Qwiic) (SPX-nnnnn)*](https://www.sparkfun.com/products/nnnnn)
6+
7+
The Allegro MicroSystems ACS37800 power monitoring IC greatly simplifies the addition of power monitoring to many powered systems.
8+
9+
Allegro’s Hall-effect-based, galvanically isolated current sensing technology achieves reinforced isolation ratings in a small PCB footprint.
10+
These features enable isolated current sensing without expensive Rogowski coils, oversized current transformers, isolated operational amplifiers,
11+
or the power loss of shunt resistors.
12+
13+
The ACS37800 power monitoring IC offers key power measurement parameters that can easily be accessed through its digital interface. Dedicated and
14+
configurable I/O pins for voltage/current zero crossing, undervoltage and overvoltage reporting, and fast overcurrent fault detection are available.
15+
User configuration of the IC is available through on-chip EEPROM.
16+
17+
Although the ACS37800 is available with both I<sup>2</sup>C and SPI interfaces, this library currently only supports communication over I2C.
18+
19+
SparkFun labored with love to create this code. Feel like supporting open source hardware?
20+
Buy a [board](https://www.sparkfun.com/products/nnnnn) from SparkFun!
21+
22+
Repository Contents
23+
-------------------
24+
25+
* **/examples** - Example sketches for the library (.ino). Run these from the Arduino IDE.
26+
* **/src** - Source files for the library (.cpp, .h).
27+
* **keywords.txt** - Keywords from this library that will be highlighted in the Arduino IDE.
28+
* **library.properties** - General library properties for the Arduino package manager.
29+
30+
Documentation
31+
--------------
32+
33+
* **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library.
34+
* **[Product Repository](https://github.com/sparkfunX/Qwiic_Power_Meter-ACS37800)** - Main repository (including hardware files)
35+
36+
Products that use this library
37+
--------------
38+
* [SPX-nnnnn](https://www.sparkfun.com/products/nnnnn)- SparkX Version
39+
40+
License Information
41+
-------------------
42+
43+
This product is _**open source**_!
44+
45+
Please use, reuse, and modify these files as you see fit. Please maintain attribution to SparkFun Electronics and release anything derivative under the same license.
46+
47+
Distributed as-is; no warranty is given.
48+
49+
- Your friends at SparkFun.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Library for the Allegro MicroSystems ACS37800 power monitor IC
3+
By: Paul Clark
4+
SparkFun Electronics
5+
Date: January 21st, 2021
6+
License: please see LICENSE.md for details
7+
8+
Feel like supporting our work? Buy a board from SparkFun!
9+
https://www.sparkfun.com/products/nnnnn
10+
*/
11+
12+
#include "SparkFun_ACS37800_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_ACS37800
13+
#include <Wire.h>
14+
15+
#define OLD_ADDRESS 0x61 //This is the address the ACS37800 is currently using
16+
#define NEW_ADDRESS 0x62 //This is the address we are changing to
17+
18+
ACS37800 mySensor; //Create an object of the ACS37800 class
19+
20+
void setup()
21+
{
22+
Serial.begin(115200);
23+
Serial.println(F("ACS37800 Example"));
24+
25+
Wire.begin();
26+
27+
//mySensor.enableDebugging(); // Uncomment this line to print useful debug messages to Serial
28+
29+
//Initialize sensor
30+
if (mySensor.begin(OLD_ADDRESS) == false)
31+
{
32+
Serial.print(F("ACS37800 not detected at 0x"));
33+
Serial.print(OLD_ADDRESS);
34+
Serial.println(F(". Check connections. Freezing..."));
35+
while (1)
36+
; // Do nothing more
37+
}
38+
39+
Serial.print(F("Changing the address to 0x"));
40+
Serial.println(NEW_ADDRESS);
41+
42+
//Initialize sensor
43+
if (mySensor.setI2Caddress(NEW_ADDRESS) == ACS37800_SUCCESS)
44+
{
45+
Serial.println(F("Address changed! Please power cycle the sensor to use the new address. Freezing..."));
46+
}
47+
else
48+
{
49+
Serial.println(F("Changing the address failed! Please try again. Freezing..."));
50+
}
51+
}
52+
53+
void loop()
54+
{
55+
//Nothing to do here
56+
}

‎keywords.txt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#######################################
2+
# Syntax Coloring Map
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
ACS37800 KEYWORD1
10+
ACS37800ERR KEYWORD1
11+
ACS37800_REGISTER_0B_t KEYWORD1
12+
ACS37800_REGISTER_0C_t KEYWORD1
13+
ACS37800_REGISTER_0D_t KEYWORD1
14+
ACS37800_REGISTER_0E_t KEYWORD1
15+
ACS37800_REGISTER_0F_t KEYWORD1
16+
ACS37800_REGISTER_20_t KEYWORD1
17+
ACS37800_REGISTER_21_t KEYWORD1
18+
ACS37800_REGISTER_22_t KEYWORD1
19+
ACS37800_REGISTER_25_t KEYWORD1
20+
ACS37800_REGISTER_26_t KEYWORD1
21+
ACS37800_REGISTER_27_t KEYWORD1
22+
ACS37800_REGISTER_28_t KEYWORD1
23+
ACS37800_REGISTER_29_t KEYWORD1
24+
ACS37800_REGISTER_2A_t KEYWORD1
25+
ACS37800_REGISTER_2C_t KEYWORD1
26+
ACS37800_REGISTER_2D_t KEYWORD1
27+
28+
#######################################
29+
# Methods and Functions (KEYWORD2)
30+
#######################################
31+
32+
begin KEYWORD2
33+
enableDebugging KEYWORD2
34+
readRegister KEYWORD2
35+
writeRegister KEYWORD2
36+
setI2Caddress KEYWORD2
37+
38+
#######################################
39+
# Constants (LITERAL1)
40+
#######################################
41+
42+
ACS37800_SUCCESS LITERAL1
43+
ACS37800_ERR_I2C_ERROR LITERAL1
44+
45+
ACS37800_CRS_SNS_1X LITERAL1
46+
ACS37800_CRS_SNS_2X LITERAL1
47+
ACS37800_CRS_SNS_3X LITERAL1
48+
ACS37800_CRS_SNS_3POINT5X LITERAL1
49+
ACS37800_CRS_SNS_4X LITERAL1
50+
ACS37800_CRS_SNS_4POINT5X LITERAL1
51+
ACS37800_CRS_SNS_5POINT5X LITERAL1
52+
ACS37800_CRS_SNS_8X LITERAL1
53+
54+
ACS37800_FLTDLY_0000 LITERAL1
55+
ACS37800_FLTDLY_0475 LITERAL1
56+
ACS37800_FLTDLY_0925 LITERAL1
57+
ACS37800_FLTDLY_1375 LITERAL1
58+
ACS37800_FLTDLY_1850 LITERAL1
59+
ACS37800_FLTDLY_2325 LITERAL1
60+
ACS37800_FLTDLY_2775 LITERAL1
61+
62+
ACS37800_DIO0_FUNC_ZERO_CROSSING LITERAL1
63+
ACS37800_DIO0_FUNC_OVERVOLTAGE LITERAL1
64+
ACS37800_DIO0_FUNC_UNDERVOLTAGE LITERAL1
65+
ACS37800_DIO0_FUNC_OV_OR_UV LITERAL1
66+
67+
ACS37800_DIO1_FUNC_OVERCURRENT LITERAL1
68+
ACS37800_DIO1_FUNC_UNDERVOLTAGE LITERAL1
69+
ACS37800_DIO1_FUNC_OVERVOLTAGE LITERAL1
70+
ACS37800_DIO1_FUNC_OV_OR_UV_OR_OCF_LAT LITERAL1
71+
72+
ACS37800_EEPROM_ECC_NO_ERROR LITERAL1
73+
ACS37800_EEPROM_ECC_ERROR_CORRECTED LITERAL1
74+
ACS37800_EEPROM_ECC_ERROR_UNCORRECTABLE LITERAL1
75+
ACS37800_EEPROM_ECC_NO_MEANING LITERAL1

‎library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=SparkFun ACS37800 Power Monitor Arduino Library
2+
version=1.0.0
3+
author=SparkFun Electronics
4+
maintainer=SparkFun Electronics
5+
sentence=Library for the Allegro MicroSystems ACS37800 power monitor IC
6+
paragraph=The Allegro MicroSystems ACS37800 power monitoring IC greatly simplifies the addition of power monitoring to many powered systems. This library supports the I2C versions like the ACS37800KMABTR-030B3-I2C found on the SparkX Qwiic Power Meter.
7+
category=Sensors
8+
url=https://github.com/sparkfun/SparkFun_ACS37800_Power_Monitor_Arduino_Library
9+
architectures=*
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
This is a library written for the I2C versions of the Allegro MicroSystems ACS37800 power monitor IC
3+
By Paul Clark @ SparkFun Electronics, January 21st, 2021
4+
5+
6+
https://github.com/sparkfun/SparkFun_ACS37800_Power_Monitor_Arduino_Library
7+
8+
Development environment specifics:
9+
Arduino IDE 1.8.13
10+
11+
SparkFun labored with love to create this code. Feel like supporting open
12+
source hardware? Buy a board from SparkFun!
13+
https://www.sparkfun.com/products/nnnnn
14+
15+
*/
16+
17+
#include "SparkFun_ACS37800_Arduino_Library.h"
18+
19+
//Constructor
20+
ACS37800::ACS37800()
21+
{
22+
}
23+
24+
//Start I2C communication using specified port
25+
//Returns true if successful or false if no sensor detected
26+
bool ACS37800::begin(uint8_t address, TwoWire &wirePort)
27+
{
28+
_ACS37800Address = address; //Grab which i2c address the user wants us to use
29+
_i2cPort = &wirePort; //Grab which port the user wants us to use
30+
31+
uint32_t accessCode; //Read and validate the access code
32+
ACS37800ERR error = readRegister(&accessCode, ACS37800_REGISTER_VOLATILE_2F);
33+
34+
if (_printDebug == true)
35+
{
36+
_debugPort->print(F("begin: readRegister error: "));
37+
_debugPort->println(error);
38+
_debugPort->print(F("begin: accessCode: 0x"));
39+
_debugPort->println(accessCode, HEX);
40+
}
41+
42+
return ((error == ACS37800_SUCCESS) && (accessCode == ACS37800_CUSTOMER_ACCESS_CODE));
43+
}
44+
45+
//Calling this function with nothing sets the debug port to Serial
46+
//You can also call it with other streams like Serial1, SerialUSB, etc.
47+
void ACS37800::enableDebugging(Stream &debugPort)
48+
{
49+
_debugPort = &debugPort;
50+
_printDebug = true;
51+
}
52+
53+
//Read a register's contents. Data is returned in data.
54+
ACS37800ERR ACS37800::readRegister(uint32_t *data, uint8_t address)
55+
{
56+
_i2cPort->beginTransmission(_ACS37800Address);
57+
_i2cPort->write(address); //Write the register address
58+
uint8_t i2cResult = _i2cPort->endTransmission(false); //Send restart. Don't release the bus.
59+
60+
if (i2cResult != 0)
61+
{
62+
if (_printDebug == true)
63+
{
64+
_debugPort->print(F("readRegister: endTransmission returned: "));
65+
_debugPort->println(i2cResult);
66+
}
67+
return (ACS37800_ERR_I2C_ERROR); // Bail
68+
}
69+
70+
//Read 4 bytes (32 bits)
71+
uint8_t toRead = _i2cPort->requestFrom(_ACS37800Address, (uint8_t)4);
72+
if (toRead != 4)
73+
{
74+
if (_printDebug == true)
75+
{
76+
_debugPort->print(F("readRegister: requestFrom returned: "));
77+
_debugPort->println(toRead);
78+
}
79+
return (ACS37800_ERR_I2C_ERROR); //Error out
80+
}
81+
82+
//Data is returned LSB first (little endian)
83+
uint32_t readData = _i2cPort->read(); //store LSB
84+
readData |= ((uint32_t)_i2cPort->read()) << 8;
85+
readData |= ((uint32_t)_i2cPort->read()) << 16;
86+
readData |= ((uint32_t)_i2cPort->read()) << 24; //store MSB
87+
88+
*data = readData; //Return the data
89+
return (ACS37800_SUCCESS);
90+
}
91+
92+
//Write data to the selected register
93+
ACS37800ERR ACS37800::writeRegister(uint32_t data, uint8_t address)
94+
{
95+
_i2cPort->beginTransmission(_ACS37800Address);
96+
_i2cPort->write(address); //Write the register address
97+
_i2cPort->write(data & 0xFF); //Write the data LSB first (little endian)
98+
_i2cPort->write((data >> 8) & 0xFF);
99+
_i2cPort->write((data >> 16) & 0xFF);
100+
_i2cPort->write((data >> 24) & 0xFF);
101+
uint8_t i2cResult = _i2cPort->endTransmission(); //Release the bus.
102+
103+
if (i2cResult != 0)
104+
{
105+
if (_printDebug == true)
106+
{
107+
_debugPort->print(F("writeRegister: endTransmission returned: "));
108+
_debugPort->println(i2cResult);
109+
}
110+
return (ACS37800_ERR_I2C_ERROR); // Bail
111+
}
112+
113+
return (ACS37800_SUCCESS);
114+
}
115+
116+
//Change the I2C address
117+
ACS37800ERR ACS37800::setI2Caddress(uint8_t newAddress)
118+
{
119+
ACS37800_REGISTER_0F_t store;
120+
ACS37800ERR error = readRegister(&store.data.all, ACS37800_REGISTER_EEPROM_0F); // Read register 0F
121+
122+
if (error != ACS37800_SUCCESS)
123+
{
124+
if (_printDebug == true)
125+
{
126+
_debugPort->print(F("setI2Caddress: readRegister (1) returned: "));
127+
_debugPort->println(error);
128+
}
129+
return (error); // Bail
130+
}
131+
132+
store.data.bits.i2c_slv_addr = newAddress; //Update the address
133+
store.data.bits.i2c_dis_slv_addr = 1; //Disable setting the address via the DIO pins
134+
135+
error = writeRegister(store.data.all, ACS37800_REGISTER_EEPROM_0F); // Write register 0F
136+
137+
if (error != ACS37800_SUCCESS)
138+
{
139+
if (_printDebug == true)
140+
{
141+
_debugPort->print(F("setI2Caddress: writeRegister returned: "));
142+
_debugPort->println(error);
143+
}
144+
return (error); // Bail
145+
}
146+
147+
error = readRegister(&store.data.all, ACS37800_REGISTER_EEPROM_0F); // Read register 0F
148+
149+
if (error != ACS37800_SUCCESS)
150+
{
151+
if (_printDebug == true)
152+
{
153+
_debugPort->print(F("setI2Caddress: readRegister (2) returned: "));
154+
_debugPort->println(error);
155+
}
156+
return (error); // Bail
157+
}
158+
159+
if (store.data.bits.i2c_slv_addr == newAddress) //Check the address was updated correctly
160+
{
161+
return (ACS37800_SUCCESS);
162+
}
163+
else
164+
{
165+
return (ACS37800_ERR_REGISTER_READ_MODIFY_WRITE_FAILURE);
166+
}
167+
}
Lines changed: 381 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,381 @@
1+
/*
2+
This is a library written for the I2C versions of the Allegro MicroSystems ACS37800 power monitor IC
3+
By Paul Clark @ SparkFun Electronics, January 21st, 2021
4+
5+
6+
https://github.com/sparkfun/SparkFun_ACS37800_Power_Monitor_Arduino_Library
7+
8+
Development environment specifics:
9+
Arduino IDE 1.8.13
10+
11+
SparkFun labored with love to create this code. Feel like supporting open
12+
source hardware? Buy a board from SparkFun!
13+
https://www.sparkfun.com/products/nnnnn
14+
15+
*/
16+
17+
#ifndef SparkFun_ACS37800_Arduino_Library_h
18+
#define SparkFun_ACS37800_Arduino_Library_h
19+
20+
#include "Arduino.h"
21+
#include <Wire.h>
22+
23+
//Default I2C Address
24+
//This is the address when DIO_0 and DIO_1 are 0V on start-up
25+
//The address can be configured in EEPROM too
26+
const uint8_t ACS37800_DEFAULT_I2C_ADDRESS = 0x61;
27+
28+
//Customer Access Code - stored in volatile register 0x2F
29+
const uint32_t ACS37800_CUSTOMER_ACCESS_CODE = 0x4F70656E;
30+
31+
//Default sense resistance for voltage measurement (kOhms)
32+
const float ACS37800_DEFAULT_SENSE_RES = 8.2;
33+
34+
//Error result
35+
typedef enum {
36+
ACS37800_SUCCESS = 0,
37+
ACS37800_ERR_I2C_ERROR,
38+
ACS37800_ERR_REGISTER_READ_MODIFY_WRITE_FAILURE
39+
} ACS37800ERR;
40+
41+
//EEPROM Registers
42+
const uint8_t ACS37800_REGISTER_EEPROM_0B = 0x0B;
43+
const uint8_t ACS37800_REGISTER_EEPROM_0C = 0x0C;
44+
const uint8_t ACS37800_REGISTER_EEPROM_0D = 0x0D;
45+
const uint8_t ACS37800_REGISTER_EEPROM_0E = 0x0E;
46+
const uint8_t ACS37800_REGISTER_EEPROM_0F = 0x0F;
47+
48+
//Shadow Registers
49+
//At power up, all shadow registers are loaded from EEPROM, including all configuration parameters.
50+
//The shadow registers can be written to in order to change the device behavior without having to
51+
//perform an EEPROM write. Any changes made in shadow memory are volatile and do not persist through a reset event.
52+
const uint8_t ACS37800_REGISTER_SHADOW_1B = 0x1B;
53+
const uint8_t ACS37800_REGISTER_SHADOW_1C = 0x1C;
54+
const uint8_t ACS37800_REGISTER_SHADOW_1D = 0x1D;
55+
const uint8_t ACS37800_REGISTER_SHADOW_1E = 0x1E;
56+
const uint8_t ACS37800_REGISTER_SHADOW_1F = 0x1F;
57+
58+
//Volatile Registers
59+
const uint8_t ACS37800_REGISTER_VOLATILE_20 = 0x20;
60+
const uint8_t ACS37800_REGISTER_VOLATILE_21 = 0x21;
61+
const uint8_t ACS37800_REGISTER_VOLATILE_22 = 0x22;
62+
const uint8_t ACS37800_REGISTER_VOLATILE_25 = 0x25;
63+
const uint8_t ACS37800_REGISTER_VOLATILE_26 = 0x26;
64+
const uint8_t ACS37800_REGISTER_VOLATILE_27 = 0x27;
65+
const uint8_t ACS37800_REGISTER_VOLATILE_28 = 0x28;
66+
const uint8_t ACS37800_REGISTER_VOLATILE_29 = 0x29;
67+
const uint8_t ACS37800_REGISTER_VOLATILE_2A = 0x2A;
68+
const uint8_t ACS37800_REGISTER_VOLATILE_2C = 0x2C;
69+
const uint8_t ACS37800_REGISTER_VOLATILE_2D = 0x2D;
70+
const uint8_t ACS37800_REGISTER_VOLATILE_2F = 0x2F;
71+
const uint8_t ACS37800_REGISTER_VOLATILE_30 = 0x30;
72+
73+
//EEPROM Registers : Bit Fields
74+
typedef struct
75+
{
76+
union
77+
{
78+
uint32_t all;
79+
struct
80+
{
81+
uint32_t qvo_fine : 9;
82+
uint32_t sns_fine : 10;
83+
uint32_t crs_sns : 3;
84+
uint32_t iavgselen : 1;
85+
uint32_t pavgselen : 1;
86+
uint32_t reserved : 2;
87+
uint32_t ECC : 6;
88+
} bits;
89+
} data;
90+
} ACS37800_REGISTER_0B_t;
91+
typedef struct
92+
{
93+
union
94+
{
95+
uint32_t all;
96+
struct
97+
{
98+
uint32_t rms_avg_1 : 7;
99+
uint32_t rms_avg_2 : 10;
100+
uint32_t vchan_offset_code : 8;
101+
uint32_t reserved : 1;
102+
uint32_t ECC : 6;
103+
} bits;
104+
} data;
105+
} ACS37800_REGISTER_0C_t;
106+
typedef struct
107+
{
108+
union
109+
{
110+
uint32_t all;
111+
struct
112+
{
113+
uint32_t reserved1 : 7;
114+
uint32_t ichan_del_en : 1;
115+
uint32_t reserved2 : 1;
116+
uint32_t chan_del_sel : 3;
117+
uint32_t reserved3 : 1;
118+
uint32_t fault : 8;
119+
uint32_t fltdly : 3;
120+
uint32_t reserved4 : 2;
121+
uint32_t ECC : 6;
122+
} bits;
123+
} data;
124+
} ACS37800_REGISTER_0D_t;
125+
typedef struct
126+
{
127+
union
128+
{
129+
uint32_t all;
130+
struct
131+
{
132+
uint32_t vevent_cycs : 6;
133+
uint32_t reserved1 : 2;
134+
uint32_t overvreg : 6;
135+
uint32_t undervreg : 6;
136+
uint32_t delaycnt_sel : 1;
137+
uint32_t halfcycle_en : 1;
138+
uint32_t squarewave_en : 1;
139+
uint32_t zerocrosschansel : 1;
140+
uint32_t zerocrossedgesel : 1;
141+
uint32_t reserved2 : 1;
142+
uint32_t ECC : 6;
143+
} bits;
144+
} data;
145+
} ACS37800_REGISTER_0E_t;
146+
typedef struct
147+
{
148+
union
149+
{
150+
uint32_t all;
151+
struct
152+
{
153+
uint32_t reserved1 : 2;
154+
uint32_t i2c_slv_addr : 7;
155+
uint32_t i2c_dis_slv_addr : 1;
156+
uint32_t dio_0_sel : 2;
157+
uint32_t dio_1_sel : 2;
158+
uint32_t n : 10;
159+
uint32_t bypass_n_en : 1;
160+
uint32_t reserved2 : 1;
161+
uint32_t ECC : 6;
162+
} bits;
163+
} data;
164+
} ACS37800_REGISTER_0F_t;
165+
typedef struct
166+
{
167+
union
168+
{
169+
uint32_t all;
170+
struct
171+
{
172+
uint32_t vrms : 16;
173+
uint32_t irms : 16;
174+
} bits;
175+
} data;
176+
} ACS37800_REGISTER_20_t;
177+
typedef struct
178+
{
179+
union
180+
{
181+
uint32_t all;
182+
struct
183+
{
184+
uint32_t pactive : 16;
185+
uint32_t pimag : 16;
186+
} bits;
187+
} data;
188+
} ACS37800_REGISTER_21_t;
189+
typedef struct
190+
{
191+
union
192+
{
193+
uint32_t all;
194+
struct
195+
{
196+
uint32_t papparent : 16;
197+
uint32_t pfactor : 11;
198+
uint32_t posangle : 1;
199+
uint32_t pospf : 1;
200+
} bits;
201+
} data;
202+
} ACS37800_REGISTER_22_t;
203+
typedef struct
204+
{
205+
union
206+
{
207+
uint32_t all;
208+
struct
209+
{
210+
uint32_t numptsout : 10;
211+
} bits;
212+
} data;
213+
} ACS37800_REGISTER_25_t;
214+
typedef struct
215+
{
216+
union
217+
{
218+
uint32_t all;
219+
struct
220+
{
221+
uint32_t vrmsavgonesec : 16;
222+
uint32_t irmsavgonesec : 16;
223+
} bits;
224+
} data;
225+
} ACS37800_REGISTER_26_t;
226+
typedef struct
227+
{
228+
union
229+
{
230+
uint32_t all;
231+
struct
232+
{
233+
uint32_t vrmsavgonemin : 16;
234+
uint32_t irmsavgonemin : 16;
235+
} bits;
236+
} data;
237+
} ACS37800_REGISTER_27_t;
238+
typedef struct
239+
{
240+
union
241+
{
242+
uint32_t all;
243+
struct
244+
{
245+
uint32_t pactavgonesec : 16;
246+
} bits;
247+
} data;
248+
} ACS37800_REGISTER_28_t;
249+
typedef struct
250+
{
251+
union
252+
{
253+
uint32_t all;
254+
struct
255+
{
256+
uint32_t pactavgonemin : 16;
257+
} bits;
258+
} data;
259+
} ACS37800_REGISTER_29_t;
260+
typedef struct
261+
{
262+
union
263+
{
264+
uint32_t all;
265+
struct
266+
{
267+
uint32_t vcodes : 16;
268+
uint32_t icodes : 16;
269+
} bits;
270+
} data;
271+
} ACS37800_REGISTER_2A_t;
272+
typedef struct
273+
{
274+
union
275+
{
276+
uint32_t all;
277+
struct
278+
{
279+
uint32_t pinstant : 16;
280+
} bits;
281+
} data;
282+
} ACS37800_REGISTER_2C_t;
283+
typedef struct
284+
{
285+
union
286+
{
287+
uint32_t all;
288+
struct
289+
{
290+
uint32_t vzerocrossout : 1;
291+
uint32_t faultout : 1;
292+
uint32_t faultlatched : 1;
293+
uint32_t overvoltage : 1;
294+
uint32_t undervoltage : 1;
295+
} bits;
296+
} data;
297+
} ACS37800_REGISTER_2D_t;
298+
299+
//Register Field Enums
300+
typedef enum
301+
{
302+
ACS37800_CRS_SNS_1X = 0,
303+
ACS37800_CRS_SNS_2X,
304+
ACS37800_CRS_SNS_3X,
305+
ACS37800_CRS_SNS_3POINT5X,
306+
ACS37800_CRS_SNS_4X,
307+
ACS37800_CRS_SNS_4POINT5X,
308+
ACS37800_CRS_SNS_5POINT5X,
309+
ACS37800_CRS_SNS_8X
310+
} ACS37800_CRS_SNS_e; //Coarse gain for the current channel
311+
typedef enum
312+
{
313+
ACS37800_FLTDLY_0000 = 0,
314+
ACS37800_FLTDLY_0475 = 2, //4.75 microseconds
315+
ACS37800_FLTDLY_0925, //9.25 microseconds
316+
ACS37800_FLTDLY_1375,
317+
ACS37800_FLTDLY_1850,
318+
ACS37800_FLTDLY_2325,
319+
ACS37800_FLTDLY_2775 //27.75 microseconds
320+
} ACS37800_FLTDLY_e; //Fault Delay
321+
typedef enum
322+
{
323+
ACS37800_DIO0_FUNC_ZERO_CROSSING = 0,
324+
ACS37800_DIO0_FUNC_OVERVOLTAGE,
325+
ACS37800_DIO0_FUNC_UNDERVOLTAGE,
326+
ACS37800_DIO0_FUNC_OV_OR_UV
327+
} ACS37800_DIO0_FUNC_e; //DIO_0 Function
328+
typedef enum
329+
{
330+
ACS37800_DIO1_FUNC_OVERCURRENT = 0,
331+
ACS37800_DIO1_FUNC_UNDERVOLTAGE,
332+
ACS37800_DIO1_FUNC_OVERVOLTAGE,
333+
ACS37800_DIO1_FUNC_OV_OR_UV_OR_OCF_LAT
334+
} ACS37800_DIO1_FUNC_e; //DIO_1 Function
335+
typedef enum
336+
{
337+
ACS37800_EEPROM_ECC_NO_ERROR = 0,
338+
ACS37800_EEPROM_ECC_ERROR_CORRECTED,
339+
ACS37800_EEPROM_ECC_ERROR_UNCORRECTABLE,
340+
ACS37800_EEPROM_ECC_NO_MEANING
341+
} ACS37800_EEPROM_ECC_e; //EEPROM ECC Errors
342+
343+
class ACS37800
344+
{
345+
// user-accessible "public" interface
346+
public:
347+
348+
//Default constructor
349+
ACS37800();
350+
351+
//Start I2C communication using specified address and port
352+
boolean begin(uint8_t address = ACS37800_DEFAULT_I2C_ADDRESS, TwoWire &wirePort = Wire); //If user doesn't specify then Wire will be used
353+
354+
//Debugging
355+
void enableDebugging(Stream &debugPort = Serial); //Turn on debug printing. If user doesn't specify then Serial will be used.
356+
357+
//Basic methods for accessing registers
358+
ACS37800ERR readRegister(uint32_t *data, uint8_t address);
359+
ACS37800ERR writeRegister(uint32_t data, uint8_t address);
360+
361+
//Configurable Settings
362+
363+
ACS37800ERR setI2Caddress(uint8_t newAddress);
364+
365+
private:
366+
//This stores the requested i2c port
367+
TwoWire * _i2cPort;
368+
369+
//Debug
370+
Stream *_debugPort; //The stream to send debug messages to if enabled. Usually Serial.
371+
boolean _printDebug = false; //Flag to print debugging variables
372+
373+
//ACS37800's I2C address
374+
uint8_t _ACS37800Address = ACS37800_DEFAULT_I2C_ADDRESS;
375+
376+
//The value of the sense resistor for voltage measurement in kOhms
377+
float _senseResistance = ACS37800_DEFAULT_SENSE_RES;
378+
379+
};
380+
381+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.