• AVR Freaks

Helpful ReplyHot!C18 48bit addition to form kWh counter. (from page 3)

Page: < 12345.. > >> Showing page 5 of 6
Author
andy_n
Senior Member
  • Total Posts : 155
  • Reward points : 0
  • Status: offline
Re: 64bit addition. 2020/06/15 00:00:20 (permalink)
+1 (1)
 
1and0
 
log(x) / log(2) = bits. Great, it's new for me. 
#81
PStechPaul
Super Member
  • Total Posts : 2932
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: online
Re: 64bit addition. 2020/06/15 00:23:23 (permalink)
+2 (2)
I had to go back and find the post where it was stated that this 15,000 number would be added every second. As I understand it, this is a watt-hour totalizer, and so I assumed a maximum power of 15,000 watts over a period of one hour. If this figure is added every second, then the totalizer would read watt-seconds, or Joules. Also, in post #62, it is stated that this is a kWh counter, so does that mean this 15,000 figure is actually based on 15,000 kW, or 15 Megawatts?
 
Another consideration is that watt-hour data is usually segmented by periods of an hour, day, week, month, or year, so it may be more useful to implement a database with these subtotals, which can then be presented for analysis or display. Utilities also often charge based on peak demand, which may be calculated over intervals as small as 5 minutes or so. Back in the 80s I worked on a Solid State Demand Recorder (SSDR) which read power from electric meters that used a pulse train which varied in frequency according to power usage. Thus the device would totalize these pulses over five minute intervals with a maximum count (IIRC) of 1000, so it was stored in UVEPROMs or EEPROMs using 10 bits in compressed format. It used plastic cartridges the same size as 8-track tapes, and contained eight 28 pin DIP packages each having about 8 kB storage for a total of 64 kB. That was enough for about 50,000 samples, or about 180 days, before which time the cartridge would be removed and read into a mainframe computer for analysis and customer billing. We also developed a remote version which used 300 and 1200 baud modems and phone lines to retrieve data and erase the local storage. This is how things were done before the internet :)

 
#82
PStechPaul
Super Member
  • Total Posts : 2932
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: online
Re: 64bit addition. 2020/06/15 00:52:39 (permalink)
0
I was composing my post before I saw your post with this:
max 15,000 is calculated Watt from measure device *3,6
230V*16A=3680W*3,6=13248 max

What is the 3.6 (or 3,6) multiplier? And, assuming this is 230 VAC, if you are simply multiplying voltage and current, you will get Volt-Amperes (VA), which is not the same as power (W), because the voltage and current are never in phase except for a pure resistive load. To get true watts, you must take a simultaneous sample of voltage and current, multiply the values, and take the average over a number of complete cycles. You could also read the phase angle, and use the cosine to determine power factor. If the voltage and current are in phase, at 0 degrees, the power factor is 1 (unity). Power factor correction is used in switching power supplies and VFDs to reduce current caused by the charging of the energy storage capacitors, especially for single phase. And utilities often add capacitors to distribution lines to achieve better PF because many loads (particularly motors) are inductive.

 
#83
moser
Super Member
  • Total Posts : 587
  • Reward points : 0
  • Joined: 2015/06/16 02:53:47
  • Location: Germany
  • Status: offline
Re: 64bit addition. 2020/06/15 01:17:53 (permalink)
+1 (1)
In theory, the compiler is allowed to reorder instructions in any way he want as long as all side effects (= mainly volatile accesses) occur in the correct order, and the result at the end is the same. 
 
Assume code like:
a = b + c;
addend1.lo += addend2;
if (STATUSbits.C) addend1.hi++;
d = e + f;

It would be perfectly legal for a C compiler to reorder it to:
addend1.lo += addend2;
a = b + c;
d = e + f;
if (STATUSbits.C) addend1.hi++;
Usually the compiler will never do that, as this doesn't optimize anything.
 
However, if it is done, the reason is often to move one instruction away from another instruction which uses the result of the first instruction, and it is done to prevent a CPU pipeline stall. But I have no idea if anything of this could happen with C18 and your processor at all.
#84
andy_n
Senior Member
  • Total Posts : 155
  • Reward points : 0
  • Status: offline
Re: 64bit addition. 2020/06/15 01:42:12 (permalink)
0
ric: Sorry, I didn't mean to offend you.
 
look: I have been working very (very) unconventionally and successfully for years.
I like such 'exceptions' but need to prove that it doesn't work.
You know 'goto' is bad but you have to use it every 5 years. wink: wink
#85
crosland
Super Member
  • Total Posts : 2041
  • Reward points : 0
  • Joined: 2005/05/10 10:55:05
  • Location: Warks, UK
  • Status: offline
Re: 64bit addition. 2020/06/15 01:57:21 (permalink)
+1 (1)
andy_n
max 15,000 is calculated Watt from measure device *3,6
230V*16A=3680W*3,6=13248 max  
 



Where in the world are you? The 230V mains is rarely 230V.
 
When the UK and Europe harmonised their slightly different supplies, nothing actually changed apart from the way the allowed voltage is specified. The UK supply is still 240V with a tolerance of -6%/+10% or 216V to 253V to fit in.
#86
andy_n
Senior Member
  • Total Posts : 155
  • Reward points : 0
  • Status: offline
Re: 64bit addition. 2020/06/15 02:12:15 (permalink)
0
Paul,
 
3.6 is only factor.
'simply multiplying voltage and current' don't works.
I get W, VA, VAR & PF
I have to add every second W and get Ws/3,600,000=kWh over years.
 
 
 
 
#87
andy_n
Senior Member
  • Total Posts : 155
  • Reward points : 0
  • Status: offline
