Analog Devices / Mbed OS EVAL-ADE120x

Dependencies:   ADE120x

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**  
00002  @file       main.cpp
00003  @brief      Main file. This file uses the ADE120x drivers to configure the ADE1202
00004              Use AN-2034 in conjunction with this example and EVAL-ADE1202EBZ 
00005  @version    V0.0.1
00006  @author     ADI
00007  @date       October 2019
00008  
00009  Copyright (c) 2017-2019 Analog Devices, Inc. All Rights Reserved.
00010  
00011  This software is proprietary to Analog Devices, Inc. and its licensors.
00012  By using this software you agree to the terms of the associated
00013  Analog Devices Software License Agreement. 
00014 *****************************************************************************/
00015 
00016 
00017 /***Libraries***/
00018 #include "ADE120x.h"
00019 #include "mbed.h"
00020 
00021 #define ADC_PGA ADCPGA_10   /* Choose ADCPGA_1, ADCPGA_2, ADCPGA_5, ADCPGA_10 */
00022 #define V_Gain  0.003832    /* This is the gain of the Resistor divider network 
00023                                before the input to the ADE1202 */
00024 
00025 //Platform IOs and Timers
00026 InterruptIn DOUT1(SDP_GPIO_0);    /* Init pin connected to U0, DOUT0 on EVAL-ADE1202EBZ*/
00027 InterruptIn DOUT2(SDP_GPIO_1);    /* Init pin connected to U0, DOUT1 on EVAL-ADE1202EBZ*/
00028 
00029 
00030 /* ADE1202 Class defined with SPI */
00031 ADE120x ade1202(SDP_SPI_MOSI, SDP_SPI_MISO, SDP_SPI_SCK, SDP_SPI_CS_A);    //MOSI, MISO, SCLK, /CS
00032 
00033 /* Initialize the serial port */
00034 Serial pc(USBTX, USBRX);
00035 
00036 uint8_t DOUT1_Status, DOUT2_Status = 0;
00037 float voltage = 0.0; 
00038 
00039 /* Interrupt Handlers for DOUT1 and DOUT2 */
00040 void DOUT1_Int()
00041 {
00042     DOUT1_Status = 1;   
00043 }
00044 
00045 void DOUT2_Int()
00046 {
00047     DOUT2_Status = 1;   
00048 }
00049 
00050 /* main() runs in its own thread in the OS */
00051 int main()
00052 {
00053     uint8_t addr = 0x0;                /* address of ADE120x device from 0x0 to 0xF*/
00054     uint16_t filter_val;
00055     uint16_t device_id;
00056     THRESHCfg_Type thresh_cfg;
00057     PLOADCfg_Type plload_cfg;
00058     EnergyMtrCfg_Type enrgymtr_cfg;
00059     RegisterData_Type reg_data[20];     /* Buffer to read back register settings 
00060                                           after configuration to confirm values */
00061     
00062     /* Intialize interrupts to fire on rising edge*/
00063     DOUT1.rise(&DOUT1_Int);
00064     DOUT2.rise(&DOUT2_Int);
00065     
00066     /* Initialize Uart with baud rate of 230400*/
00067     pc.baud(230400);
00068     pc.printf("ADE1202 Demo Application \n\r");
00069     
00070     /* Reset the ADE1202 */
00071     ade1202.Reset(addr);
00072     wait_us(100000);
00073 
00074     /* Read back device ID */
00075     device_id = ade1202.GetDevID(addr);
00076     if((device_id & DEV_ADE1202) == DEV_ADE1202)
00077         pc.printf("Device is ADE1202\n");
00078     else
00079         pc.printf("Device is ADE1201\n");
00080 
00081     /* Print silicon revision and device address */
00082     pc.printf("Rev ID is: %d * Device Address is %d \n", ADE120x_RevId(device_id), ADE120x_ChipAddr(device_id));
00083 
00084     /* Unlock the device for programming */
00085     ade1202.UnLock(addr);
00086     
00087     /* Wait some time after unlocking device */
00088     wait_us(1000);
00089     
00090     /* Configure threshold registers and Modes using library function */
00091     thresh_cfg.BIN_HighThresh = 21.7;         /* 22V */
00092     thresh_cfg.BIN_LowThresh = 11.5;          /* 12V */
00093     thresh_cfg.WARNA_HighThresh = 26.1;       /* 26V */
00094     thresh_cfg.WARNA_LowThresh = 26.1;        /* 26V */
00095     thresh_cfg.WARNB_HighThresh = 17.4;       /* 17V */
00096     thresh_cfg.WARNB_LowThresh = 11.5;        /* 11V */
00097     thresh_cfg.WARNC_HighThresh = 5.8;        /* 5V */
00098     thresh_cfg.WARNC_LowThresh = 5.8;         /* 5V */
00099     thresh_cfg.BIN_Mode = Mode_Hysteretic;
00100     thresh_cfg.WARNA_Mode = Mode_Greater;
00101     thresh_cfg.WARNB_Mode = Mode_Inbetween;
00102     thresh_cfg.WARNC_Mode = Mode_LessEqual;
00103     thresh_cfg.ADCPga = 10;
00104     thresh_cfg.VGain = V_Gain;
00105     ade1202.ThresholdCfg(addr, &thresh_cfg);
00106 
00107     /* Step 3: Configure filter values for 3 ms*/
00108     /* FilterLength = GlitchWidth(us)/(20us)*/
00109     ade1202.WriteReg(addr, REG_BIN_FILTER, 0x8096);
00110     /* 5ms filter for WARNx */
00111     ade1202.WriteReg(addr, REG_WARNA_FILTER, 0x80FA);
00112     ade1202.WriteReg(addr, REG_WARNB_FILTER, 0x80FA);
00113     ade1202.WriteReg(addr, REG_WARNC_FILTER, 0x80FA);
00114     
00115     /* Step 4: Configure programmable load */
00116     plload_cfg.ADCPga = 10;
00117     plload_cfg.enable = CH1_Enable|CH2_Enable; /*Enable for both channels */
00118     plload_cfg.HighCurrent = 30;    /* 16mA */
00119     plload_cfg.HighTime = 1000;     /* in us */
00120     plload_cfg.LowCurrent = 1;      /* 3 mA */
00121     plload_cfg.mode = LOW_IDLE;
00122     plload_cfg.VGain = V_Gain;
00123     plload_cfg.VoltThresh = 3.84;
00124     ade1202.ProgrammableLoadCfg(addr, &plload_cfg);
00125     
00126     /* Step 5: Configure Energy Monitor */
00127     enrgymtr_cfg.ADCPga = 10;
00128     enrgymtr_cfg.enable = 0;
00129     enrgymtr_cfg.VGain = V_Gain;
00130     enrgymtr_cfg.SampleRate = 20e-6;        /* 10us on ADE1201, 20us on ADE1202 */
00131     enrgymtr_cfg.WorkingVoltage = 250;
00132     enrgymtr_cfg.PulseMagnitude = 16;       /* 16mA */
00133     enrgymtr_cfg.PulseTime = 3;             /* 3ms */
00134     enrgymtr_cfg.Cooldown_Decr = 5;
00135     enrgymtr_cfg.Cooldown_TimeStep = COOLDOWN_TS_10us;
00136     enrgymtr_cfg.Ov_Scale = OV_SCALE_1;
00137     enrgymtr_cfg.Cooldown_Sec = 5;
00138     ade1202.EnergyMtrCfg(addr, &enrgymtr_cfg);
00139     
00140     /* Set ADC PGA */
00141     ade1202.SetPgaGain(addr, ADCPGA_10);
00142     
00143     /* Lock device after configuring registers */
00144     ade1202.Lock(addr);//add 100us delay
00145     wait_us(100);
00146     ade1202.ClearIntStatus(addr, INTSRC_ALL);
00147     
00148     /* Read back and print all register settings after configuration to confirm they are correct*/
00149     ade1202.GetRegisterData(addr, (RegisterData_Type*)reg_data);
00150     for(int i = 0; i<20;i++)
00151         printf("0x%x , 0x%x \n", reg_data[i].reg_addr, reg_data[i].reg_data);
00152     
00153     
00154     /* Enter main program loop and wait for threshold events */
00155     while(1) {
00156         uint32_t reg_data, status = 0;
00157         status = ade1202.GetIntStatus(addr); /* Check status register */
00158         if(DOUT1_Status)
00159         {
00160             /* Interrupt detected on DOUT1 */
00161             DOUT1_Status = 0;
00162             reg_data = ade1202.ReadADC(addr, ADC_RAW);
00163             pc.printf("DOUT1 Interrupt detected! ");
00164             pc.printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
00165         }
00166         if(DOUT2_Status)
00167         {
00168             /* Interrupt detected on DOUT2 */
00169             DOUT2_Status = 0;
00170             reg_data = ade1202.ReadADC(addr, ADC_RAW);
00171             pc.printf("DOUT2 Interrupt detected! ");
00172             pc.printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
00173         }
00174         if(status != 0)
00175         {
00176             if((status & INTSRC_WARNA1) == INTSRC_WARNA1)
00177             {
00178                 reg_data = ade1202.ReadADC(addr, ADC_RAW);
00179                 pc.printf("WARNA Interrupt detected! Voltage > 22V ");
00180                 pc.printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
00181             }
00182             if((status & INTSRC_WARNB1) == INTSRC_WARNB1)
00183             {
00184                 reg_data = ade1202.ReadADC(addr, ADC_RAW);
00185                 pc.printf("WARNB Interrupt detected! Voltage in between 11V and 15V ");
00186                 pc.printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
00187             }
00188             if((status & INTSRC_WARNC1) == INTSRC_WARNC1)
00189             {
00190                 reg_data = ade1202.ReadADC(addr, ADC_RAW);
00191                 pc.printf("WARNC Interrupt detected! Voltage below 5V");
00192                 pc.printf("Status: 0x%x , Voltage: %f \n", status, ade1202.ADCCode2Volt(reg_data, ADC_PGA, V_Gain));
00193             }
00194             ade1202.ClearIntStatus(addr, INTSRC_ALL);
00195         }
00196     }
00197     return 0;
00198 }