ADE1202 Example proegram using ADE120x library files

Dependencies:   ADE120x

Revision:
3:8024f7ba736c
Parent:
2:2ebdd709cec0
Child:
4:5ea2e3188584
diff -r 2ebdd709cec0 -r 8024f7ba736c main.cpp
--- a/main.cpp	Thu Oct 03 15:06:13 2019 +0000
+++ b/main.cpp	Tue Oct 15 12:10:33 2019 +0000
@@ -1,40 +1,18 @@
-/* Copyright (c) 2019 Analog Devices, Inc.  All rights reserved.
-
-Redistribution and use in source and binary forms, with or without modification,
-are permitted provided that the following conditions are met:
-  - Redistributions of source code must retain the above copyright notice,
-  this list of conditions and the following disclaimer.
-  - Redistributions in binary form must reproduce the above copyright notice,
-  this list of conditions and the following disclaimer in the documentation
-  and/or other materials provided with the distribution.
-  - Modified versions of the software must be conspicuously marked as such.
-  - This software is licensed solely and exclusively for use with processors/products
-  manufactured by or for Analog Devices, Inc.
-  - This software may not be combined or merged with other code in any manner
-  that would cause the software to become subject to terms and conditions which
-  differ from those listed here.
-  - Neither the name of Analog Devices, Inc. nor the names of its contributors
-  may be used to endorse or promote products derived from this software without
-  specific prior written permission.
-  - The use of this software ma     y or may not infringe the patent rights of one or
-  more patent holders.  This license does not release you from the requirement
-  that you obtain separate licenses from these patent holders to use this software.
-
-THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
-TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
-NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
-INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES
-(INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL
-PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
-OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
-EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-2019-01-10-7CBSD SLA
-
-*/
+/**  
+ * @file       main.cpp
+ * @brief      Main file. This file uses the ADE120x drivers to configure the part
+ *             Use AN-2034 in conjunction with this example 
+ * @version    V0.0.1
+ * @author     ADI
+ * @date       October 2019
+ * @par Revision History:
+ * 
+ * Copyright (c) 2017-2019 Analog Devices, Inc. All Rights Reserved.
+ * 
+ * This software is proprietary to Analog Devices, Inc. and its licensors.
+ * By using this software you agree to the terms of the associated
+ * Analog Devices Software License Agreement.
+**/
 
 /***Libraries***/
 #include "ADE120x.h"
@@ -48,14 +26,15 @@
 InterruptIn DOUT1(SDP_GPIO_0);    /* Init pin connected to U0, DOUT0 on EVAL-ADE1202EBZ*/
 InterruptIn DOUT2(SDP_GPIO_1);    /* Init pin connected to U0, DOUT1 on EVAL-ADE1202EBZ*/
 
-float voltage = 0.0;
 
 /* ADE1202 Class defined with SPI */
 ADE120x ade1202(SDP_SPI_MOSI, SDP_SPI_MISO, SDP_SPI_SCK, SDP_SPI_CS_A);    //MOSI, MISO, SCLK, /CS
 
 /* Initialize the serial port */
 Serial pc(USBTX, USBRX);
-uint8_t DOUT1_Status, DOUT2_Status = 0; 
+
+uint8_t DOUT1_Status, DOUT2_Status = 0;
+float voltage = 0.0; 
 
 /* Interrupt Handlers for DOUT1 and DOUT2 */
 void DOUT1_Int()
@@ -72,16 +51,15 @@
 int main()
 {
     uint8_t addr = 0x0;                /* address of ADE120x device from 0x0 to 0xF*/
-    uint8_t error = 0;
-    uint32_t buffer[20];
     uint16_t filter_val;
     uint16_t device_id;
     THRESHCfg_Type thresh_cfg;
     PLOADCfg_Type plload_cfg;
     EnergyMtrCfg_Type enrgymtr_cfg;
-    RegisterData_Type reg_data[20];
+    RegisterData_Type reg_data[20];     /* Buffer to read back register settings 
+                                          after configuration to confirm values */
     
-    /* Intialize interrupts */
+    /* Intialize interrupts to fire on rising edge*/
     DOUT1.rise(&DOUT1_Int);
     DOUT2.rise(&DOUT2_Int);
     
@@ -167,7 +145,7 @@
     wait_us(100);
     ade1202.ClearIntStatus(addr, INTSRC_ALL);
     
-    /* Read back and print all register settings after configuration */
+    /* Read back and print all register settings after configuration to confirm they are correct*/
     ade1202.GetRegisterData(addr, (RegisterData_Type*)reg_data);
     for(int i = 0; i<20;i++)
         printf("0x%x , 0x%x \n", reg_data[i].reg_addr, reg_data[i].reg_data);
@@ -179,37 +157,39 @@
         status = ade1202.GetIntStatus(addr); /* Check status register */
         if(DOUT1_Status)
         {
+            /* Interrupt detected on DOUT1 */
             DOUT1_Status = 0;
             reg_data = ade1202.ReadADC(addr, ADC_RAW);
-            printf("DOUT1 Interrupt detected! ");
-            printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
+            pc.printf("DOUT1 Interrupt detected! ");
+            pc.printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
         }
         if(DOUT2_Status)
         {
+            /* Interrupt detected on DOUT2 */
             DOUT2_Status = 0;
             reg_data = ade1202.ReadADC(addr, ADC_RAW);
-            printf("DOUT2 Interrupt detected! ");
-            printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
+            pc.printf("DOUT2 Interrupt detected! ");
+            pc.printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
         }
         if(status != 0)
         {
             if((status & INTSRC_WARNA1) == INTSRC_WARNA1)
             {
                 reg_data = ade1202.ReadADC(addr, ADC_RAW);
-                printf("WARNA Interrupt detected! Voltage > 22V ");
-                printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
+                pc.printf("WARNA Interrupt detected! Voltage > 22V ");
+                pc.printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
             }
             if((status & INTSRC_WARNB1) == INTSRC_WARNB1)
             {
                 reg_data = ade1202.ReadADC(addr, ADC_RAW);
-                printf("WARNB Interrupt detected! Voltage in between 11V and 15V ");
-                printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
+                pc.printf("WARNB Interrupt detected! Voltage in between 11V and 15V ");
+                pc.printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
             }
             if((status & INTSRC_WARNC1) == INTSRC_WARNC1)
             {
                 reg_data = ade1202.ReadADC(addr, ADC_RAW);
-                printf("WARNC Interrupt detected! Voltage below 5V");
-                printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
+                pc.printf("WARNC Interrupt detected! Voltage below 5V");
+                pc.printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
             }
             ade1202.ClearIntStatus(addr, INTSRC_ALL);
         }