'+--------------------------------------------------------------+ '| RMS convertor for MX170 | '+--------------------------------------------------------------+ ' The RMS value of a signal is the R(oot)M(ean)S(quare) value. ' It is calculated by adding up the squares of the input signal ' The mean of these squares is rooted. This process is explained ' in the steps below. ' This example uses the ADC in the PIC32MX170 to measure the ' AC input signal. Output is displayed at a 16x2 LCD. '---------------------- Defines ------------------------------- ADCpin = 23 ' ADC input pin Total = 800 ' array size for measuring 220mSec @ 48MHz Offset = 1.650 ' numeric offset at 3.300V CalValue = 24.85 ' gain factor attenuator and AC coupling @ 50Hz Option explicit '------------------------- Dim Variables -------------------- Dim Sample!(Total) Dim SumSQ!, Value! Dim i%,j%,a% '---------------------- Initialize hardware ------------------- SetPin ADCpin, AIN ' initialize ADC CPU 48 ' maximum speed for the MX170 'LCD LCD init 4,5,6,7,2,26 LCD 1,1,"Samples = " LCD 2,1,"RMS = V" '--------------------------- Measuring ---------------------- Do 'sample input signal For i%=1 To Total Sample(i%)=Pin(ADCpin)-Offset Next i% 'find zero crossings i%=0:j%=Total-1 'first zero crossing in array Do i%=i%+1 Loop Until(Sample(i%)>0 And Sample(i%+1)<0) 'last zero crossing in array Do j%=j%-1 Loop Until (Sample(j%)>0 And Sample(j%+1)<0) 'calculate SQUARES SumSQ!=0 For a%=i% To j% SumSQ! = SumSQ! + (Sample!(a%)*Sample!(a%)) Next a% 'calculate MEAN SumSQ! = SumSQ!/(j%-i%) 'calculate ROOT Value! = Sqr(SumSQ!) 'display values on 16x2 LCD LCD 1,11,Str$(j%-i%) LCD 2,7,Left$(Str$(Value!*CalValue!),5) Loop