AN2159 Application note
SPI Communication for comprehensive energy reading of the STPM01
Introduction
This application note describes how to read and process measured data using the STPM01 in peripheral mode along with the ST7 microcontroller as a 1-phase energy meter. The STPM01 is an energy meter ASSP (Application Specific Standard Product) IC designed to address a wide range of electricity metering requirements with built-in features, including signal conditioning and processing, data conversion, input/output signals, and voltage references. This device can operate as a single-chip standalone energy meter (1-phase meters) or as a peripheral in a microprocessor-based (1-phase or 3-phase) energy meter. It uses a Serial Peripheral Interface (SPI) bus to provide functions such as active (base and wide range), reactive, and apparent energy measurement, voltage regulator modules (VRMs), current regulator modules (IRMs), instantaneous voltage and current measurement, and line frequency readings. The SPI is configured and compiled using the C language, Cosmic Compiler for the ST evaluation board, STPMEVKIT. This board consists of a measurement module based on the STPM01 (see Figure 1) and a control module based on the ST7F2324 microcontroller (see Figure 2). The control board also includes the M41ST87 Real-Time Clock (RTC) chip, a 256Kbit SPI bus EEPROM, and a dedicated, 32-character alphanumeric LCD display with an on-glass driver. As a standalone, 1-phase energy meter, the STPM01 outputs a pulse train signal which has a frequency proportional to the active power used (i.e., to drive a stepper motor). As a peripheral mode, 1- or 3-phase, microprocessor-based energy meter, the STPM01 reads measured data at fixed time intervals from internal device registers and sends it to the microcontroller.
April 2006
Rev 2
1/26
www.st .com
AN2159 Figure 1. Measure module block diagram
N P
Stepper Counter Connector Current Sensor SPI Connector Current Sensor SPI
STPM01
Voltage Sensor LEDs AC to DC Capacitive Power Supply
AI11148
Figure 2.
Control module block diagram
SPI Connector for Measurement Module 5V
Push-buttons
M95256 (EEPROM)
LCD Reset Push-button
SPI
ST7F2324
Tamper Push-button I2C M41ST87 (RTC)
AI11149
2/26
AN2159
Contents
Contents
1 2 3 4 5 STPM01 SPI module description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 READ start and timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Reading the STPM01 data registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 STPM01 READ sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 STPM01 READ process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.1 5.2 5.3 Calculating energy consumption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Timer interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Timer Subtasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6 7
Algorithm implementation examples . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3/26
List of figures
AN2159
List of figures
Figure 1. Figure 2. Figure 3. Figure 4. Figure 5. Measure module block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Control module block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Data record READ timing diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 STPM01 data register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Data READ structure (internal order) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4/26
AN2159
List of tables
List of tables
Figure 1. Figure 2. Figure 3. Figure 4. Figure 5. Measure module block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Control module block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Data record READ timing diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 STPM01 data register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Data READ structure (internal order) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5/26
STPM01 SPI module description
AN2159
1
STPM01 SPI module description
2-wire SPI is the main interface for STPM01 modules, and can be used to perform the following three operations: a) b) c) Remote reset requests; Reading up to eight 4-byte data records; and Temporary setting or clearing of the system signals (56 OTP shadow, 16 tests, or 8 modes) or permanent setting the 56 OTP anti-fuses. SC S: When driven low, it enables some SPI operation; SYN: It controls the SDATD output driver; SC LNLC: This is the serial input of the bit synchronization clock signal; and SD ATD: It is idle if the serial output bit data is driven high (SYN = = high), otherwise, it enables the serial input bit data signal.
The SPI bus used for the STPM01 peripheral module needs four pins:
The SPI module is idle when the SYN, SCS, SCLNLC, and SDATD bits are set to a logic `1' (VCC 0.8V). Any SPI operations should start from the idle state. The SYN and SCS signals are Active-low (VCC 0.2V). When the SCS bit is driven low, the SDATD signal should rise at the trailing edge of the SCLNLC signal and stabilize at the next leading edge of the SCLNLC signal (see Figure 3). The first valid SDATD bit is always started when SCLNLC is activated. The SPI master is implemented in the microcontroller of the control board. This means that SCS and SYN are normally driven from some parallel port while SCL and SDA are normally driven from some serial port to which an internal SPI peripheral unit is connected. This enables the SPI peripheral unit of microprocessor to operate as a 2-wire (simplex synchronous transfers) SPI. The microcontroller-to-STPM01 connection can be configured as follows:
MISO connected to SDATD; SCK connected to SCLNLC; SS connected to SCS; and A general purpose I/O pin connected to SYN.
6/26
AN2159
READ start and timing
2
READ start and timing
There are two phases of reading the STPM01 data:
Latching
This is used to sample results into transmission latches by initiating an active pulse on SYN while SCS is idle. The pulse length of SYN must be longer than 2 periods of the measurement clock (i.e., more than 500ns).
Shifting
This starts when SCS becomes active. In the beginning of this phase, another, much shorter pulse (30ns) should be applied to SYN. An alternative way to read shifted data is to extend the pulse on SYN into the second phase of reading. Latching and shifting finish at the last rise of SYN (shown by the dotted line in Figure 3). After these READs are completed, the STPM01 is reset. Note: Note: It is possible to read data, by applying 32 serial clocks per data record. Up to 8 data records can be read this way. Figure 3. Data record READ timing diagram
SCS
SYN
SCLNLC
SDATD
AI11150
7/26
Reading the STPM01 data registers
AN2159
3
Reading the STPM01 data registers
Eight 32-bit data registers in STPM01 store relevant measurement information (see the STPM01 datasheet for details). Figure 4 shows the records structure and the information in each record. The default sequence of reading, Table 1 shows the meaning of status bits in first data record. The first four register bits contain the 20-bit value of the internal energy counters. The value of the Least Significant Bit (lsb) of every energy counter is related to the power meter constant (Cp). This means that Cp changes with the application and relative calibration: If Cp = 128000 imp/kWh:
lsb = 4/2^20 Wh (active energy); lsb = 4/2^20 VAh (apparent energy); lsb = 8/2^20 varh (reactive energy); and lsb = 16/2^20 Wh (active fundamental energy).
For lsb values with any Cp, please refer to STPM01 datasheet for the complete formula. Figure 4. STPM01 data register
20bit 8bit 6bit 1bit DAP DRP DSP DFP DEV DMV CFL CFH 11bit p p uRMS uMOM lower part configuration upper part configuration 16bit
AI11151
4bit
1bit 1bit type0 active energy reactive energy apparent energy type1 energy iRMS iMOM 0 1 Status upper f(u) lower f(u) mode signals parity parity parity parity parity parity parity parity
Table 1.
Bit 0 1 2 3 4 5 6 7 N a me BIL BCF BFR BIT MU X LIN PIN HLT
First record status bit descriptions
Meaning of Status Bit = 0 No-load condition is not detected. Both signals are operative. fCLKOUT/217 < f(u) < fCLKOUT/215 Tamper has not been detected. Selected Primary Current Channel "u" is rising (from most negative to most positive value) Output pins follow data READ. Data is valid. Meaning of Status Bit = 1 No-load condition is detected. One or both signals are stacked. f(u) out-of-tolerance Tamper has been detected. Selected Secondary Current Channel "u" is falling (from most positive to most negative value) At least one pin differs from data READ. Data is invalid; restart initiated.
8/26
AN2159
Reading the STPM01 data registers Most of the register holds several distinctive values of certain bit length, except CFL and CFH, which hold a bit map of configuration codes. Most values are codified as unsigned integers, except for the two values in the DMV Bit, which are codified as signed binary. It is up to the user to decide how many records an application should read from the STPM01. The data records have a fixed position of reading. This means that records do not need to be addressed. Every register is packed into 4-bytes where the most significant nibble (4 bits) is reserved for the parity code and the rest of the 28 bits are used for data. Every register is protected by its own parity code. The first read out byte of the data record is the Least Significant Byte (LSB) of the data value and the fourth one is the Most Significant Byte (MSB) of the data value (see Figure 5).
Not e:
It is necessary to re-order the four bytes after reading them. Figure 5.
7
Data READ structure (internal order)
1st byte - LSB 0 7 0 7 0 7 parity nibble 4th byte - MSB 0
MSB msb 0 parity nibble 24 20 16 12 8 4
LSB lsb 0
AI11152
9/26
STPM01 READ sequence
AN2159
4
STPM01 READ sequence
To start an SPI communication with STPM01 and read new register values, it is necessary to apply a latching phase first, then start the shifting phase(see Figure 3). After the communication is initiated, 32 serial clock pulses need to be applied to the SCLNLC pin so the DAP register can be read. There are two outcomes of these activities: The READ is continued by maintaining 32 clock cycles per register until all of the applicable registers are read, or a precharge command is applied first (8 pulses to the SCLNLC pin while SYN = 0 and SDATD = 1) which moves an internal READ pointer to the DEV register (effectively skipping the DSP and DFP registers, and then reading may be continued).
After all of the registers are read, the SCS can be returned to idle state which ends the shifting phase. Otherwise, the shifting phase may be repeated to confirm that it reads the same values each time. This repetition is used to improve the reliability of successful READs in a strong EMI environment. After the READs are completed (and re-ordering of bytes after the last READ), each register should be unpacked in order to obtain all of the individual values. For example, the DAP register is unpacked into an 8-bit status value (LSB) and a 20-bit active energy counter value (the remaining upper 3 bytes with the parity code masked). In order to convert the register integer values into meaningful values, they should be multiplied by suitable parameters. The values of these parameters are defined as multiples of many system variables (e.g., sensor sensitivity, analog amplification, reference voltage, measurement frequency, calibration, decimation filter attenuation stages, meter constants). The best way to obtain the proper parameters is to measure signal known values. Not e: For the purposes of this document, we refer to the known values obtained for the ST energy meter evaluation board, STPMEVKIT.
10/26
AN2159
STPM01 READ process
5
STPM01 READ process
The application should be configured to keep the value of the active energy counter after a READ to use for energy evaluation, as well as to obtain the direction of energy flow after the subsequent READ. The READs must be done frequently because all of the energy up/down counters roll over in about 1 second when they are integrating maximum power. The recommended READ frequency is about 32 times per second.
5.1
Calculating energy consumption
The software implements a 32-bit integrator. The integrator is divided into two stages:
frac 32-bit signed integer variable integ 32-bit signed integer variables
The value of frac is derived from adding the quant value (the difference between the new and old energy values read from the STPM01). The old value should be updated with the new value for the next quant computation. Not e: In the case of active energy values, for a step of 0.01 kWh = 10 Wh, each bit of quant represents 4/2^20 Wh (the same resolution as the internal energy counter, because quant is calculated as the difference between two energy counter values), the threshold will be 10/(4/2^20) = 5/2*2^20 = 0x00280000. When frac collects a preset amount of energy (i.e., 10Wh, absolute, with the corresponding integer value stored as a threshold variable), the integ value should be changed by 1 bit and the frac value is changed according to the threshold value. This enables the frac value to be stored as 0.01 kWh, then integ is increased by one. The integ variable will hold the accumulated energy value (which the lsb represents as 10 Wh).
5.2
Timer interrupt
It stops the micro every 1/512s. This interrupt has one of the highest priorities because within it there are 16 different subtasks, controlled by a 4-bit counter SUBTASK, which is updated by each service. The whole meter task is broken into 16 shorter, consecutive subtasks (reading of the device's register, checking the data READ and if OK, computing the value of quant, and so forth) so as not to block the main program and other interrupt services for more than 200s for every 2ms. The meter task will be completed in 16 steps (in 1/32s). The interrupt service should do the following:
Update frac and integ of the active energy using quant; Generate output pulses (for the LED and/or stepper, if any); Call the subtask (SUBTASK); Update the SUBTASK; and Perform other tasks (e.g., date, and time).
11/26
STPM01 READ process
AN2159
5.3
Timer Subtasks
The addition of quant is split into 16 intervals, which generates a microcontroller output pulse that has 16 times better time resolution. In fact, the READ period would be 1/32s = 31.25ms. If the whole value of quant would be added to the final energy register, frac, with only 31.25ms resolution of output pulse position, it would be seen as jitter just by watching the LED. By using the recommended method, the resolution of output pulse position would be 1.95ms, which would produce almost unnoticeable jitter. The subtasks perform the following operations (see Chapter 6 for examples): subtask_0: latch the values in the STPM01 subtask_1: read the STPM01 subtask_2: repeat the reading of STPM01 subtask_3: verify the parity codes of the registers and equivalence of both readings; result is flag OK subtask_4: if OK unpack values of registers read from STPM01 subtask_5: if OK process STPM01's status subtask_6: if OK compute quant and update old active energy value subtask_7: if OK compute quant and update old reactive energy value subtask_8: if OK average VRMs, subtask_9: if OK average IRMs subtask_A: if OK update frac and integ of the reactive energy using new reactive energy quant subtask_B: convert integ of active energy into format suitable for LCD subtask_C: convert integ of reactive energy into format suitable for LCD subtask_D: scale and convert VRMs into format suitable for LCD subtask_E: scale and convert IRMs into format suitable for LCD subtask_F: if !OK quant = 0 else { quot = quant % 16, update frac of active energy using quot, quant /= 16}
12/26
AN2159
Algorithm implementation examples
6
Algorithm implementation examples
Global Define /* Exported type ------------------------------------------------*/
typedef struct STPM { u32 DAP; u32 DRP; u32 DSP; u32 DFP; u32 DEV; u32 DMV; u32 CFL; u32 CFH; } STPM_Struct;
typedef struct energ { u32 old; /* previous energy value */ u16 quot; /* quant/16 */ s16 quant; /* new - old, measure of power */ s32 frac; /* fraction part of energy integrator */ s32 integ; /* integer part of energy integrator */ } ENERG;
/* Global variables ---------------------------------------------*/
STPM_Struct *pSTPM_Reading; // ->STPM01 register ENERG *e; // ->energy_integrator u8 check_OK=0; // parity check
Interrupt Service Routine /*----------------------------------------------------------------ROUTINE NAME : IRQ_TIMA INPUT/OUTPUT : None/None DESCRIPTION : Interrupt routine for Timer A.
13/26
Algorithm implementation examples
AN2159
-----------------------------------------------------------------*/ void IRQ_TIMA(void) { // Clear Output Compare flag in timer Control/Status Register ... // Reset Counter ...
// Call Subtask to execute meter tasks Subtask(subtask_count);
// Update subtask counter subtask_count++; if (subtask_count > subtask_max) subtask_count = 0;
// Call integrator function EnergyUpdate(); }
/*----------------------------------------------------------------ROUTINE NAME : EnergyUpdate INPUT/OUTPUT : None/None DESCRIPTION : Energy update function (version for signed energy integration). If quant is negative, subtract otherwise add its value from the integrator. Returns true when frac reaches ELIMIT -----------------------------------------------------------------*/ u16 EnergyUpdate(void) { e=&ActWB; e->frac += (u32)e->quot; // add quot to frac
if (e->quant < 0) { // is quant negative? if (e->frac > -ELIMIT) return(0); // yes, is within the limit? e->frac += ELIMIT;
14/26
AN2159
Algorithm implementation examples (e->integ)--; // no, subtract it and increment } else { if (e->frac < ELIMIT) return(0); // no, is within the limit? e->frac -= ELIMIT; (e->integ)++; // no, subtract it and increment } return(1); // return limit has been reached } subtask_0 /*Subtask 0*/ STPM01Init();
/*----------------------------------------------------------------ROUTINE NAME : STPM01Init INPUT/OUTPUT : None/None DESCRIPTION : Starts SPI communication
-----------------------------------------------------------------*/ void STPM01Init(void) { ClrBit(PFDR,SYN_Pin); // Latching: SYN pin low Delay(15); ClrBit(PFDR,SCS_Pin); // Shifting: SCS pin low Delay(15); SetBit(PFDR,SYN_Pin); // then SYN pin high Delay(10); } subtask_1 /*Subtask 1*/ STPM01_Get_Register(&STPM_First);
/*----------------------------------------------------------------ROUTINE NAME : STPM01_Get_Register INPUT/OUTPUT : None/None
15/26
Algorithm implementation examples DESCRIPTION : Reads STPM01 registers
AN2159
-----------------------------------------------------------------*/ void STPM01_Get_Register (STPM_Struct *pSTPM_Reading) { u32 new_dwdata;
new_dwdata = STPM01SPIRead(); pSTPM_Reading->DAP=new_dwdata;
new_dwdata = STPM01SPIRead(); pSTPM_Reading->DRP=new_dwdata;
new_dwdata = STPM01SPIRead(); pSTPM_Reading->DSP=new_dwdata;
new_dwdata = STPM01SPIRead(); pSTPM_Reading->DFP=new_dwdata;
new_dwdata = STPM01SPIRead(); pSTPM_Reading->DEV=new_dwdata;
new_dwdata = STPM01SPIRead(); pSTPM_Reading->DMV=new_dwdata;
new_dwdata = STPM01SPIRead(); pSTPM_Reading->CFL=new_dwdata;
new_dwdata = STPM01SPIRead(); pSTPM_Reading->CFH=new_dwdata; } subtask_2 /*Subtask 2*/ STPM01_Get_Register(&STPM_Second); subtask_3 /*Subtask 3*/
16/26
AN2159 check_OK = STPM01_Check_Data(); STPM01Stop(); if (!check_OK) subtask_count=subtask_max;
Algorithm implementation examples
/*----------------------------------------------------------------ROUTINE NAME : STPM01_Check_Data INPUT/OUTPUT : None/None DESCRIPTION : Check STPM01 registers readings
-----------------------------------------------------------------*/ u8 STPM01_Check_Data (void) { u8 badparity_DAP=0, badparity_DRP=0, badparity_DSP=0, badparity_DFP=0, badparity_DEV=0, badparity_DMV=0, badparity_CFL=0, badparity_CFH=0;
u8 STPM01_ReadOK =0, STPM01_ck1 = 0, STPM01_ck2 = 0;
badparity_DAP = STPM01ParityCheck(STPM_First.DAP); badparity_DRP = STPM01ParityCheck(STPM_First.DRP); badparity_DSP = STPM01ParityCheck(STPM_First.DSP); badparity_DFP = STPM01ParityCheck(STPM_First.DFP); badparity_DEV = STPM01ParityCheck(STPM_First.DEV); badparity_DMV = STPM01ParityCheck(STPM_First.DMV); badparity_CFL = STPM01ParityCheck(STPM_First.CFL); badparity_CFH = STPM01ParityCheck(STPM_First.CFH);
// Return 1 if first reading is OK STPM01_ck1 = !(badparity_DAP | badparity_DRP | badparity_DSP | badparity_DFP | badparity_DEV | badparity_DMV | badparity_CFL | badparity_CFH);
badparity_DAP = STPM01ParityCheck(STPM_Second.DAP); badparity_DRP = STPM01ParityCheck(STPM_Second.DRP); badparity_DSP = STPM01ParityCheck(STPM_Second.DSP); badparity_DFP = STPM01ParityCheck(STPM_Second.DFP);
17/26
Algorithm implementation examples badparity_DEV = STPM01ParityCheck(STPM_Second.DEV); badparity_DMV = STPM01ParityCheck(STPM_Second.DMV); badparity_CFL = STPM01ParityCheck(STPM_Second.CFL); badparity_CFH = STPM01ParityCheck(STPM_Second.CFH);
AN2159
// Return 1 if second reading is OK STPM01_ck2 = !(badparity_DAP | badparity_DRP | badparity_DSP | badparity_DFP | badparity_DEV | badparity_DMV | badparity_CFL | badparity_CFH);
if ((STPM01_ck1 && STPM01_ck2) && (STPM_First.DAP == STPM_Second.DAP) && (STPM_First.DRP == STPM_Second.DRP) && (STPM_First.DSP == STPM_Second.DSP) && (STPM_First.DFP == STPM_Second.DFP) && (STPM_First.DEV == STPM_Second.DEV) && (STPM_First.DMV == STPM_Second.DMV)) STPM01_ReadOK = 1;
return STPM01_ReadOK; } /*----------------------------------------------------------------ROUTINE NAME : STPM01ParityCheck INPUT/OUTPUT : Register read/Result of parity check DESCRIPTION : Check parity
-----------------------------------------------------------------*/ u8 STPM01ParityCheck(u32 pc) { u8 parity, i;
parity = 0x00;
for (i=0; i!=4; i++) parity ^= (pc >> (8*i));
parity |= (parity<<4);
18/26
AN2159 parity &= 0xF0;
Algorithm implementation examples
return (parity!= 0xF0); // return 1 if bad parity }
/*----------------------------------------------------------------ROUTINE NAME : STPM01Stop INPUT/OUTPUT : None/None DESCRIPTION : Stops SPI communication
-----------------------------------------------------------------*/ void STPM01Stop(void) { Delay(15); SetBit(PFDR,SCS_Pin); Delay(15); } subtask_4 /*Subtask 4*/ Unpack_Data();
/*----------------------------------------------------------------ROUTINE NAME : Unpack_Data INPUT/OUTPUT : None/None DESCRIPTION : Unpack registers of STPM01
-----------------------------------------------------------------*/ void Unpack_Data (void) { status = STPM_First.DAP;
active0 = ((STPM_First.DAP >> 8) & 0xFFFFF);
freq = (STPM_First.DRP & 0x3F); freq <<= 8; freq += (STPM_First.DSP & 0xFF);
19/26
Algorithm implementation examples reactive = ((STPM_First.DRP >> 8) & 0xFFFFF);
AN2159
apparent = ((STPM_First.DSP >> 8) & 0xFFFFF);
mode =
STPM_First.DFP;
active1 = ((STPM_First.DFP >> 8) & 0xFFFFF);
iRMS = STPM_First.DEV;
uRMS = ((STPM_First.DEV >> 16) & 0x7FF);
iMOM = STPM_First.DMV;
uMOM = ((STPM_First.DMV >> 16) & 0x7FF); } subtask_5 /*Subtask 5*/ Status();
/*---------------------------------------------------------------ROUTINE NAME : Status INPUT/OUTPUT : None/None DESCRIPTION : Analize status byte
-----------------------------------------------------------------*/ void Status (void) { u8 bil, bfc, bfr, bit, mux, lin, pin, hlt;
bil = ValBit(status, BIL); // 0 - No load condition not // detected // 1 - No load condition detected
bfc = ValBit(status, BCF)>>1; // 0 - Both Sigma-Delta signals // alive
20/26
AN2159
Algorithm implementation examples // 1 - One or both Sigma-Delta // signal stacked
bfr = ValBit(status, BFR)>>2; // 0 - Frequency between // fCLKOUT/2^17 and fCLKOUT/2^15 // 1 - Frequency out of limits
bit = ValBit(status, BIT)>>3; // 0 - Tamper not Detected // 1 - Tamper Detected
mux = ValBit(status, MUX)>>4; // 0 - Primary Current channel // selected // 1 - Secondary Current channel // selected
lin = ValBit(status, LIN)>>5; // 0 - Positive half period of u // 1 - Negative half period of u
pin = ValBit(status, PIN)>>6; // 0 - Output pins follow data // 1 - At least one pin differs // from data
hlt = ValBit(status, HLT)>>7; // 0 - Data are valid // 1 - Retarded restart in // progress } subtask_6 /*Subtask 6*/ e=&ActWB; com=EnergyQuant(active0);
/*----------------------------------------------------------------ROUTINE NAME : EnergyQuant INPUT/OUTPUT : None/None DESCRIPTION : Energy quant computing function (version for signed energy integration). Checks for rollovers and compute quant as difference between new and old value. Returns a negative sign of quant. -----------------------------------------------------------------*/ u16 EnergyQuant(u32 new) {
21/26
Algorithm implementation examples // Rollover high to low occurs when MSB goes from F to 0 if((e->old & 0xF0000) == 0xF0000){// possible HL rollover? if((new & 0xF0000) == 0x0) // yes, really? Yes new += 0x10 0 0;// fix new value }
AN2159
// Rollover low to high occurs when MSB goes from 0 to F else if((e->old & 0xF0000) == 0x0){// possible LH rollover? if((new & 0xF0000) == 0xF0000)// yes, really? Yes e->old += 0x10 0 0;// fix old value }
e->quant = (u16)(new - e->old);// evaluate the difference e->old = new;// save the value for next // time e->quot = e->quant & 0x000F;// reminder of quant/16
if (e->quant < 0)// is quant negative? e->quot |= 0xFFF0;// yes, make reminder // negative
e->frac += (u32)e->quot;// add reminder to frac e->quot = e->quant >> 4;// quotient of quant/16
return(e->quant < 0);// return true if quant is // negative } subtask_7 /*Subtask 7*/ e=&Reactive; com=EnergyQuant(reactive); subtask_8 /*Subtask 8*/ uRMS = Process_VRMS();
22/26
AN2159
Algorithm implementation examples /*----------------------------------------------------------------ROUTINE NAME : Process_VRMS INPUT/OUTPUT : None/None DESCRIPTION : Average VRMS
-----------------------------------------------------------------*/ u16 Process_VRMS (void) { u16 aux=uRMS; aux /= 0.364; return aux; } subtask_9 /*Subtask 9*/ iRMS = Process_IRMS();
/*---------------------------------------------------------------ROUTINE NAME : Process_IRMS INPUT/OUTPUT : None/None DESCRIPTION : Average IRMS
-----------------------------------------------------------------*/ u16 Process_IRMS (void) { u16 aux = iRMS; aux /= 6.468; return aux; } subtask_A /*Subtask A*/ ReactiveUpdate(); subtask_B /*Subtask B*/ long2ascii(ActWB.integ, ActWB_Str); subtask_C /*Subtask C*/ long2ascii(Reactive.integ, Reactive_Str);
23/26
Algorithm implementation examples subtask_D /*Subtask D*/ volt2ascii(uRMS, uRMS_Str); subtask_E /*Subtask E*/ curr2ascii(iRMS, iRMS_Str); subtask_F /*Subtask F*/ ActWB.quot = 0;
AN2159
24/26
AN2159
Revision history
7
Revision history
Table 2.
Dat e June-05 19-Apr-06
Revision history
Version 1 2 Description First edition New template
25/26
AN2159
Please Read Carefully:
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries ("ST") reserve the right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any time, without notice. All ST products are sold pursuant to ST's terms and conditions of sale. Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no liability whatsoever relating to the choice, selection or use of the ST products and services described herein. No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST'S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT. UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZE REPRESENTATIVE OF ST, ST PRODUCTS ARE NOT DESIGNED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS, WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY, DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any liability of ST.
ST and the ST logo are trademarks or registered trademarks of ST in various countries. Information in this document supersedes and replaces all information previously supplied. The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.
2006 STMicroelectronics - All rights reserved STMicroelectronics group of companies Australia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan Malaysia - Malta - Morocco - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of America www.st.com
26/26
|