OOOOK. Don, you gave me another ( complicated ) subject to think at. Thanks very much. Vasile On Wed, 14 Nov 2001, Don Hyde wrote: > 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< movwf T1CON > > Do_a_sample: > > bank0 > btfss X_Data_Ready ;interrupt routine sets this bit when data's ready > goto $-1 ;loop until some data is ready. > movfw ADC_X_MSB > bank3 > movwf samplemsb > movwf sample10+1 > bank0 > movfw ADC_X_LSB > bcf X_Data_Ready > bank3 > movwf samplelsb > movwf sample10 > > > ; Along the way, determine the max and min values as well for peak > measurement. > > movf samplelsb,w > subwf Maxx+1,w > movf samplemsb,w > skpc > incf samplemsb,w ;propagate the carry from the low byte subtraction. > subwf Maxx,w > skpnc > goto Maxx_is_greater > > movfw samplelsb > movwf Maxx+1 > movfw samplemsb > movwf Maxx > goto Convert_x_to_signed > > Maxx_is_greater: > > movf Minx+1,w > subwf samplelsb,w > movf Minx,w > skpc > incf Minx,w ;propagate the carry from the low byte subtraction. > subwf samplemsb,w > skpnc > goto Convert_x_to_signed > > movfw samplelsb > movwf Minx+1 > movfw samplemsb > movwf Minx > > Convert_x_to_signed: > > movlw 2 > xorwf sample10+1,f > > rrf sample10,w ; Convert sample to fractional form (S.15) > rrf sample10+1,f > rrf sample10,f > rrf sample10,w > rrf sample10+1,f > rrf sample10,f ;6 > > ; Extend sign bit into BARGB2 > clrf BARGB2 > btfsc sample10,7 > decf BARGB2,f > > movfw sample10+1 ;Accumulate sum x > addwf Xsum1_1,f > > movfw sample10 > skpnc > incfsz sample10,w > addwf Xsum1_0,f > > movfw BARGB2 ;extended sign > skpnc > incfsz BARGB2,w > addwf Xsum1_oflo,f > > btfsc samplemsb,1 ;is it negative? > goto xwaspositive ;two's complement it > comf samplelsb,f > incf samplelsb,f > skpnz > decf samplemsb,f > comf samplemsb,f > xwaspositive: ;3 or 7 > movlw 1 > andwf samplemsb,f > call sq_lo ; 10 ;call for lo byte of square table > movwf AARGB2 > > movlw high $ > movwf PCLATH ;3 > call sq_mid ;10 > movwf AARGB1 > > movlw high $ > movwf PCLATH ;3 > call sq_hi ;10 > movwf AARGB0 > movlw high $ > movwf PCLATH ;3 > > ; Accumulate to sum squared. > > movfw AARGB2 > addwf Xsum2_2,f > movfw AARGB1 > skpnc > incfsz AARGB1,w > addwf Xsum2_1,f > movfw AARGB0 > skpnc > incfsz AARGB0,w > addwf Xsum2_0,f > skpnc > incf Xsum2_oflo,f ;12 > > decfsz nsam+1,f > goto Do_a_sample > decfsz nsam,f > goto Do_a_sample > > goto PostProcessing > > -- > http://www.piclist.com hint: The list server can filter out subtopics > (like ads or off topics) for you. See http://www.piclist.com/#topics > > -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.