ADE1202 Example proegram using ADE120x library files

Dependencies:   ADE120x

Committer:
mlambe
Date:
Thu Oct 03 15:06:13 2019 +0000
Revision:
2:2ebdd709cec0
Parent:
1:cff2074f52d5
Child:
3:8024f7ba736c
Added interrupts for DOUT pins

Who changed what in which revision?

UserRevisionLine numberNew contents of line
malavikasaji 0:4b7ec1ec0c7a 1 /* Copyright (c) 2019 Analog Devices, Inc. All rights reserved.
malavikasaji 0:4b7ec1ec0c7a 2
mlambe 2:2ebdd709cec0 3 Redistribution and use in source and binary forms, with or without modification,
malavikasaji 0:4b7ec1ec0c7a 4 are permitted provided that the following conditions are met:
mlambe 2:2ebdd709cec0 5 - Redistributions of source code must retain the above copyright notice,
malavikasaji 0:4b7ec1ec0c7a 6 this list of conditions and the following disclaimer.
mlambe 2:2ebdd709cec0 7 - Redistributions in binary form must reproduce the above copyright notice,
mlambe 2:2ebdd709cec0 8 this list of conditions and the following disclaimer in the documentation
mlambe 2:2ebdd709cec0 9 and/or other materials provided with the distribution.
malavikasaji 0:4b7ec1ec0c7a 10 - Modified versions of the software must be conspicuously marked as such.
mlambe 2:2ebdd709cec0 11 - This software is licensed solely and exclusively for use with processors/products
malavikasaji 0:4b7ec1ec0c7a 12 manufactured by or for Analog Devices, Inc.
mlambe 2:2ebdd709cec0 13 - This software may not be combined or merged with other code in any manner
mlambe 2:2ebdd709cec0 14 that would cause the software to become subject to terms and conditions which
malavikasaji 0:4b7ec1ec0c7a 15 differ from those listed here.
mlambe 2:2ebdd709cec0 16 - Neither the name of Analog Devices, Inc. nor the names of its contributors
mlambe 2:2ebdd709cec0 17 may be used to endorse or promote products derived from this software without
malavikasaji 0:4b7ec1ec0c7a 18 specific prior written permission.
mlambe 2:2ebdd709cec0 19 - The use of this software ma y or may not infringe the patent rights of one or
mlambe 2:2ebdd709cec0 20 more patent holders. This license does not release you from the requirement
malavikasaji 0:4b7ec1ec0c7a 21 that you obtain separate licenses from these patent holders to use this software.
malavikasaji 0:4b7ec1ec0c7a 22
mlambe 2:2ebdd709cec0 23 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND
mlambe 2:2ebdd709cec0 24 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
mlambe 2:2ebdd709cec0 25 TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
mlambe 2:2ebdd709cec0 26 NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
mlambe 2:2ebdd709cec0 27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES
mlambe 2:2ebdd709cec0 28 (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL
mlambe 2:2ebdd709cec0 29 PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
mlambe 2:2ebdd709cec0 30 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
mlambe 2:2ebdd709cec0 31 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
mlambe 2:2ebdd709cec0 32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
malavikasaji 0:4b7ec1ec0c7a 33 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
malavikasaji 0:4b7ec1ec0c7a 34
malavikasaji 0:4b7ec1ec0c7a 35 2019-01-10-7CBSD SLA
malavikasaji 0:4b7ec1ec0c7a 36
malavikasaji 1:cff2074f52d5 37 */
malavikasaji 0:4b7ec1ec0c7a 38
malavikasaji 1:cff2074f52d5 39 /***Libraries***/
mlambe 2:2ebdd709cec0 40 #include "ADE120x.h"
mlambe 2:2ebdd709cec0 41 #include "mbed.h"
mlambe 2:2ebdd709cec0 42
mlambe 2:2ebdd709cec0 43 #define ADC_PGA ADCPGA_10 /* Choose ADCPGA_1, ADCPGA_2, ADCPGA_5, ADCPGA_10 */
mlambe 2:2ebdd709cec0 44 #define V_Gain 0.003832 /* This is the gain of the Resistor divider network
mlambe 2:2ebdd709cec0 45 before the input to the ADE1202 */
malavikasaji 1:cff2074f52d5 46
mlambe 2:2ebdd709cec0 47 //Platform IOs and Timers
mlambe 2:2ebdd709cec0 48 InterruptIn DOUT1(SDP_GPIO_0); /* Init pin connected to U0, DOUT0 on EVAL-ADE1202EBZ*/
mlambe 2:2ebdd709cec0 49 InterruptIn DOUT2(SDP_GPIO_1); /* Init pin connected to U0, DOUT1 on EVAL-ADE1202EBZ*/
mlambe 2:2ebdd709cec0 50
mlambe 2:2ebdd709cec0 51 float voltage = 0.0;
mlambe 2:2ebdd709cec0 52
mlambe 2:2ebdd709cec0 53 /* ADE1202 Class defined with SPI */
mlambe 2:2ebdd709cec0 54 ADE120x ade1202(SDP_SPI_MOSI, SDP_SPI_MISO, SDP_SPI_SCK, SDP_SPI_CS_A); //MOSI, MISO, SCLK, /CS
malavikasaji 0:4b7ec1ec0c7a 55
mlambe 2:2ebdd709cec0 56 /* Initialize the serial port */
mlambe 2:2ebdd709cec0 57 Serial pc(USBTX, USBRX);
mlambe 2:2ebdd709cec0 58 uint8_t DOUT1_Status, DOUT2_Status = 0;
malavikasaji 0:4b7ec1ec0c7a 59
mlambe 2:2ebdd709cec0 60 /* Interrupt Handlers for DOUT1 and DOUT2 */
mlambe 2:2ebdd709cec0 61 void DOUT1_Int()
mlambe 2:2ebdd709cec0 62 {
mlambe 2:2ebdd709cec0 63 DOUT1_Status = 1;
mlambe 2:2ebdd709cec0 64 }
malavikasaji 0:4b7ec1ec0c7a 65
mlambe 2:2ebdd709cec0 66 void DOUT2_Int()
mlambe 2:2ebdd709cec0 67 {
mlambe 2:2ebdd709cec0 68 DOUT2_Status = 1;
mlambe 2:2ebdd709cec0 69 }
malavikasaji 1:cff2074f52d5 70
mlambe 2:2ebdd709cec0 71 /* main() runs in its own thread in the OS */
malavikasaji 0:4b7ec1ec0c7a 72 int main()
malavikasaji 0:4b7ec1ec0c7a 73 {
mlambe 2:2ebdd709cec0 74 uint8_t addr = 0x0; /* address of ADE120x device from 0x0 to 0xF*/
mlambe 2:2ebdd709cec0 75 uint8_t error = 0;
mlambe 2:2ebdd709cec0 76 uint32_t buffer[20];
mlambe 2:2ebdd709cec0 77 uint16_t filter_val;
mlambe 2:2ebdd709cec0 78 uint16_t device_id;
mlambe 2:2ebdd709cec0 79 THRESHCfg_Type thresh_cfg;
mlambe 2:2ebdd709cec0 80 PLOADCfg_Type plload_cfg;
mlambe 2:2ebdd709cec0 81 EnergyMtrCfg_Type enrgymtr_cfg;
mlambe 2:2ebdd709cec0 82 RegisterData_Type reg_data[20];
mlambe 2:2ebdd709cec0 83
mlambe 2:2ebdd709cec0 84 /* Intialize interrupts */
mlambe 2:2ebdd709cec0 85 DOUT1.rise(&DOUT1_Int);
mlambe 2:2ebdd709cec0 86 DOUT2.rise(&DOUT2_Int);
mlambe 2:2ebdd709cec0 87
mlambe 2:2ebdd709cec0 88 /* Initialize Uart with baud rate of 230400*/
mlambe 2:2ebdd709cec0 89 pc.baud(230400);
mlambe 2:2ebdd709cec0 90 pc.printf("ADE1202 Demo Application \n\r");
mlambe 2:2ebdd709cec0 91
mlambe 2:2ebdd709cec0 92 /* Reset the ADE1202 */
mlambe 2:2ebdd709cec0 93 ade1202.Reset(addr);
mlambe 2:2ebdd709cec0 94 wait_us(100000);
malavikasaji 1:cff2074f52d5 95
mlambe 2:2ebdd709cec0 96 /* Read back device ID */
mlambe 2:2ebdd709cec0 97 device_id = ade1202.GetDevID(addr);
mlambe 2:2ebdd709cec0 98 if((device_id & DEV_ADE1202) == DEV_ADE1202)
mlambe 2:2ebdd709cec0 99 pc.printf("Device is ADE1202\n");
mlambe 2:2ebdd709cec0 100 else
mlambe 2:2ebdd709cec0 101 pc.printf("Device is ADE1201\n");
mlambe 2:2ebdd709cec0 102
mlambe 2:2ebdd709cec0 103 /* Print silicon revision and device address */
mlambe 2:2ebdd709cec0 104 pc.printf("Rev ID is: %d * Device Address is %d \n", ADE120x_RevId(device_id), ADE120x_ChipAddr(device_id));
mlambe 2:2ebdd709cec0 105
mlambe 2:2ebdd709cec0 106 /* Unlock the device for programming */
mlambe 2:2ebdd709cec0 107 ade1202.UnLock(addr);
mlambe 2:2ebdd709cec0 108
mlambe 2:2ebdd709cec0 109 /* Wait some time after unlocking device */
mlambe 2:2ebdd709cec0 110 wait_us(1000);
mlambe 2:2ebdd709cec0 111
mlambe 2:2ebdd709cec0 112 /* Configure threshold registers and Modes using library function */
mlambe 2:2ebdd709cec0 113 thresh_cfg.BIN_HighThresh = 21.7; /* 22V */
mlambe 2:2ebdd709cec0 114 thresh_cfg.BIN_LowThresh = 11.5; /* 12V */
mlambe 2:2ebdd709cec0 115 thresh_cfg.WARNA_HighThresh = 26.1; /* 26V */
mlambe 2:2ebdd709cec0 116 thresh_cfg.WARNA_LowThresh = 26.1; /* 26V */
mlambe 2:2ebdd709cec0 117 thresh_cfg.WARNB_HighThresh = 17.4; /* 17V */
mlambe 2:2ebdd709cec0 118 thresh_cfg.WARNB_LowThresh = 11.5; /* 11V */
mlambe 2:2ebdd709cec0 119 thresh_cfg.WARNC_HighThresh = 5.8; /* 5V */
mlambe 2:2ebdd709cec0 120 thresh_cfg.WARNC_LowThresh = 5.8; /* 5V */
mlambe 2:2ebdd709cec0 121 thresh_cfg.BIN_Mode = Mode_Hysteretic;
mlambe 2:2ebdd709cec0 122 thresh_cfg.WARNA_Mode = Mode_Greater;
mlambe 2:2ebdd709cec0 123 thresh_cfg.WARNB_Mode = Mode_Inbetween;
mlambe 2:2ebdd709cec0 124 thresh_cfg.WARNC_Mode = Mode_LessEqual;
mlambe 2:2ebdd709cec0 125 thresh_cfg.ADCPga = 10;
mlambe 2:2ebdd709cec0 126 thresh_cfg.VGain = V_Gain;
mlambe 2:2ebdd709cec0 127 ade1202.ThresholdCfg(addr, &thresh_cfg);
malavikasaji 1:cff2074f52d5 128
mlambe 2:2ebdd709cec0 129 /* Step 3: Configure filter values for 3 ms*/
mlambe 2:2ebdd709cec0 130 /* FilterLength = GlitchWidth(us)/(20us)*/
mlambe 2:2ebdd709cec0 131 ade1202.WriteReg(addr, REG_BIN_FILTER, 0x8096);
mlambe 2:2ebdd709cec0 132 /* 5ms filter for WARNx */
mlambe 2:2ebdd709cec0 133 ade1202.WriteReg(addr, REG_WARNA_FILTER, 0x80FA);
mlambe 2:2ebdd709cec0 134 ade1202.WriteReg(addr, REG_WARNB_FILTER, 0x80FA);
mlambe 2:2ebdd709cec0 135 ade1202.WriteReg(addr, REG_WARNC_FILTER, 0x80FA);
mlambe 2:2ebdd709cec0 136
mlambe 2:2ebdd709cec0 137 /* Step 4: Configure programmable load */
mlambe 2:2ebdd709cec0 138 plload_cfg.ADCPga = 10;
mlambe 2:2ebdd709cec0 139 plload_cfg.enable = CH1_Enable|CH2_Enable; /*Enable for both channels */
mlambe 2:2ebdd709cec0 140 plload_cfg.HighCurrent = 30; /* 16mA */
mlambe 2:2ebdd709cec0 141 plload_cfg.HighTime = 1000; /* in us */
mlambe 2:2ebdd709cec0 142 plload_cfg.LowCurrent = 1; /* 3 mA */
mlambe 2:2ebdd709cec0 143 plload_cfg.mode = LOW_IDLE;
mlambe 2:2ebdd709cec0 144 plload_cfg.VGain = V_Gain;
mlambe 2:2ebdd709cec0 145 plload_cfg.VoltThresh = 3.84;
mlambe 2:2ebdd709cec0 146 ade1202.ProgrammableLoadCfg(addr, &plload_cfg);
mlambe 2:2ebdd709cec0 147
mlambe 2:2ebdd709cec0 148 /* Step 5: Configure Energy Monitor */
mlambe 2:2ebdd709cec0 149 enrgymtr_cfg.ADCPga = 10;
mlambe 2:2ebdd709cec0 150 enrgymtr_cfg.enable = 0;
mlambe 2:2ebdd709cec0 151 enrgymtr_cfg.VGain = V_Gain;
mlambe 2:2ebdd709cec0 152 enrgymtr_cfg.SampleRate = 20e-6; /* 10us on ADE1201, 20us on ADE1202 */
mlambe 2:2ebdd709cec0 153 enrgymtr_cfg.WorkingVoltage = 250;
mlambe 2:2ebdd709cec0 154 enrgymtr_cfg.PulseMagnitude = 16; /* 16mA */
mlambe 2:2ebdd709cec0 155 enrgymtr_cfg.PulseTime = 3; /* 3ms */
mlambe 2:2ebdd709cec0 156 enrgymtr_cfg.Cooldown_Decr = 5;
mlambe 2:2ebdd709cec0 157 enrgymtr_cfg.Cooldown_TimeStep = COOLDOWN_TS_10us;
mlambe 2:2ebdd709cec0 158 enrgymtr_cfg.Ov_Scale = OV_SCALE_1;
mlambe 2:2ebdd709cec0 159 enrgymtr_cfg.Cooldown_Sec = 5;
mlambe 2:2ebdd709cec0 160 ade1202.EnergyMtrCfg(addr, &enrgymtr_cfg);
mlambe 2:2ebdd709cec0 161
mlambe 2:2ebdd709cec0 162 /* Set ADC PGA */
mlambe 2:2ebdd709cec0 163 ade1202.SetPgaGain(addr, ADCPGA_10);
mlambe 2:2ebdd709cec0 164
mlambe 2:2ebdd709cec0 165 /* Lock device after configuring registers */
mlambe 2:2ebdd709cec0 166 ade1202.Lock(addr);//add 100us delay
mlambe 2:2ebdd709cec0 167 wait_us(100);
mlambe 2:2ebdd709cec0 168 ade1202.ClearIntStatus(addr, INTSRC_ALL);
mlambe 2:2ebdd709cec0 169
mlambe 2:2ebdd709cec0 170 /* Read back and print all register settings after configuration */
mlambe 2:2ebdd709cec0 171 ade1202.GetRegisterData(addr, (RegisterData_Type*)reg_data);
mlambe 2:2ebdd709cec0 172 for(int i = 0; i<20;i++)
mlambe 2:2ebdd709cec0 173 printf("0x%x , 0x%x \n", reg_data[i].reg_addr, reg_data[i].reg_data);
mlambe 2:2ebdd709cec0 174
mlambe 2:2ebdd709cec0 175
mlambe 2:2ebdd709cec0 176 /* Enter main program loop and wait for threshold events */
mlambe 2:2ebdd709cec0 177 while(1) {
mlambe 2:2ebdd709cec0 178 uint32_t reg_data, status = 0;
mlambe 2:2ebdd709cec0 179 status = ade1202.GetIntStatus(addr); /* Check status register */
mlambe 2:2ebdd709cec0 180 if(DOUT1_Status)
mlambe 2:2ebdd709cec0 181 {
mlambe 2:2ebdd709cec0 182 DOUT1_Status = 0;
mlambe 2:2ebdd709cec0 183 reg_data = ade1202.ReadADC(addr, ADC_RAW);
mlambe 2:2ebdd709cec0 184 printf("DOUT1 Interrupt detected! ");
mlambe 2:2ebdd709cec0 185 printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
mlambe 2:2ebdd709cec0 186 }
mlambe 2:2ebdd709cec0 187 if(DOUT2_Status)
mlambe 2:2ebdd709cec0 188 {
mlambe 2:2ebdd709cec0 189 DOUT2_Status = 0;
mlambe 2:2ebdd709cec0 190 reg_data = ade1202.ReadADC(addr, ADC_RAW);
mlambe 2:2ebdd709cec0 191 printf("DOUT2 Interrupt detected! ");
mlambe 2:2ebdd709cec0 192 printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
mlambe 2:2ebdd709cec0 193 }
mlambe 2:2ebdd709cec0 194 if(status != 0)
mlambe 2:2ebdd709cec0 195 {
mlambe 2:2ebdd709cec0 196 if((status & INTSRC_WARNA1) == INTSRC_WARNA1)
mlambe 2:2ebdd709cec0 197 {
mlambe 2:2ebdd709cec0 198 reg_data = ade1202.ReadADC(addr, ADC_RAW);
mlambe 2:2ebdd709cec0 199 printf("WARNA Interrupt detected! Voltage > 22V ");
mlambe 2:2ebdd709cec0 200 printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
mlambe 2:2ebdd709cec0 201 }
mlambe 2:2ebdd709cec0 202 if((status & INTSRC_WARNB1) == INTSRC_WARNB1)
mlambe 2:2ebdd709cec0 203 {
mlambe 2:2ebdd709cec0 204 reg_data = ade1202.ReadADC(addr, ADC_RAW);
mlambe 2:2ebdd709cec0 205 printf("WARNB Interrupt detected! Voltage in between 11V and 15V ");
mlambe 2:2ebdd709cec0 206 printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
mlambe 2:2ebdd709cec0 207 }
mlambe 2:2ebdd709cec0 208 if((status & INTSRC_WARNC1) == INTSRC_WARNC1)
mlambe 2:2ebdd709cec0 209 {
mlambe 2:2ebdd709cec0 210 reg_data = ade1202.ReadADC(addr, ADC_RAW);
mlambe 2:2ebdd709cec0 211 printf("WARNC Interrupt detected! Voltage below 5V");
mlambe 2:2ebdd709cec0 212 printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
mlambe 2:2ebdd709cec0 213 }
mlambe 2:2ebdd709cec0 214 ade1202.ClearIntStatus(addr, INTSRC_ALL);
malavikasaji 1:cff2074f52d5 215 }
malavikasaji 0:4b7ec1ec0c7a 216 }
malavikasaji 1:cff2074f52d5 217 return 0;
mlambe 2:2ebdd709cec0 218 }