OK Vasile. Here's a sample of sampling. > -----Original Message----- > From: Vasile Surducan [mailto:vasile@L30.ITIM-CJ.RO] > Sent: Wednesday, November 14, 2001 1:32 AM > To: PICLIST@MITVMA.MIT.EDU > Subject: Re: [EE]:Measuring AC currents > > > On Tue, 13 Nov 2001, Harold M Hallikainen wrote: > > > On Mon, 12 Nov 2001 13:14:35 -0600 Don Hyde writes: > > > Since you have a processor, you can compute the true RMS. SNIP > > > I think you just need the sum of the squares and > the number of samples. If you want it to work for arbitrary waveforms, then you must allow for a DC offset, in which case you also need the sum of the samples. > > So you compute the squares on the fly, add them to the existing sum. > > After you have some number of samples (256 is nice), divide > by the number > > of samples, then take the square root. In one system, I > "shot for" 200 > > samples per half cycle of the AC line, then used the divided by the > > actual number of samples I got between zero crossings (not > quite as nice > > as dividing by 256, but it's difficult to get just 256 > samples between > > zero crossings). > > Collect samples for long enough to guarantee that you've sampled many cycles, and zero crossings become irrelevant. > > > I'm not sure I've completely understood. If you take 256 > samples in 10mS > ( 50 Hz ) [ HOW ? ] with 10 bit resolution probably you > haven't time to do > averaging in the same time with taking samples. So you must > storage the > sampling results somewhere and then perform the sum and > square root ( both > with 16 or more bits ) > I'll love to see a sample code doing this job... > regards, Vasile > This code was snipped from a considerably larger more complex data-collection routine which also computed several other values, and also did two channels simultaneously. The complete routine keeps up with a sample rate of 4KHz (two channels, thus 8K samples/second) on a PIC16F876 at 20 MHz. Doing just the one channel and skipping the other time-consuming processing, I would expect this to be able to keep up with around 10K samples/second. Count the cycles. You can do a lot of signal processing with a PIC! The A/D samples are considered to be fractions -- with the binary point to the left of the sample: 10-bit ADC gives 9 bits plus sign (after conversion to two's complement). s.XXXXXXXXX where the X's are bits The sum is 24 bits: sXXXXXXX.XXXXXXXXXXXXXXXX Too much DC offset in the signal, and this will overflow, but this application did not present this problem. Sum of squares is 32 bits: sXXXXXXX.XXXXXXXXXXXXXXXXXXXXXXXX The postprocessing routine converts these to floating-point and then finishes things off that way. 32-bit floating-point runs at around 1K FLOPS on 20 MHz 16F876 using libraries supplied by Microchip. ; Table-based square routines eliminate one multiply... ; 30 cycles to compute 19-bit square of 10-bit signed number ; No information is lost at this point. sq_lo: movlw high sq_table_lo addwf samplemsb,w movwf PCLATH movfw samplelsb movwf PCL ;call for lo byte of square table sq_mid: movlw high sq_table_mid addwf samplemsb,w movwf PCLATH movfw samplelsb movwf PCL ;call for lo byte of square table sq_hi: movlw high sq_table_hi addwf samplemsb,w movwf PCLATH movfw samplelsb movwf PCL ;call for lo byte of square table movlw (high 4096) - 1 movwf nsam clrf nsam+1 clrf Xsum1_0 clrf Xsum1_1 clrf Xsum1_oflo clrf Xsum2_0 clrf Xsum2_1 clrf Xsum2_2 clrf Xsum2_oflo clrf Maxx clrf Maxx+1 movlw 0x3f movwf Minx movlw 0xff movwf Minx+1 ;Timer 1 is used to create interrupts for sampling the ADC (in this case at about 4000 Hz) bank0 clrf T1CON ;make sure it's all turned off. clrf TMR1L clrf TMR1H movlw (1<