ADE1202 Example proegram using ADE120x library files

Dependencies:   ADE120x

Committer:
mlambe
Date:
Tue Oct 22 13:40:27 2019 +0000
Revision:
6:bded7f68793d
Parent:
4:5ea2e3188584
Initial publish

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mlambe 3:8024f7ba736c 1 /**
mlambe 4:5ea2e3188584 2 @file main.cpp
mlambe 4:5ea2e3188584 3 @brief Main file. This file uses the ADE120x drivers to configure the ADE1202
mlambe 4:5ea2e3188584 4 Use AN-2034 in conjunction with this example and EVAL-ADE1202EBZ
mlambe 4:5ea2e3188584 5 @version V0.0.1
mlambe 4:5ea2e3188584 6 @author ADI
mlambe 4:5ea2e3188584 7 @date October 2019
mlambe 4:5ea2e3188584 8
mlambe 4:5ea2e3188584 9 Copyright (c) 2017-2019 Analog Devices, Inc. All Rights Reserved.
mlambe 4:5ea2e3188584 10
mlambe 4:5ea2e3188584 11 This software is proprietary to Analog Devices, Inc. and its licensors.
mlambe 4:5ea2e3188584 12 By using this software you agree to the terms of the associated
mlambe 4:5ea2e3188584 13 Analog Devices Software License Agreement.
mlambe 4:5ea2e3188584 14 *****************************************************************************/
mlambe 4:5ea2e3188584 15
malavikasaji 0:4b7ec1ec0c7a 16
malavikasaji 1:cff2074f52d5 17 /***Libraries***/
mlambe 2:2ebdd709cec0 18 #include "ADE120x.h"
mlambe 2:2ebdd709cec0 19 #include "mbed.h"
mlambe 2:2ebdd709cec0 20
mlambe 2:2ebdd709cec0 21 #define ADC_PGA ADCPGA_10 /* Choose ADCPGA_1, ADCPGA_2, ADCPGA_5, ADCPGA_10 */
mlambe 2:2ebdd709cec0 22 #define V_Gain 0.003832 /* This is the gain of the Resistor divider network
mlambe 2:2ebdd709cec0 23 before the input to the ADE1202 */
malavikasaji 1:cff2074f52d5 24
mlambe 2:2ebdd709cec0 25 //Platform IOs and Timers
mlambe 2:2ebdd709cec0 26 InterruptIn DOUT1(SDP_GPIO_0); /* Init pin connected to U0, DOUT0 on EVAL-ADE1202EBZ*/
mlambe 2:2ebdd709cec0 27 InterruptIn DOUT2(SDP_GPIO_1); /* Init pin connected to U0, DOUT1 on EVAL-ADE1202EBZ*/
mlambe 2:2ebdd709cec0 28
mlambe 2:2ebdd709cec0 29
mlambe 2:2ebdd709cec0 30 /* ADE1202 Class defined with SPI */
mlambe 2:2ebdd709cec0 31 ADE120x ade1202(SDP_SPI_MOSI, SDP_SPI_MISO, SDP_SPI_SCK, SDP_SPI_CS_A); //MOSI, MISO, SCLK, /CS
malavikasaji 0:4b7ec1ec0c7a 32
mlambe 2:2ebdd709cec0 33 /* Initialize the serial port */
mlambe 2:2ebdd709cec0 34 Serial pc(USBTX, USBRX);
mlambe 3:8024f7ba736c 35
mlambe 3:8024f7ba736c 36 uint8_t DOUT1_Status, DOUT2_Status = 0;
mlambe 3:8024f7ba736c 37 float voltage = 0.0;
malavikasaji 0:4b7ec1ec0c7a 38
mlambe 2:2ebdd709cec0 39 /* Interrupt Handlers for DOUT1 and DOUT2 */
mlambe 2:2ebdd709cec0 40 void DOUT1_Int()
mlambe 2:2ebdd709cec0 41 {
mlambe 2:2ebdd709cec0 42 DOUT1_Status = 1;
mlambe 2:2ebdd709cec0 43 }
malavikasaji 0:4b7ec1ec0c7a 44
mlambe 2:2ebdd709cec0 45 void DOUT2_Int()
mlambe 2:2ebdd709cec0 46 {
mlambe 2:2ebdd709cec0 47 DOUT2_Status = 1;
mlambe 2:2ebdd709cec0 48 }
malavikasaji 1:cff2074f52d5 49
mlambe 2:2ebdd709cec0 50 /* main() runs in its own thread in the OS */
malavikasaji 0:4b7ec1ec0c7a 51 int main()
malavikasaji 0:4b7ec1ec0c7a 52 {
mlambe 2:2ebdd709cec0 53 uint8_t addr = 0x0; /* address of ADE120x device from 0x0 to 0xF*/
mlambe 2:2ebdd709cec0 54 uint16_t filter_val;
mlambe 2:2ebdd709cec0 55 uint16_t device_id;
mlambe 2:2ebdd709cec0 56 THRESHCfg_Type thresh_cfg;
mlambe 2:2ebdd709cec0 57 PLOADCfg_Type plload_cfg;
mlambe 2:2ebdd709cec0 58 EnergyMtrCfg_Type enrgymtr_cfg;
mlambe 3:8024f7ba736c 59 RegisterData_Type reg_data[20]; /* Buffer to read back register settings
mlambe 3:8024f7ba736c 60 after configuration to confirm values */
mlambe 2:2ebdd709cec0 61
mlambe 3:8024f7ba736c 62 /* Intialize interrupts to fire on rising edge*/
mlambe 2:2ebdd709cec0 63 DOUT1.rise(&DOUT1_Int);
mlambe 2:2ebdd709cec0 64 DOUT2.rise(&DOUT2_Int);
mlambe 2:2ebdd709cec0 65
mlambe 2:2ebdd709cec0 66 /* Initialize Uart with baud rate of 230400*/
mlambe 2:2ebdd709cec0 67 pc.baud(230400);
mlambe 2:2ebdd709cec0 68 pc.printf("ADE1202 Demo Application \n\r");
mlambe 2:2ebdd709cec0 69
mlambe 2:2ebdd709cec0 70 /* Reset the ADE1202 */
mlambe 2:2ebdd709cec0 71 ade1202.Reset(addr);
mlambe 2:2ebdd709cec0 72 wait_us(100000);
malavikasaji 1:cff2074f52d5 73
mlambe 2:2ebdd709cec0 74 /* Read back device ID */
mlambe 2:2ebdd709cec0 75 device_id = ade1202.GetDevID(addr);
mlambe 2:2ebdd709cec0 76 if((device_id & DEV_ADE1202) == DEV_ADE1202)
mlambe 2:2ebdd709cec0 77 pc.printf("Device is ADE1202\n");
mlambe 2:2ebdd709cec0 78 else
mlambe 2:2ebdd709cec0 79 pc.printf("Device is ADE1201\n");
mlambe 2:2ebdd709cec0 80
mlambe 2:2ebdd709cec0 81 /* Print silicon revision and device address */
mlambe 2:2ebdd709cec0 82 pc.printf("Rev ID is: %d * Device Address is %d \n", ADE120x_RevId(device_id), ADE120x_ChipAddr(device_id));
mlambe 2:2ebdd709cec0 83
mlambe 2:2ebdd709cec0 84 /* Unlock the device for programming */
mlambe 2:2ebdd709cec0 85 ade1202.UnLock(addr);
mlambe 2:2ebdd709cec0 86
mlambe 2:2ebdd709cec0 87 /* Wait some time after unlocking device */
mlambe 2:2ebdd709cec0 88 wait_us(1000);
mlambe 2:2ebdd709cec0 89
mlambe 2:2ebdd709cec0 90 /* Configure threshold registers and Modes using library function */
mlambe 2:2ebdd709cec0 91 thresh_cfg.BIN_HighThresh = 21.7; /* 22V */
mlambe 2:2ebdd709cec0 92 thresh_cfg.BIN_LowThresh = 11.5; /* 12V */
mlambe 2:2ebdd709cec0 93 thresh_cfg.WARNA_HighThresh = 26.1; /* 26V */
mlambe 2:2ebdd709cec0 94 thresh_cfg.WARNA_LowThresh = 26.1; /* 26V */
mlambe 2:2ebdd709cec0 95 thresh_cfg.WARNB_HighThresh = 17.4; /* 17V */
mlambe 2:2ebdd709cec0 96 thresh_cfg.WARNB_LowThresh = 11.5; /* 11V */
mlambe 2:2ebdd709cec0 97 thresh_cfg.WARNC_HighThresh = 5.8; /* 5V */
mlambe 2:2ebdd709cec0 98 thresh_cfg.WARNC_LowThresh = 5.8; /* 5V */
mlambe 2:2ebdd709cec0 99 thresh_cfg.BIN_Mode = Mode_Hysteretic;
mlambe 2:2ebdd709cec0 100 thresh_cfg.WARNA_Mode = Mode_Greater;
mlambe 2:2ebdd709cec0 101 thresh_cfg.WARNB_Mode = Mode_Inbetween;
mlambe 2:2ebdd709cec0 102 thresh_cfg.WARNC_Mode = Mode_LessEqual;
mlambe 2:2ebdd709cec0 103 thresh_cfg.ADCPga = 10;
mlambe 2:2ebdd709cec0 104 thresh_cfg.VGain = V_Gain;
mlambe 2:2ebdd709cec0 105 ade1202.ThresholdCfg(addr, &thresh_cfg);
malavikasaji 1:cff2074f52d5 106
mlambe 2:2ebdd709cec0 107 /* Step 3: Configure filter values for 3 ms*/
mlambe 2:2ebdd709cec0 108 /* FilterLength = GlitchWidth(us)/(20us)*/
mlambe 2:2ebdd709cec0 109 ade1202.WriteReg(addr, REG_BIN_FILTER, 0x8096);
mlambe 2:2ebdd709cec0 110 /* 5ms filter for WARNx */
mlambe 2:2ebdd709cec0 111 ade1202.WriteReg(addr, REG_WARNA_FILTER, 0x80FA);
mlambe 2:2ebdd709cec0 112 ade1202.WriteReg(addr, REG_WARNB_FILTER, 0x80FA);
mlambe 2:2ebdd709cec0 113 ade1202.WriteReg(addr, REG_WARNC_FILTER, 0x80FA);
mlambe 2:2ebdd709cec0 114
mlambe 2:2ebdd709cec0 115 /* Step 4: Configure programmable load */
mlambe 2:2ebdd709cec0 116 plload_cfg.ADCPga = 10;
mlambe 2:2ebdd709cec0 117 plload_cfg.enable = CH1_Enable|CH2_Enable; /*Enable for both channels */
mlambe 2:2ebdd709cec0 118 plload_cfg.HighCurrent = 30; /* 16mA */
mlambe 2:2ebdd709cec0 119 plload_cfg.HighTime = 1000; /* in us */
mlambe 2:2ebdd709cec0 120 plload_cfg.LowCurrent = 1; /* 3 mA */
mlambe 2:2ebdd709cec0 121 plload_cfg.mode = LOW_IDLE;
mlambe 2:2ebdd709cec0 122 plload_cfg.VGain = V_Gain;
mlambe 2:2ebdd709cec0 123 plload_cfg.VoltThresh = 3.84;
mlambe 2:2ebdd709cec0 124 ade1202.ProgrammableLoadCfg(addr, &plload_cfg);
mlambe 2:2ebdd709cec0 125
mlambe 2:2ebdd709cec0 126 /* Step 5: Configure Energy Monitor */
mlambe 2:2ebdd709cec0 127 enrgymtr_cfg.ADCPga = 10;
mlambe 2:2ebdd709cec0 128 enrgymtr_cfg.enable = 0;
mlambe 2:2ebdd709cec0 129 enrgymtr_cfg.VGain = V_Gain;
mlambe 2:2ebdd709cec0 130 enrgymtr_cfg.SampleRate = 20e-6; /* 10us on ADE1201, 20us on ADE1202 */
mlambe 2:2ebdd709cec0 131 enrgymtr_cfg.WorkingVoltage = 250;
mlambe 2:2ebdd709cec0 132 enrgymtr_cfg.PulseMagnitude = 16; /* 16mA */
mlambe 2:2ebdd709cec0 133 enrgymtr_cfg.PulseTime = 3; /* 3ms */
mlambe 2:2ebdd709cec0 134 enrgymtr_cfg.Cooldown_Decr = 5;
mlambe 2:2ebdd709cec0 135 enrgymtr_cfg.Cooldown_TimeStep = COOLDOWN_TS_10us;
mlambe 2:2ebdd709cec0 136 enrgymtr_cfg.Ov_Scale = OV_SCALE_1;
mlambe 2:2ebdd709cec0 137 enrgymtr_cfg.Cooldown_Sec = 5;
mlambe 2:2ebdd709cec0 138 ade1202.EnergyMtrCfg(addr, &enrgymtr_cfg);
mlambe 2:2ebdd709cec0 139
mlambe 2:2ebdd709cec0 140 /* Set ADC PGA */
mlambe 2:2ebdd709cec0 141 ade1202.SetPgaGain(addr, ADCPGA_10);
mlambe 2:2ebdd709cec0 142
mlambe 2:2ebdd709cec0 143 /* Lock device after configuring registers */
mlambe 2:2ebdd709cec0 144 ade1202.Lock(addr);//add 100us delay
mlambe 2:2ebdd709cec0 145 wait_us(100);
mlambe 2:2ebdd709cec0 146 ade1202.ClearIntStatus(addr, INTSRC_ALL);
mlambe 2:2ebdd709cec0 147
mlambe 3:8024f7ba736c 148 /* Read back and print all register settings after configuration to confirm they are correct*/
mlambe 2:2ebdd709cec0 149 ade1202.GetRegisterData(addr, (RegisterData_Type*)reg_data);
mlambe 2:2ebdd709cec0 150 for(int i = 0; i<20;i++)
mlambe 2:2ebdd709cec0 151 printf("0x%x , 0x%x \n", reg_data[i].reg_addr, reg_data[i].reg_data);
mlambe 2:2ebdd709cec0 152
mlambe 2:2ebdd709cec0 153
mlambe 2:2ebdd709cec0 154 /* Enter main program loop and wait for threshold events */
mlambe 2:2ebdd709cec0 155 while(1) {
mlambe 2:2ebdd709cec0 156 uint32_t reg_data, status = 0;
mlambe 2:2ebdd709cec0 157 status = ade1202.GetIntStatus(addr); /* Check status register */
mlambe 2:2ebdd709cec0 158 if(DOUT1_Status)
mlambe 2:2ebdd709cec0 159 {
mlambe 3:8024f7ba736c 160 /* Interrupt detected on DOUT1 */
mlambe 2:2ebdd709cec0 161 DOUT1_Status = 0;
mlambe 2:2ebdd709cec0 162 reg_data = ade1202.ReadADC(addr, ADC_RAW);
mlambe 3:8024f7ba736c 163 pc.printf("DOUT1 Interrupt detected! ");
mlambe 3:8024f7ba736c 164 pc.printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
mlambe 2:2ebdd709cec0 165 }
mlambe 2:2ebdd709cec0 166 if(DOUT2_Status)
mlambe 2:2ebdd709cec0 167 {
mlambe 3:8024f7ba736c 168 /* Interrupt detected on DOUT2 */
mlambe 2:2ebdd709cec0 169 DOUT2_Status = 0;
mlambe 2:2ebdd709cec0 170 reg_data = ade1202.ReadADC(addr, ADC_RAW);
mlambe 3:8024f7ba736c 171 pc.printf("DOUT2 Interrupt detected! ");
mlambe 3:8024f7ba736c 172 pc.printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
mlambe 2:2ebdd709cec0 173 }
mlambe 2:2ebdd709cec0 174 if(status != 0)
mlambe 2:2ebdd709cec0 175 {
mlambe 2:2ebdd709cec0 176 if((status & INTSRC_WARNA1) == INTSRC_WARNA1)
mlambe 2:2ebdd709cec0 177 {
mlambe 2:2ebdd709cec0 178 reg_data = ade1202.ReadADC(addr, ADC_RAW);
mlambe 3:8024f7ba736c 179 pc.printf("WARNA Interrupt detected! Voltage > 22V ");
mlambe 3:8024f7ba736c 180 pc.printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
mlambe 2:2ebdd709cec0 181 }
mlambe 2:2ebdd709cec0 182 if((status & INTSRC_WARNB1) == INTSRC_WARNB1)
mlambe 2:2ebdd709cec0 183 {
mlambe 2:2ebdd709cec0 184 reg_data = ade1202.ReadADC(addr, ADC_RAW);
mlambe 3:8024f7ba736c 185 pc.printf("WARNB Interrupt detected! Voltage in between 11V and 15V ");
mlambe 3:8024f7ba736c 186 pc.printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
mlambe 2:2ebdd709cec0 187 }
mlambe 2:2ebdd709cec0 188 if((status & INTSRC_WARNC1) == INTSRC_WARNC1)
mlambe 2:2ebdd709cec0 189 {
mlambe 2:2ebdd709cec0 190 reg_data = ade1202.ReadADC(addr, ADC_RAW);
mlambe 3:8024f7ba736c 191 pc.printf("WARNC Interrupt detected! Voltage below 5V");
mlambe 3:8024f7ba736c 192 pc.printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
mlambe 2:2ebdd709cec0 193 }
mlambe 2:2ebdd709cec0 194 ade1202.ClearIntStatus(addr, INTSRC_ALL);
malavikasaji 1:cff2074f52d5 195 }
malavikasaji 0:4b7ec1ec0c7a 196 }
malavikasaji 1:cff2074f52d5 197 return 0;
mlambe 2:2ebdd709cec0 198 }