I am using ic acs71020 for measuring current, voltage , active power etc but didn't getting correct result. i am sharing arduino programming. if anyone solve it i shall be thankful.
#include
#include
#define kNOERROR 0
#define kREADERROR 1
#define kWRITEERROR 2
const uint16_t SPIChipSelectPin = 23;
const uint16_t I2CAddress = 127;
const uint16_t ProtocolSelectPin = 16;
const uint32_t WRITE = 0x00;
const uint32_t READ = 0x80;
const uint32_t COMMAND_MASK = 0x80;
const uint32_t ADDRESS_MASK = 0x7F;
unsigned long nextTime;
bool ledOn = false;
bool UseI2C = true;
// Setup the demo board.
void setup()
{
// Initialize serial
Serial.begin(115200);
// Turn on the pullup so the determination of communication protocol can be made.
pinMode(ProtocolSelectPin, INPUT_PULLUP);
delay(50); // Wait for the pullup to take affect
UseI2C = (digitalRead(ProtocolSelectPin) == HIGH);
if (UseI2C)
{
// Initialize I2C
Wire.begin();
Wire.setClock(400000);
}
else
{
// Initialize SPI
SPI.begin();
// Setup chip select
pinMode(SPIChipSelectPin, OUTPUT);
digitalWrite(SPIChipSelectPin, HIGH);
// SPI.setSCK(14);
// Make sure all of the SPI pins are
// ready by doing a read
uint32_t unused;
Read(0x0, unused);
}
Write(0x2F, 0x4F70656E); // Unlock device
// If the Arduino has built in USB, keep the next line
// in to wait for the Serial to initialize
while (!Serial);
if (UseI2C)
{
Serial.println("Using I2C version of ACS71020");
}
else
{
Serial.println("Using SPI version of ACS71020");
}
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
nextTime = millis();
}
/*
* Every 500 milliseconds, read the ACS71020 and print out the values
*/
void loop()
{
uint32_t vrms_irms;
uint32_t vrms;
uint32_t irms;
uint32_t pactive;
uint32_t paparent;
uint32_t pimag;
uint32_t pfactor;
uint32_t numptsout;
uint32_t vrmsavgonesec_irmsavgonesec;
uint32_t vrmsavgonesec;
uint32_t irmsavgonesec;
uint32_t vrmsavgonemin_irmsavgonemin;
uint32_t vrmsavgonemin;
uint32_t irmsavgonemin;
uint32_t pactavgonesec;
uint32_t pactavgonemin;
uint32_t vcodes;
uint32_t icodes;
uint32_t pinstant;
uint32_t flags;
// Every 1/2 second, toggle the state of the LED and read the ACS71020
if (nextTime > 16) & 0x7FFF;
// Serial.printf("irms = %ul\n", irms);
Serial.print("irms = ");
Serial.print(irms);
Serial.print("\n " );
pactive = pactive & 0x1FFFF;
//Serial.printf("pactive = %dl\n", pactive);
Serial.print("pactive = ");
Serial.print(pactive);
Serial.print("\n " );
/* Serial.printf("vrms = %ul\n", vrms);
irms = (vrms_irms >> 16) & 0x7FFF;
Serial.printf("irms = %ul\n", irms);
pactive = pactive & 0x1FFFF;
Serial.printf("pactive = %dl\n", pactive);
paparent = paparent & 0xFFFF;
Serial.printf("paparent = %ul\n", paparent);
pimag = pimag & 0x1FFFF;
Serial.printf("pimag = %ul\n", pimag);
pfactor = pfactor & 0x7FF;
Serial.printf("pfactor = %dl\n", pfactor);
numptsout = numptsout & 0x1FF;
Serial.printf("numptsout = %ul\n", numptsout);
vrmsavgonesec = vrmsavgonesec_irmsavgonesec & 0x7FFF;
Serial.printf("vrmsavgonesec = %ul\n", vrmsavgonesec);
irmsavgonesec = (vrmsavgonesec_irmsavgonesec >> 16) & 0x7FFF;
Serial.printf("irmsavgonesec = %ul\n", irmsavgonesec);
vrmsavgonemin = vrmsavgonemin_irmsavgonemin & 0x7FFF;
Serial.printf("vrmsavgonemin = %ul\n", vrmsavgonemin);
irmsavgonemin = (vrmsavgonemin_irmsavgonemin >> 16) & 0x7FFF;
Serial.printf("irmsavgonemin = %ul\n", irmsavgonemin);
pactavgonesec = pactavgonesec & 0x1FFFF;
Serial.printf("pactavgonesec = %ul\n", pactavgonesec);
pactavgonemin = pactavgonemin & 0x1FFFF;
Serial.printf("pactavgonemin = %ul\n", pactavgonemin);
vcodes = vcodes & 0x1FFFF;
Serial.printf("vcodes = %ul\n", vcodes);
icodes = icodes & 0x1FFFF;
Serial.printf("icodes = %ul\n", icodes);
Serial.printf("pinstant = %ul\n", pinstant);
Serial.print("pospf = ");
Serial.println((flags >> 6) & 0x1);
Serial.print("posangle = ");
Serial.println((flags >> 5) & 0x1);
Serial.print("undervoltage = ");
Serial.println((flags >> 4) & 0x1);
Serial.print("overvoltage = ");
Serial.println((flags >> 3) & 0x1);
Serial.print("faultlatched = ");
Serial.println((flags >> 2) & 0x1);
Serial.print("faultout = ");
Serial.println((flags >> 1) & 0x1);
Serial.print("vzerocrossout = ");
Serial.println((flags >> 0) & 0x1);*/
Serial.println();
if (ledOn)
{
digitalWrite(13, LOW);
ledOn = false;
}
else
{
digitalWrite(13, HIGH);
ledOn = true;
}
nextTime = millis() + 500L;
}
}
/*
* Read a register
*
* address - the address to be written
* value - the value that was read
* returns - the error (0 otherwise)
*/
uint16_t Read(uint8_t address, uint32_t& value)
{
uint16_t results = kNOERROR;
if (UseI2C)
{
Wire.beginTransmission(127);
Wire.write(address);
results = Wire.endTransmission();
if (results == kNOERROR)
{
Wire.requestFrom(127, 4u);
value = Wire.read(); // receive a byte as character
value |= Wire.read() << 8; // receive a byte as character
value |= Wire.read() << 16; // receive a byte as character
value |= Wire.read() << 24; // receive a byte as character
}
}
else
{
SPI.beginTransaction(SPISettings(1000000, LSBFIRST, SPI_MODE3));
// Combine the register address and the command into one byte
uint8_t command = (address & ADDRESS_MASK) | READ;
// take the chip select low to select the device
digitalWrite(SPIChipSelectPin, LOW);
// send the device the register you want to read
SPI.transfer(command);
SPI.transfer(0);
SPI.transfer(0);
SPI.transfer(0);
SPI.transfer(0);
digitalWrite(SPIChipSelectPin, HIGH);
delayMicroseconds(4);
digitalWrite(SPIChipSelectPin, LOW);
// send the command again to read the contents
SPI.transfer(command);
value = (uint32_t)SPI.transfer(0);
value |= (uint32_t)SPI.transfer(0) << 8;
value |= (uint32_t)SPI.transfer(0) << 16;
value |= (uint32_t)SPI.transfer(0) <> 8);
Wire.write(value >> 16);
Wire.write(value >> 24);
results = Wire.endTransmission();
}
else
{
SPI.beginTransaction(SPISettings(1000000, LSBFIRST, SPI_MODE3));
// Combine the register address and the command into one byte
uint8_t command = ((address & ADDRESS_MASK) | WRITE);
// take the chip select low to select the device:
digitalWrite(SPIChipSelectPin, LOW);
// Send the command then the value (least significant byte first)
SPI.transfer(command);
SPI.transfer((uint8_t)value);
SPI.transfer((uint8_t)(value >> 8));
SPI.transfer((uint8_t)(value >> 16));
SPI.transfer((uint8_t)(value >> 24));
// take the chip select high to de-select:
digitalWrite(SPIChipSelectPin, HIGH);
SPI.endTransaction();
}
if (address < 0x10)
{
delay(30); // If writing to EEPROM delay 30 ms
}
return results;
}