Scroll down to the end for a brute force integer square root algorithm.
I have a couple of questions that you need to answer when considering RMS calculations use a TriLog PLC:
- How quickly does your ADC input change?
- What is the "shape" of the input waveform? Sine, square, triangle, random?
- Is there you input a pure AC waveform? Is there a DC component?
The reason I ask the first question is that if you are trying to look at an input that changes quickly (50/60Hz) the PLC may not be fast enough or able to sample consistently enough to get data that you can use for your computations.
The reason I ask the second question is simple: if the input waveform has a predictable "shape" you don't need to do a formal RMS calculation, just a simple multiply of a constant.
The third question is a test. If you don't know why I asked this question, you don't need to compute RMS values nor do you need a square root algorithm.
OK, as promised a brute force algorithm
' Integer Square Root algorithm
'
' X is argument
'
' Temp variables used:
' T temporary root
' A temporary add
' B temporary result
'
' On Exit
' R is calculated square root
'
R = 0
A = 32768
WHILE (A)
T = R + A
B = T * T
IF (B <= X)
R = T
ENDIF
A = A/2
ENDWHILE
[/color]
Good luck,
Gary