C18 48bit addition to form kWh counter. (from page 3)https://www.microchip.com/forums/(c) Microchip30Re: 64bit addition. (PStechPaul)If the waveform being measured is DC, or a square wave, then each measurement will be a maximum of either 512 or -512, and you would be correct that the square would be 2^18. But the readings for a sine wave having a peak value of +/-512 would have a sequence of readings (every 30 degrees for simplicity) of:<br/> <br/>0, 256, 443, 512, 443, 256, ...<br/> <br/>The squares (for RMS voltage or current) or products (for power) would be:<br/> <br/>0, 65536, 196608, 262154, 196608, 65536, ...<br/> <br/>The sum of squares (products) is <b>785724</b>. Divide by six samples is <b>130954</b>. The square root is <b>362</b>, while 512/sqrt(2) = <b>362</b>.<br/> <br/>https://www.microchip.com/forums/FindPost/1144223Tue, 16 Jun 2020 23:43:40 GMTRe: 64bit addition. (GlennP)<blockquote class="quote"><span class="original">PStechPaul</span> ...<br/>Also, the RMS value will be 70.7% of peak value, or 362 counts. Thus <b>the product will be a maximum of 131,000</b>, or 17 bits. ...</blockquote><br>Emphasis added.<br/> <br/>Huh? I must be missing something. Doesn't one compute the product <b>before</b> doing the mean or the root?<br/> <br/>I think the maximum product is -512 * -512 = 262,144, which is 2^18.<br/> <br/>GP<br/> <br/>Edit 1: square -> product in "Huh" line.<br/>https://www.microchip.com/forums/FindPost/1144219Tue, 16 Jun 2020 23:16:53 GMTRe: 64bit addition. (PStechPaul)Sounds like you have a good handle on the situation.<br/> <br/>Maybe it's time to change the title of this thread to something more appropriate, now that it's back on a good track. Lots of good info here, and it might help someone else with similar issues. ; )<br/>https://www.microchip.com/forums/FindPost/1144216Tue, 16 Jun 2020 22:39:34 GMTRe: 64bit addition. (andy_n)Thanks Paul,<br/>it have to be potential between L and Earth. N is practically Earth.<br>I've built L detection and can correct the problem.<br/>https://www.microchip.com/forums/FindPost/1144213Tue, 16 Jun 2020 22:21:40 GMTRe: 64bit addition. (PStechPaul)All the references I have found indicate that Germany (and most of the EU) use 230/400 volt service, and household 230 VAC sockets allow reversal of neutral and line. This necessitates the use of two pole switches and breakers on appliances, but distribution panels may have single pole breakers, fuses, and disconnects. The full three phase supply may be commonly available for many residents, and probably almost all commercial and industrial installations.<br/> <br/><a href="https://en.wikipedia.org/wiki/AC_power_plugs_and_sockets" target="_blank" rel="nofollow" title="https://en.wikipedia.org/wiki/AC_power_plugs_and_sockets">https://en.wikipedia.org/wiki/AC_power_plugs_and_sockets</a><br/> <br/><a href="https://www.generatorsource.com/Voltages_and_Hz_by_Country.aspx" target="_blank" rel="nofollow" title="https://www.generatorsource.com/Voltages_and_Hz_by_Country.aspx">https://www.generatorsour...and_Hz_by_Country.aspx</a><br/> <br/>Thus, it may be expected that a small error (or difference) may be found in voltage, current, and power measurement if line and neutral are reversed. The measurement device most likely has a grounded enclosure, which means that internal components may have some leakage or capacitive current to earth ground, and this could affect the measurement. The CT is the most likely source of error, as its potential to earth will change when line and neutral are reversed. Even if the device is fully insulated and lacks a ground connection (as would be the case with a battery operated DMM), there will still be capacitance and resistance to the earth reference, and prevailing power line EM radiation will be present unless all power lines and components are fully shielded.<br/>https://www.microchip.com/forums/FindPost/1144152Tue, 16 Jun 2020 14:44:01 GMTRe: 64bit addition. (andy_n) <br/>"unmeasured current between the L (LINE) and E (EARTH) ground." Is a good idea. <br/> <br/>Till now I found a lot of experience. <br/>I think "real electrical power engineers" has no time for practical problems.<br/>Some answers have helped me a lot, fewer unnecessary questions.<br/>Some claim to have the only correct answer. This is dangerous. grin: <img src="https://www.microchip.com/forums/upfiles/smiley/bigSmile.gif" alt="grin" data-smiley="<img src="https://www.microchip.com/forums/upfiles/smiley/bigSmile.gif" alt="grin" data-smiley="[:D]" />" /><br/> <br/> <br/> <br/>https://www.microchip.com/forums/FindPost/1144134Tue, 16 Jun 2020 12:24:59 GMTRe: 64bit addition. (dan1138)andy_n,<br/> <br/>What you described where the load can have L (LINE) and N (NEUTRAL) connected in any order is a problem for modern equipment that has a safety E (EARTH) ground connection, and is very dangerous in old equipment that does not.<br/> <br/>That you see an error in power measurement in reversed connection suggests there is unmeasured current between the L (LINE) and E (EARTH) ground.<br/> <br/>That you have not posted about equipment failures, explosions or fires would indicate that these kinds of connections are not so serious in your test environment.<br/> <br/>Perhaps you should check with some real electrical power engineers rather that this nasty lot of ne'er-do-well reprobates (me included) that hang about this forum. :)<br/>https://www.microchip.com/forums/FindPost/1144133Tue, 16 Jun 2020 11:49:12 GMTRe: 64bit addition. (andy_n)Active power, Reactive power, Apparent power are depending of PF. This works properly. <br/> <br/>Andrew:<br/>Differential measurement between the two.<br/>If the current measurement is at 'L', the voltage measurement is greater.<br/>Can the L 'hum' like in audio amplifiers? grin: <img src="https://www.microchip.com/forums/upfiles/smiley/bigSmile.gif" alt="grin" data-smiley="<img src="https://www.microchip.com/forums/upfiles/smiley/bigSmile.gif" alt="grin" data-smiley="[:D]" />" /><br/>No idea! <br/> <br/> <br/>https://www.microchip.com/forums/FindPost/1144119Tue, 16 Jun 2020 09:47:56 GMTRe: 64bit addition. (crosland)<blockquote class="quote"><span class="original">andy_n</span><br/>I have a small difference (approx. 1%) between L and N voltage measurements. <br/></blockquote><br><br><br/>Measured relative to Earth? In which case are they guaranteed to be balanced? In the UK the N is tied to Earth..<br/> <br/>Or simply the differential measurement between the two? In which case your measuring equipment is introducing an offset.<br/>https://www.microchip.com/forums/FindPost/1144103Tue, 16 Jun 2020 09:11:53 GMTRe: 64bit addition. (moser)If rotating the plug makes a difference, then this is your measurement. If we assume that the device which you try to measure does not behave different, then physics is clear:<br/> <br/>V<sub>plug_not_rotated</sub> = - V<sub>plug_rotated</sub><br/>I<sub>plug_not_rotated</sub> = - I<sub>plug_rotated</sub><br/> <br/>However, if the behavior of your device is affected by something else, for example it is electrically connected to another device which doesn't have its plug rotated, then this could in theory cause a different current behavior.<br/> <br/>By the way, PStechPaul and NorthGuy are referring to the difference of<br/><ul><li>Active power<li>Reactive power<li>Apparent power (or Complex power)</ul>See for example:<br/><a href="https://en.wikipedia.org/wiki/AC_power" target="_blank" rel="nofollow">https://en.wikipedia.org/wiki/AC_power</a><br/><a href="https://en.wikipedia.org/wiki/Power_factor" target="_blank" rel="nofollow">https://en.wikipedia.org/wiki/Power_factor</a><br/> <br/>https://www.microchip.com/forums/FindPost/1144102Tue, 16 Jun 2020 09:04:27 GMTRe: 64bit addition. (andy_n)NorthGuy<br/> <br/>I did not describe it correctly: not this phase grin: <img src="https://www.microchip.com/forums/upfiles/smiley/bigSmile.gif" alt="grin" data-smiley="<img src="https://www.microchip.com/forums/upfiles/smiley/bigSmile.gif" alt="grin" data-smiley="[:D]" />" /><br/>You have (L) = Line and Neutral (N) in the power line.<br/>The 'L' is not determined in Germany. The plug can be turned. No polarity L/N<br/> <br/>If I turn the plag (change N with L) get different (ca. 1%) voltage measurement.<br/> <br/> <br/>https://www.microchip.com/forums/FindPost/1144092Tue, 16 Jun 2020 08:00:56 GMTRe: 64bit addition. (NorthGuy)<blockquote class="quote"><span class="original">andy_n</span><br/>The phase is not determined in Germany.<br/></blockquote><br><br><br/>Not that phase. When you use non-resistive loads, such as a motor, the current draw is shifted in time compared to voltage - the peaks in current do not coincide with peaks in voltage. This is called a phase shift. If it is 90 degrees, the power is zero despite huge swings in current - power factor (PF) is zero.<br/> <br/>When you measure current (with current transformer), your measurement is also phase-shifted compared to actual current. This is called phase error. Phase error distorts PF and leads to errors in calculated power. If PF is 0.5-0.6 (as it is in inductive motor), a few degree phase error may lead to huge errors in your power measurements. If you use single ADC to measure both current and voltage, you can select the time between measurements equal to your phase error. This way, your consecutive readings of voltage and current will refer to the same point in time.<br/>https://www.microchip.com/forums/FindPost/1144089Tue, 16 Jun 2020 07:42:15 GMTRe: 64bit addition. (andy_n)Paul & NorthGuy,<br/> <br/>You seem to have some experience with power measurement.<br/> <br/>I have a small difference (approx. 1%) between L and N voltage measurements. The phase is not determined in Germany. The plug can be turned.<br/>Is that known.<br/>I have built in phase detection but wanted to know if it is basically like this?<br/> <br/> <br/>https://www.microchip.com/forums/FindPost/1144080Tue, 16 Jun 2020 06:52:47 GMTRe: 64bit addition. (andy_n)Hi Hen,<br/> <br/>It is another solution but the 18F87J72 would be just right for that.<br/> <br/>Thanks for info. I look closely at it.<br/> <br/>https://www.microchip.com/forums/FindPost/1144046Tue, 16 Jun 2020 04:34:02 GMTRe: 64bit addition. (Hen)Nice, Andy grin: <img src="https://www.microchip.com/forums/upfiles/smiley/bigSmile.gif" alt="grin" data-smiley="<img src="https://www.microchip.com/forums/upfiles/smiley/bigSmile.gif" alt="grin" data-smiley="[:D]" />" /><br/> <br/>Do you use the 18F87J72?<br/>I have used it succesfully in several applications.<br/>https://www.microchip.com/forums/FindPost/1144023Tue, 16 Jun 2020 03:23:30 GMTRe: 64bit addition. (andy_n)Thanks for the discussion.<br>Power measurement works very well. True RMS with Watt average calculation, VA, VAR, PF.<br>3.6 should also be a kind of calibration parameter.<br>It is a mobile device.<br>Gotta do something today, get in touch later.<br/> <br/>Thanks for help.<br/>https://www.microchip.com/forums/FindPost/1143992Tue, 16 Jun 2020 00:51:31 GMTRe: 64bit addition. (PStechPaul)Actually, if you are sampling a sine wave, you will need to shift it to 1/2 the Vref, so the resolution effectively becomes nine bits (plus sign), and readings will be +/- 512 counts. Also, the RMS value will be 70.7% of peak value, or 362 counts. Thus the product will be a maximum of 131,000, or 17 bits. Then if you sample at 16,000 per second, over a 1 second period, you will have a maximum count of 2,097,152,000, or 31 bits. Then you need to divide that total by the number of samples (16,000) which will again be 17 bits, maximum.<br/> <br/>But there is really no need to maintain that many significant digits. You could easily shift by 4 to get a 15 bit integer result, or perform other adjustments to provide a watt reading in the desired units. No need to apply any mysterious factors (3.6?) for the results to be totalized. That can be done at the display rate, which should be 2-4 per second, and can use floating point.<br/>https://www.microchip.com/forums/FindPost/1143978Mon, 15 Jun 2020 23:01:15 GMTRe: 64bit addition. (NorthGuy)<blockquote class="quote"><span class="original">andy_n</span><br/>The A/D converter works with 32kHz. <br/></blockquote><br><br><br/>Assuming you interleave voltage and current measurements, you should then get a new power value 16000 times per second. If ADC is 10 bit, then your number will have 20 bits for the result of multiplication. If you accumulate everything straight, you will need 34-bit number by the end of the second.<br/> <br/>https://www.microchip.com/forums/FindPost/1143962Mon, 15 Jun 2020 18:51:49 GMTRe: 64bit addition. (PStechPaul)In order to get correct (true RMS) voltage and current, you need to sample the waveform at least twice the maximum frequency of any significant harmonics, generally at least 10 samples per half-cycle. 1200 samples per second will give 10 samples per cycle at 60 Hz and 12 samples per cycle at 50 Hz. And you need to take the square of each reading, add that to an accumulator, and then take the square root over an integral number of half-cycles. 240 samples would be 20 cycles at 60 Hz and 24 cycles at 50 Hz, and if you perform the square root on 240 samples and update the display each time, it will be 3 per second which is a comfortable rate for readability.<br/> <br/>32 kHz may be the fastest the ADC can update, but you don't need that for mains power measurement, unless you are trying to construct a high precision laboratory instrument, in which case you may need a more powerful processor. You will at least need a PIC with dual simultaneous sampling ADCs to read power accurately. PF, phase angle, and VARs can be computed from voltage, current, and true power.<br/> <br/>It would be helpful if you would provide a schematic of your device and the code, or at least that which is relevant. We may be able to make suggestions that will result in a much better product, whether this is for a commercial or industrial instrument, or for personal or consumer use.<br/>https://www.microchip.com/forums/FindPost/1143942Mon, 15 Jun 2020 15:37:27 GMTRe: 64bit addition. (andy_n)The A/D converter works with 32kHz. <br/>https://www.microchip.com/forums/FindPost/1143933Mon, 15 Jun 2020 14:34:03 GMT