ADE120x Library Files
Diff: ADE120x.cpp
- Revision:
- 2:f9a986799375
- Parent:
- 1:2eb9d6296ec3
- Child:
- 3:6c708642886d
--- a/ADE120x.cpp Thu Oct 03 15:05:59 2019 +0000 +++ b/ADE120x.cpp Tue Oct 15 12:06:51 2019 +0000 @@ -14,15 +14,6 @@ **/ #include "ADE120x.h" - -/*! \mainpage ADE120x Library Introduction - * - * # Introduction - * - * The documentation is for ADE120x library and examples. - * TBD - */ - /* Constructor for ADE120x; intializes the SPI interface only */ ADE120x::ADE120x( PinName mosi,PinName miso,PinName sclk,PinName cs) : spi_(mosi, miso, sclk), nCS_(cs) { @@ -33,11 +24,11 @@ } /** - @brief Write to ADE120x register through SPI. - @param addr: The address of the ADE120x device. + @brief Write to ADE120x register through SPI. + @param addr: The address of the ADE120x device. @param reg_addr: register address @param data: register data - @return received data. + @return received data. **/ void ADE120x::WriteReg(uint8_t addr, uint32_t reg_addr, uint32_t data) { @@ -51,10 +42,10 @@ nCS_ = 1; } /** - @brief Read ADE120x register through SPI. - @param addr: The address of the ADE120x device. + @brief Read ADE120x register through SPI. + @param addr: The address of the ADE120x device. @param reg_addr: register address - @return received data. + @return received data. **/ uint32_t ADE120x::ReadReg(uint8_t addr, uint32_t reg_addr) { @@ -81,8 +72,8 @@ UnLock(addr); WriteReg(addr, REG_CTRL, 0x0010); //wait until RSTDONE bit is set - while ((ReadReg(addr, REG_INT_STATUS) & INTSRC_RSTDONE) == 0) - { } + while ((ReadReg(addr, REG_INT_STATUS) & INTSRC_RSTDONE) == 0) + { } return 0; } @@ -120,20 +111,21 @@ } /** - @brief Configure interrupt. - @param addr: the address of the ADE120x device. + @brief Configure interrupt. + @param addr: the address of the ADE120x device. @param IntSrcSel: Interrupt source to enable. - @return Status. + @return Status. **/ void ADE120x::SetInt(uint8_t addr, uint16_t IntSrcSel) { WriteReg(addr, REG_MASK, IntSrcSel); } + /** - @brief Clear interrupt status. - @param addr: the address of the ADE120x device. + @brief Clear interrupt status. + @param addr: the address of the ADE120x device. @param IntSrcSel: Interrupt source to clear. - @return Status. + @return Status. **/ void ADE120x::ClearIntStatus(uint8_t addr, uint16_t IntSrcSel) { @@ -153,11 +145,11 @@ } /** - @brief Configure ADC PGA gain. - @param addr: the address of the ADE120x device. + @brief Configure ADC PGA gain. + @param addr: the address of the ADE120x device. @param Gain: Select from the following ADCPGA_1, ADCPGA_2, ADCPGA_5, ADCPGA_10. - @return Status. + @return Status. **/ void ADE120x::SetPgaGain(uint8_t addr, uint16_t gain) { @@ -165,11 +157,12 @@ WriteReg(addr, REG_PGA_GAIN, gain); Lock(addr); } + /** - @brief Read ADC data. - @param addr: the address of the ADE120x device. + @brief Read ADC data. + @param addr: the address of the ADE120x device. @param src: ADC source, ADC_DECIMATOR to select decimator output. ADC_RAW for raw output. - @return Status. + @return Status. **/ uint8_t ADE120x::ReadADC(uint8_t addr, int8_t src) { @@ -181,43 +174,10 @@ return code; } - -/** - @brief Convert ADC Code to voltage. - @param ADCCode: ADC code. - @param ADCPga: the actual 1.82V reference voltage. - @param VOLTAGE_Gain: THe gain factor set by the external resistor divider network - @return Voltage in volt. -**/ -float ADE120x::ADCCode2Volt(uint32_t ADCCode, uint8_t ADCPga, float VOLTAGE_Gain) -{ - float tmp = 0.0; - float fVolt = 0.0; - tmp = (ADCCode&0xFF); - tmp = 1.25 * (tmp/255) / VOLTAGE_Gain; - switch(ADCPga) - { - case ADCPGA_1: - fVolt = tmp; - break; - case ADCPGA_2: - fVolt = tmp/2; - break; - case ADCPGA_5: - fVolt = tmp/5; - break; - case ADCPGA_10: - fVolt = tmp/10; - break; - default:break; - } - return fVolt; -} - /** @brief Set binary Threshold. @param addr: the address of the ADE120x device. - @param thresh: Threshold value. + @param thresh: Threshold value. @return none. **/ void ADE120x::SetBinaryThresh(uint8_t addr, uint16_t thresh) @@ -227,9 +187,9 @@ /** @brief Calculate threshold register code. - @param V_Thresh: Threshold value in volts. - @param ADCPga: PGA gain value - @param V_Gain: Gain of external voltage divider circuit + @param V_Thresh: Threshold value in volts. + @param ADCPga: PGA gain value + @param V_Gain: Gain of external voltage divider circuit @return 0. **/ uint8_t ADE120x::CalculateThreshCode(float V_Thresh, uint8_t ADCPga, float V_Gain) @@ -242,9 +202,9 @@ } /** - @brief Configure threshold fvoltage for BIN, WARNA, WARNB and WARNC. - @param addr: the address of the ADE120x device. - @param pCfg: Pointer to structure + @brief Configure threshold voltage for BIN, WARNA, WARNB and WARNC. + @param addr: the address of the ADE120x device. + @param pCfg: Pointer to structure @return 0. **/ uint8_t ADE120x::ThresholdCfg(uint8_t addr, THRESHCfg_Type *pCfg) @@ -275,10 +235,11 @@ tmp = ReadReg(addr, REG_BIN_CTRL); return 0; } + /** @brief Configure programmable load. - @param addr: the address of the ADE120x device. - @param pCfg: Pointer to structure + @param addr: the address of the ADE120x device. + @param pCfg: Pointer to structure @return 0. **/ uint8_t ADE120x::ProgrammableLoadCfg(uint8_t addr, PLOADCfg_Type *pCfg) @@ -312,8 +273,8 @@ /** @brief Configure Energy Meter. - @param addr: the address of the ADE120x device. - @param pCfg: Pointer to structure + @param addr: the address of the ADE120x device. + @param pCfg: Pointer to structure @return 0. **/ uint8_t ADE120x::EnergyMtrCfg(uint8_t addr, EnergyMtrCfg_Type *pCfg) @@ -375,52 +336,35 @@ wait_us(1000); } } + /** - @brief Configure Device with default settings. - @param addr: the address of the ADE120x device. - @return 0. + @brief Convert ADC Code to voltage. + @param ADCCode: ADC code. + @param ADCPga: the actual 1.82V reference voltage. + @param VOLTAGE_Gain: THe gain factor set by the external resistor divider network + @return Voltage in volt. **/ -uint8_t ADE120x::DefaultConfig(uint8_t addr) +float ADE120x::ADCCode2Volt(uint32_t ADCCode, uint8_t ADCPga, float VOLTAGE_Gain) { - /** Array defined for configuration ***/ - uint16_t config[] = { - //REG_CTRL, 0x04, - // REG_BIN_CTRL, 0x0000, - REG_BIN_THR, 0x757d, // fail - REG_WARNA_THR, 0xcccc, - REG_WARNB_THR, 0x5a88, - REG_WARNC_THR, 0x2d2d, - REG_BIN_FILTER, BIN_FILTER_VAL, //fail using this register to validate against unintended resets in the DOUT validate - REG_WARNA_FILTER, 0x80fa, - REG_WARNB_FILTER, 0x80fa, - REG_WARNC_FILTER, 0x80fa, - REG_MASK, 0x4000, - REG_PL_CTRL, 0x0000, - REG_PL_RISE_THR, 0x007d, // fail - REG_PL_LOW_CODE, 0x001e, - REG_PL_HIGH_CODE, 0x000f, // fail - REG_PL_HIGH_TIME, 0x0001, // fail - REG_EGY_MTR_CTRL, 0x0505, - REG_EGY_MTR_THR, 0x9ba3, - REG_PL_EN, 0xC000, //fail - REG_PGA_GAIN, ADCPGA_10 }; //fail - - uint8_t config_size = sizeof(config)/sizeof(*config); - uint8_t i = 0; - int resp; - wait_us(10000); - while(i<config_size-1) + float tmp = 0.0; + float fVolt = 0.0; + tmp = (ADCCode&0xFF); + tmp = 1.25 * (tmp/255) / VOLTAGE_Gain; + switch(ADCPga) { - WriteReg(addr, config[i], config[i+1]); - resp = ReadReg(addr, config[i]); - if (resp != config[i+1]) - { - printf("Register 0x%x write failed: Expected: 0x%x , Actual: 0x%x \r\n", config[i], config[i+1], resp); - return 255; + case ADCPGA_1: + fVolt = tmp; + break; + case ADCPGA_2: + fVolt = tmp/2; + break; + case ADCPGA_5: + fVolt = tmp/5; + break; + case ADCPGA_10: + fVolt = tmp/10; + break; + default:break; } - // printf("Register 0x%x write passed: Expected: 0x%x , Actual: 0x%x \r\n", config[i], config[i+1], resp); - i+=2; - wait_us(1000); - } - return 0; + return fVolt; }