Calibrate and get energy readings from ADE7758 IC from Analog Devices

Fork of ADE7758 by Emma

Currently this library can be used to calibrate and get VRMS, IRMS, active, and apparent energy. I havent worked on reactive energy measurement.

ade7758.cpp

Committer:
bonchenko
Date:
2015-04-10
Revision:
1:f5e8c8591449
Parent:
0:cf3dc3c5156b
Child:
2:ea36884772ae

File content as of revision 1:f5e8c8591449:

#include "mbed.h"
#include "ade7758.h"
#include "SWSPI.h"

// public
ADE7758::ADE7758(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName interrupt):
    _ADE7758SPI(mosi, miso, sclk), _ADESS(cs), _ADEINT(interrupt)
{
    _ADESS = 1;
}

void ADE7758::begin()
{
    enableChip();
    //normal mode
    _ADE7758SPI.format(8, 1); // pha 0, pol 1
    _ADE7758SPI.frequency(1000000);    
    write8bits(OPMODE, 0x04);
}

void ADE7758::CalibrateVI(uint8_t numSamples) 
{    
    write8bits(LCYCMODE, 0x38);
    write24bits(MASK, 0xE00);
    long AVRMSBuffer = 0, BVRMSBuffer = 0, CVRMSBuffer = 0;
    long AIRMSBuffer = 0, BIRMSBuffer = 0, CIRMSBuffer = 0;
    for (uint8_t i=0; i<numSamples; i++) {
        read24bits(RSTATUS);
        while ( _ADEINT != 0 ) { } // wait until INT go low
        AVRMSBuffer += VRMS(PHASE_A);
        BVRMSBuffer += VRMS(PHASE_B);
        CVRMSBuffer += VRMS(PHASE_C);
        AIRMSBuffer += IRMS(PHASE_A);
        BIRMSBuffer += IRMS(PHASE_B);
        CIRMSBuffer += IRMS(PHASE_C);
    }
    AVRMSCalib = AVRMSBuffer/numSamples;
    BVRMSCalib = BVRMSBuffer/numSamples;
    CVRMSCalib = CVRMSBuffer/numSamples;
    AIRMSCalib = AIRMSBuffer/numSamples;
    BIRMSCalib = BIRMSBuffer/numSamples;
    CIRMSCalib = CIRMSBuffer/numSamples;
}

int ADE7758::getWattHR(char phase)
{
    return read16bits(AWATTHR+phase);
}

int ADE7758::getVARHR(char phase)
{
    return read16bits(AVARHR+phase);
}

int ADE7758::getVAHR(char phase)
{
    return read16bits(AVAHR+phase);
}

int ADE7758::WattHR(char phase)
{
  return getWattHR(phase);
}

int ADE7758::VARHR(char phase)
{
  return getVARHR(phase);
}

int ADE7758::VAHR(char phase)
{
  return getVAHR(phase);
}

long ADE7758::VRMS(char phase)
{
  char i=0;
  long volts=0;
  getVRMS(phase);//Ignore first reading
  for(i=0;i<10;++i){
          volts+=getVRMS(phase);
          wait_us(50);
  }
  //average
  return volts/10;
}

long ADE7758::IRMS(char phase)
{
  char i=0;
  long current=0;
  getIRMS(phase);//Ignore first reading
  for(i=0;i<10;++i){
    current+=getIRMS(phase);
    wait_us(50);
  }
  //average
  return current/10;
}


float ADE7758::CalculateIRMS(char phase) {
    if ( phase == PHASE_A ) {
        return IRMS(phase) * 0.00000967;
    }
    else if ( phase == PHASE_B ) {
        return IRMS(phase) * 0.00000955;
    }    
    else if ( phase == PHASE_C ) {
        return IRMS(phase) * 0.00000958;
    }
}

float ADE7758::CalculateVRMS(char phase) {
    if ( phase == PHASE_A ) {
        return VRMS(phase) * 0.000158;
    }
    else if ( phase == PHASE_B ) {
        return VRMS(phase) * 0.000157;
    }
    else if ( phase == PHASE_C ) {
        return VRMS(phase) * 0.000156;
    }        
}

