Micheál Lambe
/
EVAL-ADE120x
ADE1202 Example proegram using ADE120x library files
Diff: main.cpp
- Revision:
- 2:2ebdd709cec0
- Parent:
- 1:cff2074f52d5
- Child:
- 3:8024f7ba736c
--- a/main.cpp Mon Jul 29 23:59:40 2019 +0000 +++ b/main.cpp Thu Oct 03 15:06:13 2019 +0000 @@ -1,35 +1,35 @@ /* Copyright (c) 2019 Analog Devices, Inc. All rights reserved. -Redistribution and use in source and binary forms, with or without modification, +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, + - 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. + - 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 + - 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 + - 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 + - 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 + - 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, +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 @@ -37,90 +37,182 @@ */ /***Libraries***/ -#include <stdio.h> /*Standard Library*/ -#include "mbed.h" /*mbed header file*/ +#include "ADE120x.h" +#include "mbed.h" + +#define ADC_PGA ADCPGA_10 /* Choose ADCPGA_1, ADCPGA_2, ADCPGA_5, ADCPGA_10 */ +#define V_Gain 0.003832 /* This is the gain of the Resistor divider network + before the input to the ADE1202 */ -/***Defines for UART Protocol***/ -#define BAUD_RATE 115200 /*Baud Rate*/ +//Platform IOs and Timers +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 -/*Configure and instantiate UART protocol - and baud rate*/ -Serial port(USBTX, USBRX, BAUD_RATE); - -/***Function Declarations***/ +/* Initialize the serial port */ +Serial pc(USBTX, USBRX); +uint8_t DOUT1_Status, DOUT2_Status = 0; -/*Print Information about Program's purpose*/ -static void print_title(void); +/* Interrupt Handlers for DOUT1 and DOUT2 */ +void DOUT1_Int() +{ + DOUT1_Status = 1; +} -/*Print Instructions on how to use Program*/ -static void print_prompt(void); +void DOUT2_Int() +{ + DOUT2_Status = 1; +} +/* main() runs in its own thread in the OS */ int main() { - uint8_t user_command; /*User input variable*/ - uint8_t connected = 1; /*Initialize SPI*/ + 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]; + + /* Intialize interrupts */ + DOUT1.rise(&DOUT1_Int); + DOUT2.rise(&DOUT2_Int); + + /* Initialize Uart with baud rate of 230400*/ + pc.baud(230400); + pc.printf("ADE1202 Demo Application \n\r"); + + /* Reset the ADE1202 */ + ade1202.Reset(addr); + wait_us(100000); - print_title(); + /* Read back device ID */ + device_id = ade1202.GetDevID(addr); + if((device_id & DEV_ADE1202) == DEV_ADE1202) + pc.printf("Device is ADE1202\n"); + else + pc.printf("Device is ADE1201\n"); + + /* Print silicon revision and device address */ + pc.printf("Rev ID is: %d * Device Address is %d \n", ADE120x_RevId(device_id), ADE120x_ChipAddr(device_id)); + + /* Unlock the device for programming */ + ade1202.UnLock(addr); + + /* Wait some time after unlocking device */ + wait_us(1000); + + /* Configure threshold registers and Modes using library function */ + thresh_cfg.BIN_HighThresh = 21.7; /* 22V */ + thresh_cfg.BIN_LowThresh = 11.5; /* 12V */ + thresh_cfg.WARNA_HighThresh = 26.1; /* 26V */ + thresh_cfg.WARNA_LowThresh = 26.1; /* 26V */ + thresh_cfg.WARNB_HighThresh = 17.4; /* 17V */ + thresh_cfg.WARNB_LowThresh = 11.5; /* 11V */ + thresh_cfg.WARNC_HighThresh = 5.8; /* 5V */ + thresh_cfg.WARNC_LowThresh = 5.8; /* 5V */ + thresh_cfg.BIN_Mode = Mode_Hysteretic; + thresh_cfg.WARNA_Mode = Mode_Greater; + thresh_cfg.WARNB_Mode = Mode_Inbetween; + thresh_cfg.WARNC_Mode = Mode_LessEqual; + thresh_cfg.ADCPga = 10; + thresh_cfg.VGain = V_Gain; + ade1202.ThresholdCfg(addr, &thresh_cfg); - while(connected == 1) { - print_prompt(); - port.scanf("%2d", (int *) &user_command); - switch (user_command) { - case 1: - port.printf("Case 1\n\n"); - break; - - case 2: - port.printf("Case 2\n\n"); - //menu_item_1_conversion_read(dev); - break; - - default: - port.printf(" ***Illegal Entry****\n\n"); - break; - + /* Step 3: Configure filter values for 3 ms*/ + /* FilterLength = GlitchWidth(us)/(20us)*/ + ade1202.WriteReg(addr, REG_BIN_FILTER, 0x8096); + /* 5ms filter for WARNx */ + ade1202.WriteReg(addr, REG_WARNA_FILTER, 0x80FA); + ade1202.WriteReg(addr, REG_WARNB_FILTER, 0x80FA); + ade1202.WriteReg(addr, REG_WARNC_FILTER, 0x80FA); + + /* Step 4: Configure programmable load */ + plload_cfg.ADCPga = 10; + plload_cfg.enable = CH1_Enable|CH2_Enable; /*Enable for both channels */ + plload_cfg.HighCurrent = 30; /* 16mA */ + plload_cfg.HighTime = 1000; /* in us */ + plload_cfg.LowCurrent = 1; /* 3 mA */ + plload_cfg.mode = LOW_IDLE; + plload_cfg.VGain = V_Gain; + plload_cfg.VoltThresh = 3.84; + ade1202.ProgrammableLoadCfg(addr, &plload_cfg); + + /* Step 5: Configure Energy Monitor */ + enrgymtr_cfg.ADCPga = 10; + enrgymtr_cfg.enable = 0; + enrgymtr_cfg.VGain = V_Gain; + enrgymtr_cfg.SampleRate = 20e-6; /* 10us on ADE1201, 20us on ADE1202 */ + enrgymtr_cfg.WorkingVoltage = 250; + enrgymtr_cfg.PulseMagnitude = 16; /* 16mA */ + enrgymtr_cfg.PulseTime = 3; /* 3ms */ + enrgymtr_cfg.Cooldown_Decr = 5; + enrgymtr_cfg.Cooldown_TimeStep = COOLDOWN_TS_10us; + enrgymtr_cfg.Ov_Scale = OV_SCALE_1; + enrgymtr_cfg.Cooldown_Sec = 5; + ade1202.EnergyMtrCfg(addr, &enrgymtr_cfg); + + /* Set ADC PGA */ + ade1202.SetPgaGain(addr, ADCPGA_10); + + /* Lock device after configuring registers */ + ade1202.Lock(addr);//add 100us delay + wait_us(100); + ade1202.ClearIntStatus(addr, INTSRC_ALL); + + /* Read back and print all register settings after configuration */ + 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); + + + /* Enter main program loop and wait for threshold events */ + while(1) { + uint32_t reg_data, status = 0; + status = ade1202.GetIntStatus(addr); /* Check status register */ + if(DOUT1_Status) + { + 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)); + } + if(DOUT2_Status) + { + 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)); + } + 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)); + } + 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)); + } + 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)); + } + ade1202.ClearIntStatus(addr, INTSRC_ALL); } } return 0; -} - -/***Function Definitions***/ - - -/*Function to print the title block when program first starts. - Parameters: None - Return Value: None*/ -void print_title() -{ - port.printf("\n*****************************************************************\n"); - port.printf("* EVAL-AD1234 Demonstration Program *\n"); - port.printf("* *\n"); - port.printf("* This program demonstrates how to measure strain gauge or *\n"); - port.printf("* other form of a Wheatstone bridge sensors with the AD1234 *\n"); - port.printf("* *\n"); - port.printf("* *\n"); - port.printf("* Set the baud rate to 115200 and select the newline terminator.*\n"); - port.printf("* *\n"); - port.printf("*****************************************************************\n"); -} - -/*Function to Print Command Summary - Parameters: None - Return Value: None */ -static void print_prompt() { - port.printf("Command Summary\n"); - port.printf(" 1 - Select ADC\n"); - port.printf(" 2 - Select Power Mode\n"); - port.printf(" x - Put More Menu Items Here\n"); -} - -void menu_1_function1() -{ - port.printf("In function 1\n"); -} - -void menu_2_function2() -{ - port.printf("In function 2\n"); -} - +} \ No newline at end of file