Re: 64bit addition. 2020/06/15 02:19:25 (permalink)
0
Thanks Manuel,
 
But 'if (STATUSbits.C) addend1.hi++;' can not be changed?
This is the point.
 
#88
andy_n
Senior Member
  • Total Posts : 155
  • Reward points : 0
  • Status: offline
Re: 64bit addition. 2020/06/15 02:26:50 (permalink)
+1 (1)
Andrew,
 
Germany. Old 220VAC is now 230VAC.
 
found this:
The mains voltage in most of Europe is 230 volts with a frequency of 50 Hertz. The often
220 volts specified are outdated. Up to 1987 the mains voltage in Germany was 220 V.
with a tolerance of ± 10%. After that there was a gradual change to 230 V. Ab
In 2009 the 230 V mains voltage may deviate by ± 10%. The tolerance is a use
older devices with a voltage of 220 V possible without problems.
#89
andy_n
Senior Member
  • Total Posts : 155
  • Reward points : 0
  • Status: offline
Re: 64bit addition. 2020/06/15 03:12:48 (permalink)
0
Paul,
 
SSDR - interesting!
 
I want to make two Ws (kWh) counter one of them erasable.
You can determine the time range yourself.
Then write all electric data A, V, W ... on the SD card via SPI.
 
 
 
#90
1and0
Access is Denied
  • Total Posts : 11163
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: 64bit addition. 2020/06/15 10:43:17 (permalink)
+2 (2)
andy_n
log(x) / log(2) = bits. Great, it's new for me. 

That is an approximation, the precised formula is

    bits = log(x + 1) / log(2)
 

andy_n
look: I have been working very (very) unconventionally and successfully for years.
I like such 'exceptions' but need to prove that it doesn't work.
...
 
But 'if (STATUSbits.C) addend1.hi++;' can not be changed?
This is the point.

Several members, including myself, have informed you of the caveats in using the Carry bit in C code. This
addend1.lo += addend2;
if (STATUSbits.C) addend1.hi++;

most likely will work, but no experienced programmers will guarantee it 100%. You may do so (gamble) at your own risks; so check the disassembly listing whenever your code or compiler is changed. However, doing it the way I've posted is guaranteed to work.
 
That being said, are you really that press for code space? It's only a difference of four instructions.
 
#91
andy_n
Senior Member
  • Total Posts : 155
  • Reward points : 0
  • Status: offline
Re: 64bit addition. 2020/06/15 11:33:36 (permalink)
0
Harry,
Thanks. I press for nothing. I just wanted to hear other opinions.
I hope i am reasonable enough and do not expect any guarantee.
 
Regards 
#92
dan1138
Super Member
  • Total Posts : 3858
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: 64bit addition. 2020/06/15 11:40:46 (permalink)
+2 (2)
@Harry,
 
andy_n would get better code space utilization from an improving the algorithm that implements his application rather than implementing an extended arithmetic accumulator with controller and compiler specific inline code.
 
A better design that uses only the basic types supported by his C18 compiler will result in code that can be maintained by anyone with adequate experience.
#93
andy_n
Senior Member
  • Total Posts : 155
  • Reward points : 0
  • Status: offline
Re: 64bit addition. 2020/06/15 12:22:27 (permalink)
0
 
But I have to admit one thing:
In order to keep the project alive for years through new features, I had to learn to use resources sparingly and I am always at the limit but never in danger. We are just talking. grin: grin
 
Dan:
I come from the assembler but don't like the inline code in C. The side effects are too uncertain for me.
 
I think the status bits are the soul of the assembler programming. Therefore the enthusiasm for 'if (STATUSbits.C)'. Joke!
#94
NorthGuy
Super Member
  • Total Posts : 6309
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: 64bit addition. 2020/06/15 13:51:33 (permalink)
+1 (1)
andy_n
I have to add every second W and get Ws/3,600,000=kWh over years.



If this is 50 Hz AC, you should measure at the very least every millisecond, preferably faster. If you have any motors with low PF then even few degrees phase error between current and voltage will produce huge (eg 20%) differences in the result. If you have devices such as computers and similar, the most of the consumption is on the peaks and therefore the current may change very fast.
 
Measuring power to get even 1% accuracy is difficult. Thus, maintaining it with such a high resolution is not necessary. You can calculate the power for the day, then accumulate every day with lower resolution.
#95
andy_n
Senior Member
  • Total Posts : 155
  • Reward points : 0
  • Status: offline
Re: 64bit addition. 2020/06/15 14:34:03 (permalink)
0
The A/D converter works with 32kHz. 
#96
PStechPaul
Super Member
  • Total Posts : 2932
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: online
Re: 64bit addition. 2020/06/15 15:37:27 (permalink)
+2 (2)
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.
 
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.
 
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.

 
#97
NorthGuy
Super Member
  • Total Posts : 6309
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: 64bit addition. 2020/06/15 18:51:49 (permalink)
+1 (1)
andy_n
The A/D converter works with 32kHz. 



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.
 
#98
PStechPaul
Super Member
  • Total Posts : 2932
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: online
Re: 64bit addition. 2020/06/15 23:01:15 (permalink)
+2 (2)
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.
 
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.

 
#99
andy_n
Senior Member
  • Total Posts : 155
  • Reward points : 0
  • Status: offline
Re: 64bit addition. 2020/06/16 00:51:31 (permalink)
0
Thanks for the discussion.
Power measurement works very well. True RMS with Watt average calculation, VA, VAR, PF.
3.6 should also be a kind of calibration parameter.
It is a mobile device.
Gotta do something today, get in touch later.
 
Thanks for help.
Page: < 12345.. > >> Showing page 5 of 6
Jump to:
© 2020 APG vNext Commercial Version 4.5