void accumulateEnergy()
{
    
}

long ADE7758::waveform(char phase,char source)
{
  return 1;
}

void ADE7758::powerOff()
{

}

void ADE7758::powerON()
{

}

void ADE7758::sleep()
{

}

void ADE7758::wakeUp()
{

}

long ADE7758::getInterruptStatus(void){
        return read24bits(STATUS);
}


long ADE7758::getResetInterruptStatus(void){
        return read24bits(RSTATUS);
}


int ADE7758::lineFreq(char phase){
    uint8_t mmode;
    mmode = read8bits(MMODE);
    write8bits(MMODE,(mmode&0x11111100 )| phase);
    wait_ms(10);
    return read16bits(FREQ);
}
// private

void ADE7758::enableChip()
{
    _ADESS = 0;
}

void ADE7758::disableChip()
{
    _ADESS = 1;
}

void ADE7758::write8bits(char reg, unsigned char data)
{
    enableChip();
        
    wait_ms(10);
    _ADE7758SPI.write(REG_WRITE(reg));
    wait_ms(2);

    _ADE7758SPI.write(data);

    wait_ms(1);
    
    disableChip();
}

void ADE7758::write16bits(char reg, unsigned int data)
{
    enableChip();
    
    wait_ms(10);
    _ADE7758SPI.write(REG_WRITE(reg));
    wait_ms(2);
    _ADE7758SPI.write((unsigned char)((data>>8)&0xFF));
    wait_ms(2);
    _ADE7758SPI.write((unsigned char)(data&0xFF));
    wait_ms(1);

    disableChip();
}

void ADE7758::write24bits(char reg, unsigned int data)
{
    enableChip();
    
    wait_ms(10);
    _ADE7758SPI.write(REG_WRITE(reg));
    wait_ms(2);
    _ADE7758SPI.write((unsigned char)((data>>16)&0xFF));
    wait_ms(2);
    _ADE7758SPI.write((unsigned char)((data>>8)&0xFF));
    wait_ms(2);
    _ADE7758SPI.write((unsigned char)(data&0xFF));
    wait_ms(1);

    disableChip();
}

unsigned char ADE7758::read8bits(char reg)
{
    enableChip();
    
    unsigned char ret;
    wait_ms(10);
    _ADE7758SPI.write(REG_READ(reg));
    wait_ms(2);
    ret=_ADE7758SPI.write(0x00);
    wait_ms(1);
    
    disableChip();

    return ret;
}

unsigned int ADE7758::read16bits(char reg)
{
    enableChip();
    unsigned int ret=0;
    unsigned char ret0=0;
    wait_ms(10);
    _ADE7758SPI.write(REG_READ(reg));
    wait_ms(2);
    ret=_ADE7758SPI.write(0x00);
    wait_ms(2);
    ret0=_ADE7758SPI.write(0x00);
    wait_ms(1);
    
    disableChip();
    ret= (ret<<8)|ret0;
    return ret;
}

unsigned long ADE7758::read24bits(char reg)
{
    enableChip();
    unsigned long ret=0;
    unsigned int ret1=0;
    unsigned char ret0=0;
    wait_ms(10);
    _ADE7758SPI.write(REG_READ(reg));
    wait_ms(2);
    ret=_ADE7758SPI.write(0x00);
    wait_ms(2);
    ret1=_ADE7758SPI.write(0x00);
    wait_ms(2);
    ret0=_ADE7758SPI.write(0x00);
    wait_ms(1);
    
    disableChip();
    ret= (ret<<16)|(ret1<<8)| ret0;
    return ret;
}

long ADE7758::getIRMS(char phase)
{
    return read24bits(AIRMS+phase);
}

long ADE7758::getVRMS(char phase)
{          
    return read24bits(AVRMS+phase);
}

// ADE7758 ADE(mosi, miso, sclk, cs);