#include <SPI.h>
//#include <SoftwareSerial.h>
const char PIN_ADE_INT = 2; // Interrupt from ADE
const char PIN_ADE_RST = 3; // ~Reset to ADE
const char PIN_ADE_CS = 5; // SPI ~chip select to ADE
const char PIN_ADE_CLK = 18; // SPI clock to ADE
const char PIN_ADE_MISO = 19; // SPI MISO from ADE
const char PIN_ADE_MOSI = 23; // SPI MOSI from ADE
const int MR_IRMS = 0x16; /* Current Channel 1 RMS Value. */
const int MR_VRMS = 0x17; /* Voltage Channel 2 RMS Value. */
const int MR_IPEAK = 0x22; /* Current Channel Peak Register. */
const int MR_VPEAK = 0x24; /* Voltage Channel Peak Register. */
const int MR_MODE = 0x09; /* Mode Register. */
const int MR_CH1OS = 0x0D; /* Current Channel Offset Adjust. */
const int MR_CH2OS = 0x0E; /* Voltage Channel Offset Adjust. */
const int MR_GAIN = 0x0F; /* PGA Gain Adjust. */
const int ADE_WRITE_FLAG = 0x80;
const int MR_IRMS_CNT = 3;
const int MR_VRMS_CNT = 3;
const int MR_IPEAK_CNT = 3;
const int MR_VPEAK_CNT = 3;
const int MR_MODE_CNT = 2;
const int MR_CH1OS_CNT = 1;
const int MR_CH2OS_CNT = 1;
const int MR_GAIN_CNT = 1;
/* Prototypes */
int count = 0;
void ADE_read (unsigned char addr,
unsigned char * data,
unsigned char count);
void ADE_write(unsigned char addr,
unsigned char * data,
unsigned char count);
/**********************************************************************
*
* Set up routine
*
*********************************************************************/
void setup(void) {
unsigned int ui;
unsigned char uc;
/* Initialize the serial port to host */
Serial.begin(115200);
/* Configure Chip Select */
pinMode(PIN_ADE_CS, OUTPUT);
digitalWrite(PIN_ADE_CS, HIGH); // Deselect
delayMicroseconds(10);
/* Enable SPI */
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1); // clk idle low, sample falling edge
SPI.setClockDivider(SPI_CLOCK_DIV4);
/*****************************/
/* Initialize the ADE */
/*****************************/
/* Set the mode */
ui = 0x0080;
ADE_write (MR_MODE, (unsigned char *)&ui, MR_MODE_CNT);
/* Set up the gain register */
uc = 0x00; // ADC1,2 gain = 1 full scale range is +-0.5V
ADE_write (MR_GAIN, (unsigned char *)&uc, MR_GAIN_CNT);
/* Set up the offset correction for ADC1 */
uc = 0x00;
ADE_write (MR_CH1OS, (unsigned char *)&uc, MR_CH1OS_CNT);
/* Set up the offset correction for ADC2 */
uc = 0x00;
ADE_write (MR_CH2OS, (unsigned char *)&uc, MR_CH2OS_CNT);
Serial.println("setup complete");
return;
} /* end setup */
/**********************************************************************
*
* Main loop
*
*********************************************************************/
void loop(void) {
int i, v, p;
unsigned long ul;
int temp;
float ADC_MAX_RANGE = 0.5;
float Line_Vrms_Max = 260;
int ADC_RES = 10322;
double Low_Vrms = 0.0;
double Full_Vrms = 0.0;
delay(5000);
ul = 0;
ADE_read(MR_IRMS, (unsigned char *)&ul, MR_IRMS_CNT);
Serial.print("IRMS 0x");
Serial.println(ul, HEX);
Serial.print("Irms (dec): ");
Serial.println((int)ul);
ul = 0;
ADE_read(MR_VRMS, (unsigned char *)&ul, MR_VRMS_CNT);
Serial.print("VRMS 0x");
Serial.println(ul, HEX);
Serial.print("VRMS (dec): ");
temp = (int)ul;
Serial.println(temp);
Low_Vrms = ( ADC_MAX_RANGE / ADC_RES ) * temp; // Senthil check. This part is not working
Serial.print("1Vrms equiv: ");
Serial.println(Low_Vrms);
Full_Vrms = Low_Vrms * Line_Vrms_Max; //Senthil check. This part is not working
Serial.print("Full Vpp equiv: ");
Serial.println(Full_Vrms);
ul = 0;
ADE_read(MR_IPEAK, (unsigned char *)&ul, MR_IPEAK_CNT);
Serial.print("IPEAK 0x");
Serial.println(ul, HEX);
ul = 0;
ADE_read(MR_VPEAK, (unsigned char *)&ul, MR_VPEAK_CNT);
Serial.print("VPEAK 0x");
Serial.println(ul, HEX);
Serial.print("VPEAK (dec): ");
Serial.println((int)ul);
Serial.print("--------------Loop ends --------------");
Serial.println(count);
count++;
} /* end loop */
/**************************************************************************
* ADE_read
*
* This routine enables the chip select and read the selected number
* of bytes from the ADE.
*
* ADE is big endian and the Arduino is little. Therefore items are
* assembled in revese order.
*************************************************************************/
void ADE_read (unsigned char addr,
unsigned char * data,
unsigned char count)
{
unsigned char i;
/* If a 3 byte read, zero out the MSB of the 4 byte data */
if (count == 3) {
//*((unsigned long *)(data+4-1)) = 0;
}
/* Select the chip */
digitalWrite(PIN_ADE_CS, LOW);
delayMicroseconds(10);
/* Point to the Arduino MSB */
data += (count-1);
/* Write the address to access */
SPI.transfer(addr);
/* Must wait 4 us for data to become valid */
delayMicroseconds(4);
// Do for each byte in transfer
for (i=0; i<count; i++)
{
/* Transer the byte */
*data = SPI.transfer (0x00);
data--;
}
/* Deselect the chip */
digitalWrite(PIN_ADE_CS, HIGH);
delayMicroseconds(10);
} /* end ADE_read */
/**************************************************************************
* ADE_write
* This routine enables chip select and reads the selected number of
* bytes from the ADE
*
* ADE is big endian and the Arduino is little. Therefore items are
* assembled in revese order.
*************************************************************************/
void ADE_write(unsigned char addr,
unsigned char * data,
unsigned char count)
{
unsigned char i;
/* Select the chip */
digitalWrite(PIN_ADE_CS, LOW);
delayMicroseconds(10);
/* Point to the Arduino MSB */
data += (count-1);
/* Write the address to access */
SPI.transfer(addr | ADE_WRITE_FLAG);
// Do for each byte */
for (i=0; i<count; i++)
{
/* Transer the byte */
SPI.transfer(*data); // write all the bytes
data--;
}
/* Deselect chip */
digitalWrite(PIN_ADE_CS, HIGH);
delayMicroseconds(10);
} /* end ADE_write */