Three-Phase Power Meter Software Design Reference Manual Freescale Semiconductor, Inc.
Transcription
Three-Phase Power Meter Software Design Reference Manual Freescale Semiconductor, Inc.
Freescale Semiconductor, Inc. Document Number: DRM145 Rev. 0, 03/2014 Design Reference Manual Three-Phase Power Meter Software Design Reference Manual by: Albert Chen and Shawn Shi 1 Overview The Freescale three-phase power meter reference design was designed in compliance with the China state grid corporation Q/GDW 354-2012 (functional specification for smart electricity meters), Q/GDW 356-2012 (type specification for smart poly-phase electricity meters) standard. The reference design aims to shorten time to market for power meter customers and partners, and it can be tailored to a customer’s unique powering system needs. Contents 1 Overview ...................................................................................................................... 1 1.1 Hardware develop environment ...................................................................... 2 1.2 Software develop environment ....................................................................... 2 1.3 2 Key features ................................................................................................... 2 System architecture ...................................................................................................... 3 2.1 Software architecture...................................................................................... 5 2.2 Core modules ................................................................................................. 5 2.3 3 Source code organization................................................................................ 8 Install the software ..................................................................................................... 10 3.1 Compile and flash in KL36........................................................................... 10 3.2 Compile and flash in KM14 ......................................................................... 10 3.3 4 4.1 Macro USED_INTERNAL_TEMP .............................................................. 12 4.2 Macro KM14_DEBUG_PHASE .................................................................. 13 4.3 Macro PULSE_NUM ................................................................................... 13 4.4 Macro PULSE_WIDTH ............................................................................... 13 4.5 Macro ESD_TEST_KL36 ............................................................................ 13 4.6 Macro ESD_TEST_KM14 ........................................................................... 13 4.7 Macro SPI_SEND_FREQ ............................................................................ 14 4.8 Macro RTC_COMP_ENABLE .................................................................... 14 4.9 Macro RTC_COMP_INTERA ..................................................................... 14 4.10 Macro USED_EXTERNAL_VREF....................................................... 14 4.11 Macro KL36_FEI_MODE ..................................................................... 14 4.12 Macro COMM_PIN_OUTPUT_PULSE................................................ 14 4.13 Macro CAL_CURR and CAL_VOLT .................................................. 15 4.14 Macro I_MAX ...................................................................................... 15 4.15 Macro U_MAX ..................................................................................... 16 5 APIs exported for KM14 ............................................................................................ 17 5.1 Internal flash(NVM) operations .................................................................... 17 5.2 SPI Master interface ..................................................................................... 18 5.3 RTC calibration interface ............................................................................. 19 5.4 6 Meterlib ....................................................................................................... 19 APIs exported for KL36 ............................................................................................. 22 6.1 6.2 7 Run the demo ............................................................................................... 10 Application configurations ......................................................................................... 12 LCM operations ........................................................................................... 22 SPI Slave interface ....................................................................................... 23 Revision history ......................................................................................................... 23 © 2014 Freescale Semiconductor, Inc. ___________________________________________________________________ 1.1 Hardware develop environment • • • • Debugger tool: P&E multilink Test device: 3PH calibration instrument (Model No: KP-P2001-C) Initial Calibration Condition: Three-Phases, 220V/5A, initial angle is 0, pure resistance load. MCU: KM14 as metering; KL36 as control. 1.2 Software develop environment • • IDE: IAR6.5 OS support: No OS 1.3 Key features • • • • • • • • • • • • • • • • • 2 Voltage Range: 3 x 220V/380V Current range: 5(60)A Active Accuracy (-40C ~ 70C): 0.5S Reactive Accuracy (-40C ~ 70C): 2S RTC (-40C ~ 70C): 5ppm Energy of active; reactive; apparent Power of active; reactive; apparent RMS of voltage and current Line frequency measurement (for precision zero-cross detection) Current transformers sensing circuit implementation Low-power modes effectively implemented, including the use of the built-in RTC LCD display, 8x32 segments LEDs pulse outputs (kWh, kVARh) Temper detection and records Phase missing detection Calibration UI tools Communication I/F (Isolated IR; Isolated RS232/485) Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. 2 System architecture Figure 1. KM14 architecture The brushless DC (BLDC) motor is a rotating electric machine where the stator is a classical three-phase stator similar to an induction motor and the rotor has surface-mounted permanent magnets (see Figure 1). The motor can have more than one pole pair per phase. (The figure shows the motor with two pole pairs per phase.) The number of pole pairs per phase defines the ratio between the electrical revolution and the mechanical revolution. Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. 3 Figure 2. KM36 architecture 4 Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. 2.1 Software architecture Sample Phase Compensation Meter Lib Pulse out SPI Pulse out KL36 RS485/Irda LCD Figure 3. System architecture 2.2 Core modules 1. ZCD: Zero Cross Detect ZCD is used to calculate power line frequency. It uses a comparator (CMP) detect if voltage reaches a certain threshold. When CMP interrupt occurs, the trigger timer runs. The timer capture function will record the frequency. Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. 5 Figure 4. ZCD module 2. Sample AFE (SD ADC ) is used for sample current. SAR ADC is used for sample voltage. Compensation technology ensures that the voltage and current are sampled simultaneously. Figure 5. Sample module 3. Algorithm lib According to the sampled data, calculated the active/reactive/total energy, etc. Note 1. Input sampled value is 24 bits. Example: voltage is 16 bits, please convert to 24 bits as in: u24_sample = u24_sample << 8; The current sample value is 24 bits, and its value should not be changed. 2. The filter coefficient is based on a 1200 Hz sample rate, so data should be sent to meter lib with 1200 Hz. If you want to change the sample rate, use “Filter-Based Metering Algorithms Configuration Tool” to create new set of coefficients. 6 Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. Figure 6. Configuration tool Figure 7. Algorithm lib framework 4. RTC compensation lib RTC output accuracy reaches 5PPM in full temperature zone. Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. 7 5. SPI between meter and control MCU KM14 informs KL36 power energy information, and KL36 may respond with useful information such as the button pressed. 6. Segment LCD show Show active power and reactive power values. Figure 8. LCD display 2.3 Source code organization MS3P4W\projects\: mk343ph\: KM34 meter IAR build, use the file project.eww to open project. kl36\: HMI IAR build, use the file iar\platinum\platinum.eww to open project. MS3P4W\src\: public.c, public.h: public functions and definitions comu.h: SPI communication protocol rtc_comp.c, rtc_ rtc_comp_table.c: RTC compensation process. common\: 8 system startup code Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. config\: compile options for flash cpu\: system public code drivers\: peripheral drivers in KM34 fraclib\: math lib freemaster\: freemaster lib support meterlib3ph\: core algorithm lib projects\ km34z128_3ph_meter\: main app appconfig.h: KM14 configuration config.c, config.h: save/read calibration data mk343ph.h: filter coefficient of meter lib mk343ph_local.c: main function nvm.c, nvm.h: flash operation freemaster_cfg.h: freemaster configuration projects\ local\: peripheral drivers misc.c: system init function spi_hal.c: spi interface test.c,test.h: peripheral test routines. MS3P4W \src\HMI\KL36\src\: KL36 HMI code common\: system startup code cpu\: system public code drivers\: peripheral drivers in KL36 freemaster\:freemaster lib support platforms\: platform specified files projects\platinum\: HMI code at24cxx .h, at24cxx.c: Microchip I2C 512KB EEPROM driver kbi.c: button driver. lcm.c: LCD high level driver. spi_slave.c: spi slave high level driver Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. 9 mag3110.h, mag3110.c: I2C mag3110 Three-Axis, Digital Magnetometer driver platinum.c: main loop rs485_parse.c: RS485 high level driver 3 Install the software 3.1 Compile and flash in KL36 1. 2. 3. 4. 5. Open MS3P4W\projects\mk343ph\project.eww by IAR. Click IAR menu project->build. Connected PE with J27 on board. Power on board. Click IAR menu project->Download and Debug. 3.2 Compile and flash in KM14 1. 2. 3. 4. 5. Open MS3P4W\projects\KL36\iar\platinum\platinum_freedom.ewp. Click menu project->build. Connected PE with J32 on board. Power on board. Click IAR menu project->Download and Debug. 3.3 Run the demo 1. If this meter is not calibrated: Use standard calibration instrument and set parameters: 220V/5A, initial angle is 0, pure resistance load. After power on, wait about 40 seconds until the calibration process is complete. Power off and power on device after calibration. 10 Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. Start System Init Sample Meter Lib Compute Energy Update Pulsing information N Computation Times > 140 Y Write Correction Data to Flash Figure 9. Calibration process 2. If this meter is calibrated then the meter test can be performed. Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. 11 Start System Init Sample Phase Compensation Meter Lib Compute Energy Update Pulsing information Figure 10. Meter running 4 Application configurations Configurations are stored primarily in MS3P4W\src\public.h file. Macros whose name includes the string “KL36” are used for KL36 MCU. Others are used for KM14 MCU. 4.1 Macro USED_INTERNAL_TEMP If defined, the meter uses an internal temperature sensor to calibrate the RTC. If not defined, the meter uses an external temperature sensor. Default: not defined. 12 Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. 4.2 Macro KM14_DEBUG_PHASE If defined, the line frequency and external temperature sensor cannot be measured. During the developing phase, define this macro. PTE6 and PTE7 are multiplexes with a debug pin. PTE6: used to measure line frequency. PTE7: used as an external temperature sensor. Figure 11. Signal multiplexing Default: not defined. 4.3 Macro PULSE_NUM Constant pulse number. Default is 400. 4.4 Macro PULSE_WIDTH Pulse width. Unit is µs. Default is 80000 (80ms). 4.5 Macro ESD_TEST_KL36 If defined, set the reset pin on KL36 to GPIO output; update the LCD every 1s indicated the kl36 is alive. Default: defined. 4.6 Macro ESD_TEST_KM14 If defined, set the reset pin on KM14 to GPIO output; LED “Mfunc” on board toggle every 1s indicate photon is alive. Default: defined. Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. 13 4.7 Macro SPI_SEND_FREQ SPI communicated frequency. Default is 2, (communicate 2 times 1 second). 4.8 Macro RTC_COMP_ENABLE If defined, enable RTC compensation algorithm. Default: defined. 4.9 Macro RTC_COMP_INTERA If defined, enable RTC interact calibration, and RS485 port will output the ADC value of temp sensor. Default: not defined. 4.10 Macro USED_EXTERNAL_VREF If defined, the ADC used an external 1.2V reference. If not defined, the ADC used an internal 1.2V reference. 4.11 Macro KL36_FEI_MODE If defined, KL36 used an internal OSC as the clock source. If not defined, KL36 used an external 8 MHz clock source. Default: not defined. Note If an internal OSC (Macro KL36_FEI_MODE defined) is used, the pulse output from KL36 is not precise because the internal OSC is not precise. 4.12 Macro COMM_PIN_OUTPUT_PULSE If defined, pta5/pta6 on photon is used as active/reactive energy pulse out signal. ptc0/ptc1 on kl36 as pulse out pins. Photon calculated the energy, when need toggle pulse, photon informs KL36 by 2 pins (pta5/pta6, ptc0/ptc1). Default: not defined. 14 Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. Note If defined KL36_FEI_MODE, should enable this macro because KL36 internal OSC is not precise, enable macro COMM_PIN_OUTPUT_PULSE, KL36 will product precise pulse signal. 4.13 Macro CAL_CURR and CAL_VOLT #define CAL_CURR #define CAL_VOLT 5.0 220.0 The default calibration environment is used 220V, 5.0A. The preset calibration angle is 0 degree. 4.14 Macro I_MAX #define I_MAX 110.0 Unit is Ampere. It used for meter lib. SD ADC(AFE) is used for sampling current. When PGA is off, the maximum differential input range is 500mV. so V+ max (IA_IN+) = 250mv. ( 250mV – (-250mV) = 500mV) Figure 12. Current sampling Sample resistance is 4.7 (like R56). U = I * R, suppose K is CT ratio. So Umax = 0.25V = (Imax * R/ K), Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. 15 Imax = 0.25 * K / R here K = 2000, R = 4.7, Imax = 0.25 * 2000 / 4.7 = 106 Left some margin, ~ 5%: 106 * (1 + 0.05) = 111A Used 110A. 4.15 Macro U_MAX #define U_MAX 800.0 Vin Unit is volts. Used for meter lib. Vadc Figure 13. Voltage sample The point Vadc is connected with P_TP3. Rt = P_R1 + P_R2 + P_R4 + P_R5 + P_R6 + P_R7 = 600K Then: Current = ( Vin – Vadc) / Rt = (Vadc – Vref/2) / R57 So: Vadc = Vref/2 + (Vin * R57 / Rt) Vadc max is Vref, Vref = 1.2V So: Vin(max) = 0.6 * Rt / R57 = 0.6 * 600K / 470 = 766V 16 Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. Left some margin, ~ 5%: 766V * ( 1 + 0.05) = 804V Used 800V. 5 APIs exported for KM14 For peripheral access interface, see the content named “photon_refman.chm.” This manual will only introduce the application level interface. 5.1 Internal flash(NVM) operations NVM — Nonvolatile memory. All APIs related to NVM are defined in src\projects\km34z128_3ph_meter\nvm.h. Note 1. The flash memory can only move bits from the '1' state (erased) to the '0' state (programmed). Only the erase operation restores bits from '0' to'1'; bits cannot be programmed from a '0' to a '1'. 2. Sector size of 1 KB. The smallest portion of the program flash memory is 1KB. 3. When performing the erase operation, the start address is 1KB aligned. Figure 14. KM14 System Memory Map Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. 17 We set the location of the meter parameters to 0x0001f000. #pragma location = 0x0001f000 const tCONFIG_FLASH_DATA3PH nvmcfg3Ph;/* this variable is stored in flash */ 1. NVM_MemRead int NVM_MemRead (void *pSrc, void *pDst, int size) Read array from NVM memory. 2. NVM_MemSave int NVM_MemSave (void *pDst, void *pSrc, int size) Write array into NVM memory. 3. NVM_SectorErase void NVM_SectorErase (void *pDst) Erase sector in NVM memory. 4. NVM_W32Read int NVM_W32Read (unsigned long *pSrc, unsigned long *pVal) Read long word from NVM memory. 5. NVM_W32Write int NVM_W32Write (unsigned long *pDst, unsigned long val) Write long word into NVM memory. 5.2 SPI Master interface SPI module is used to communicate between KM14 and KL36. KM14 acts as SPI master. 1. spi_poll_send void spi_poll_send(uint16* buf, uint16 len) Send bufffer content to KL36. 2. spi_poll_recv 18 Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. void spi_poll_recv(uint16* buf, uint16 len) Receive something from KL36. 5.3 RTC calibration interface 1. cal_ppm int32 cal_ppm(double diff) Translate seconds difference within one day to PPM. [in] diff: 1 day delta seconds, read from a meter calibration device. Return: PPM value 2. cal_rtc int8 cal_rtc(int32 ppm, uint8 *pfrac) Translate PPM value to register setting values. [in] ppm: ppm value [out] pfrac: frac value Return : int_part value. Call IRTC_UpdateFineCompVal(int_part, frac) To calibrate the RTC. 3. cal_rtc_ext int8 cal_rtc_ext(double diff, uint8 *pfrac) Same as cal_rtc except input value is one day delta seconds. 4. get_temp int8 get_temp(uint16 adc_temp) Calculate the temperature according to sampled ADC values. 5.4 Meterlib Requirements: A. Sampled values are 24-bit data format. Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. 19 If sampled values are not 24-bit data format, convert them to 24-bit. For example, the sampled value for voltage is 16-bit as in: uint16 adc_voltage; define a new variable as in: Frac32 u24_samplePh2, and convert it to 24-bit as in: u24_samplePh2 = (Frac32)temp16 << 8. Lastly, the variable u24_samplePh2 can be used to meter lib. B. Filter coefficient is based on 1200Hz. The function call frequency for auxcalc_callback is 1200 Hz. To change to another frequency, recreate the filter coefficient table. 5.4.1 Interfaces for meterlib 1. KWH_PULS_NUM(x): represents 50% energy of one active pulse #define KWH_PULS_NUM(x) FRAC48(((5e2/(x))/(U_MAX*I_MAX/3600/CALCFREQ))) X: constant pulse number. Default is 400. 2. KVARH_PULS_NUM(x): represent the 50% energy of one reactive pulse #define KVARH_PULS_NUM(x) FRAC48(((5e2/(x))/(U_MAX*I_MAX/3600/CALCFREQ))) Similar to KWH_PULS_NUM(x). 3. do_filter: Calculate the energy. The frequency of this function call is 1200 Hz. void do_filter(tMETERLIB3PH_DATA *p, Frac24 u1Q, Frac24 i1Q, Frac24 u2Q, Frac24 i2Q, Frac24 u3Q, Frac24 i3Q, Frac32 *whCnt, Frac32 *varCnt, Frac64 whRes, Frac64 varRes); Input: tMETERLIB3PH_DATA *p: includes the filter coefficient. u1Q, i1Q, …: the calibrated sample result. whRes, varRes: pulse resolution Input, Output: whCnt, varCnt: output pulse number. They would be changed if energy reaches the threshold. 20 Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. Example: do_filter(mlib, -L_mul (u1 – u1_offset, u1_gain), -L_mul (i1 – i1_offset, i1_gain), -L_mul (u2 – u2_offset, u2_gain), -L_mul (i2 – i2_offset, i2_gain), -L_mul (u3 – u3_offset, u3_gain), -L_mul (i3 – i3_offset, i3_gain), &wh_cnt, &varh_cnt, KWH_PULS_NUM(400), KVARH_PULS_NUM(400)); void do_filter(tMETERLIB3PH_DATA *p, Frac24 u1Q, Frac24 i1Q, Frac24 u2Q, Frac24 i2Q, Frac24 u3Q, Frac24 i3Q, Frac32 *whCnt, Frac32 *varCnt, Frac64 whRes, Frac64 varRes) { METERLIB3PH_RemoveDcBias(p, u1Q, i1Q, u2Q, i2Q, u3Q, i3Q); METERLIB3PH_CalcWattHours(p, whCnt, whRes); METERLIB3PH_CalcVarHours( p, varCnt, varRes); METERLIB3PH_CalcAuxiliary(p); } 5.4.2 When to produce the pulse After call do_filter, the mlib.Wh.pulsOut and mlib.VARh.pulsOut would be changed if energy reaches the threshold. Save the last value (last_pulseOut) of pulsOut and use the following algorithm: if (last_pulseOut!= mlib.Wh.pulsOut) { if (mlib.Wh.pulsOut) { product a pulse; //time to product a pulse } Last_pulseOut = mlib.Wh.pulsOut; } Use another timer to count the pulse duty. 4. METERLIB3PH_ReadResultsPhx Return auxiliary variables: IRMS, URMS, P, Q and S. It is OK to call this function anytime. Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. 21 6 APIs exported for KL36 Figure 15. LCD example 6.1 LCM operations LCD Control Module (LCM). This solution uses ht16c23 controller with I2C interface. 1. #define I2C_LCM_ADDR 0x3e /* 7 bit I2C slave address */ 7 bit LCM I2C slave addresses. 2. lcm_i2c0_cmd void lcm_i2c0_cmd(uint8 reg, uint8 val) Write LCM register. 3. lcm_i2c0_write void lcm_i2c0_write(uint8* data, uint8 len) 22 Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. Write data to the LCM buffer. 4. lcm_i2c0_write_mem void lcm_i2c0_write_mem(uint8 reg, uint8 val) Write value into specified LCM memory. 5. lcm_i2c0_read_mem uint8 lcm_i2c0_write_mem(uint8 reg) Read value from specified LCM memory. 6. lcmd_show_icon void lcmd_show_icon(enum segment_name icon, uint8 show) Show/hide icon. 7. lcm_show_num void lcm_show_num(uint8 num, uint8 pos) Show num from 1-based pos. One example: See figure 7-1, there are 19 number locations. If you want to show number 23 from position 2, you could call: lcm_show_num(23, 2); 6.2 SPI Slave interface 1. get_command: get command from master. uint8 get_command(void) 2. SPI0_ReadW: read one 16bit content from master. 3. SPI0_writeW: write one 16bit content to master. 7 Revision history Table 1. Revision history Revision number Date Substantial changes 0 03/2014 Initial release Three-Phase Power Meter Software Design Reference Manual, Rev. 0, 03/2014 Freescale Semiconductor, Inc. 23 How to Reach Us: Home Page: freescale.com Web Support: freescale.com/support Information in this document is provided solely to enable system and software implementers to use Freescale products. There are no express or implied copyright licenses granted hereunder to design or fabricate any integrated circuits based on the information in this document. Freescale reserves the right to make changes without further notice to any products herein. Freescale makes no warranty, representation, or guarantee regarding the suitability of its products for any particular purpose, nor does Freescale assume any liability arising out of the application or use of any product or circuit, and specifically disclaims any and all liability, including without limitation consequential or incidental damages. “Typical” parameters that may be provided in Freescale data sheets and/or specifications can and do vary in different applications, and actual performance may vary over time. All operating parameters, including “typicals,” must be validated for each customer application by customer’s technical experts. Freescale does not convey any license under its patent rights nor the rights of others. Freescale sells products pursuant to standard terms and conditions of sale, which can be found at the following address: freescale.com/SalesTermsandConditions. Freescale, the Freescale logo, and Kinetis are trademarks of Freescale Semiconductor, Inc., Reg. U.S. Pat. & Tm. Off. All other product or service names are the property of their respective owners. ARM and Cortex are registered trademarks of ARM Limited (or its subsidiaries) in the EU and/or elsewhere. All rights reserved. © 2014 Freescale Semiconductor, Inc. Document Number: DRM145 Rev. 0 03/2014