English
Language : 

AN2512 Datasheet, PDF (17/38 Pages) STMicroelectronics – This application note describes how to design
AN2512
Power calculation algorithm
3.4
Data integrity checks
Each bit of parity nibble is defined as odd parity of all seven corresponding bits of data
nibbles. In order to check the data record integrity, the application might execute the
following C code, given as an example:
int BadParity (unsigned char *bp)
{
register unsigned char prty;
/* temporary register */
prty = *bp,
/* take the 1st byte of data */
prty ^= *(bp+1),
/* XOR it with the 2nd byte*/
prty ^= *(bp+2),
/*and with the 3rd byte */
prty ^= *(bp+3),
/*and with the 4th byte */
prty ^= prty<<4, prty &= 0xF0;
/* combine and remove the lower nibble */
return (prty != 0xF0);
/*returns 1, if bad parity */
}
if (BadParity(dap) || BadParity(drp) || /* DAP and DRP. data record*/
BadParity(dsp) || BadParity(dfp) || /* DSP and DFP data record */
BadParity(dev) || BadParity(dmv) || /* DEV and DMV data record */
BadParity(cfl) || BadParity(cfh)) /* CFL and CFH data record */
/* code for repeat of reading sequence should be entered here */ ;
If the parity nibble check fails, the reading task should be repeated but, this time, without
request of latching, otherwise a new data is latched and the previous reading is incorrectly
lost. In a very harsh EMI environment, it would be good practice to read the data records
twice and then compare both readings. This way the probability of detecting bad readings is
significantly improved. A single bad data reading can be discarded because no meaningful
information is lost as long the reading frequency is about 30 ms.
3.5
Unpacking of data records
After each data record is read correctly, its parity nibble and possible padding bits should be
masked out and then it should be unpacked to retrieve each information as indicated in
Figure 9. The momentary values of current and voltage should be considered as signed
integers, while all the other values as unsigned integers.
Below an example of readings and unpacking of the data is given (MSBF is cleared, that is,
the device reads most significant bit first).
● DAP 65 7A 7C 82: parity = 8, type0 energy=27C7A, status = 65
● DRP 52 7A 0C 90: parity = 9, reactive energy=00C7A, frequency high byte = 52
● DSP 25 00 8C E2: parity = E, apparent energy=28C00, frequency low byte = 25
17/38