a GoSub AvgToRMS ¶ GoTo A *** Rectangular wave, average to rms conversion **** ; This subroutine will convert a duty cycle in W into an ; rms value. ;Theory: ;Consider a PWM current with a rectangular waveform: ;_____ _____ _____ ; _____________ ____________ ; ;The on time / total cycle time is duty cycle 'd'. ;If the On voltage is 1 (normalised) the average voltage ; is 1*d, eg. 1V at 10% is an average of 0.1V ;However the RMS (heating effect) is different. It is given ;as Sqrt(d). ;The ratio RMS/Average is Sqrt(d)/d ;As many measurement systems measure average, the latter ratio ;is useful for converting to RMS, and is what this routine calculates. ;Enter with d in W as a normalised number 0-1, return with ; W = RMS/Average. ;Method: Uses a lookup table in NVEM0. This has 100 steps. ;The resulting error is 1% of full scale at 25% duty cyle, rising ;to 4% of full scale at 1% duty cycle and dropping to 0.5% FS at 99% ;duty cycle. ;Boundary values: ; W Result ; -ve 0 ; <.005 0 See note ; >=100 1 ;Note: For very small values the result should be very high. In practical terms ; duty cycles down in the 0.5% or less range probably mean nothing. ; Consider too that the PWM hardware generally quantizes to 0.1% (10-bit) ;How to use it: ;Suppose you have set a PWM output to 0.23 using fAnOut. You measure ;an average load current of 0.6723 using fAnIn. What is the RMS current? ;The average load current scale is 5A full scale. ;Load the PWM value d=0.23 into W and call AvgToRMS. The returned value is 2.085144 ;Multiply that by 0.6723 to get 1.402 ;Multiply that by 5A to get 7.01A RMS. ;NOTE: This is an estimate and assumes a rectangular current waveform ; That would be the case ONLY for a resistive load, like a heater. ; For a motor, for example, it would only be approximately valid if the PWM frequency ; is so high that the current is substantially constant during the whole ; switching cycle. That means the PWM period T is much shorter than ; the LR time constant of the motor. AvgToRMS: fGoIfPos AvgToRMS_a ;1st bounds check fLoadW 0 Return AvgToRMS_a: fLoadQ 100 fMUL ;Scale to 0-100 fLoadQ 0.5 fAdd ;Round up fLoadQ 100 fSwap fSUB ;Value - 100 ... Test if >100 fGoIfNEG AvgToRMS_b ;G/ Val <100 fLoadW 1 Return AvgToRMS_b: fSwap Fix ;X should be 0 - 100 NVSetPage 0 NVSetPtr AvgToRMS_Table NVSetRecLen 4 NVPopRecNum NVfReadW 0 Return ******************************************************** ;The following table must be at the end of your program: ******************************************************** NVEM0 AvgToRMS_Table: NV0fNum 0.000000 ;Duty cycle 0% NV0fNum 10.000000 ;Duty cycle 1% NV0fNum 7.071068 ;Duty cycle 2% NV0fNum 5.773503 ;Duty cycle 3% NV0fNum 5.000000 ;Duty cycle 4% NV0fNum 4.472136 ;Duty cycle 5% NV0fNum 4.082483 ;Duty cycle 6% NV0fNum 3.779645 ;Duty cycle 7% NV0fNum 3.535534 ;Duty cycle 8% NV0fNum 3.333333 ;Duty cycle 9% NV0fNum 3.162278 ;Duty cycle 10% NV0fNum 3.015113 ;Duty cycle 11% NV0fNum 2.886751 ;Duty cycle 12% NV0fNum 2.773501 ;Duty cycle 13% NV0fNum 2.672612 ;Duty cycle 14% NV0fNum 2.581989 ;Duty cycle 15% NV0fNum 2.500000 ;Duty cycle 16% NV0fNum 2.425356 ;Duty cycle 17% NV0fNum 2.357023 ;Duty cycle 18% NV0fNum 2.294157 ;Duty cycle 19% NV0fNum 2.236068 ;Duty cycle 20% NV0fNum 2.182179 ;Duty cycle 21% NV0fNum 2.132007 ;Duty cycle 22% NV0fNum 2.085144 ;Duty cycle 23% NV0fNum 2.041241 ;Duty cycle 24% NV0fNum 2.000000 ;Duty cycle 25% NV0fNum 1.961161 ;Duty cycle 26% NV0fNum 1.924501 ;Duty cycle 27% NV0fNum 1.889822 ;Duty cycle 28% NV0fNum 1.856953 ;Duty cycle 29% NV0fNum 1.825742 ;Duty cycle 30% NV0fNum 1.796053 ;Duty cycle 31% NV0fNum 1.767767 ;Duty cycle 32% NV0fNum 1.740777 ;Duty cycle 33% NV0fNum 1.714986 ;Duty cycle 34% NV0fNum 1.690309 ;Duty cycle 35% NV0fNum 1.666667 ;Duty cycle 36% NV0fNum 1.643990 ;Duty cycle 37% NV0fNum 1.622214 ;Duty cycle 38% NV0fNum 1.601282 ;Duty cycle 39% NV0fNum 1.581139 ;Duty cycle 40% NV0fNum 1.561738 ;Duty cycle 41% NV0fNum 1.543033 ;Duty cycle 42% NV0fNum 1.524986 ;Duty cycle 43% NV0fNum 1.507557 ;Duty cycle 44% NV0fNum 1.490712 ;Duty cycle 45% NV0fNum 1.474420 ;Duty cycle 46% NV0fNum 1.458650 ;Duty cycle 47% NV0fNum 1.443376 ;Duty cycle 48% NV0fNum 1.428571 ;Duty cycle 49% NV0fNum 1.414214 ;Duty cycle 50% NV0fNum 1.400280 ;Duty cycle 51% NV0fNum 1.386750 ;Duty cycle 52% NV0fNum 1.373606 ;Duty cycle 53% NV0fNum 1.360828 ;Duty cycle 54% NV0fNum 1.348400 ;Duty cycle 55% NV0fNum 1.336306 ;Duty cycle 56% NV0fNum 1.324532 ;Duty cycle 57% NV0fNum 1.313064 ;Duty cycle 58% NV0fNum 1.301889 ;Duty cycle 59% NV0fNum 1.290994 ;Duty cycle 60% NV0fNum 1.280369 ;Duty cycle 61% NV0fNum 1.270001 ;Duty cycle 62% NV0fNum 1.259882 ;Duty cycle 63% NV0fNum 1.250000 ;Duty cycle 64% NV0fNum 1.240347 ;Duty cycle 65% NV0fNum 1.230915 ;Duty cycle 66% NV0fNum 1.221694 ;Duty cycle 67% NV0fNum 1.212678 ;Duty cycle 68% NV0fNum 1.203859 ;Duty cycle 69% NV0fNum 1.195229 ;Duty cycle 70% NV0fNum 1.186782 ;Duty cycle 71% NV0fNum 1.178511 ;Duty cycle 72% NV0fNum 1.170411 ;Duty cycle 73% NV0fNum 1.162476 ;Duty cycle 74% NV0fNum 1.154701 ;Duty cycle 75% NV0fNum 1.147079 ;Duty cycle 76% NV0fNum 1.139606 ;Duty cycle 77% NV0fNum 1.132277 ;Duty cycle 78% NV0fNum 1.125088 ;Duty cycle 79% NV0fNum 1.118034 ;Duty cycle 80% NV0fNum 1.111111 ;Duty cycle 81% NV0fNum 1.104315 ;Duty cycle 82% NV0fNum 1.097643 ;Duty cycle 83% NV0fNum 1.091089 ;Duty cycle 84% NV0fNum 1.084652 ;Duty cycle 85% NV0fNum 1.078328 ;Duty cycle 86% NV0fNum 1.072113 ;Duty cycle 87% NV0fNum 1.066004 ;Duty cycle 88% NV0fNum 1.059998 ;Duty cycle 89% NV0fNum 1.054093 ;Duty cycle 90% NV0fNum 1.048285 ;Duty cycle 91% NV0fNum 1.042572 ;Duty cycle 92% NV0fNum 1.036952 ;Duty cycle 93% NV0fNum 1.031421 ;Duty cycle 94% NV0fNum 1.025978 ;Duty cycle 95% NV0fNum 1.020621 ;Duty cycle 96% NV0fNum 1.015346 ;Duty cycle 97% NV0fNum 1.010153 ;Duty cycle 98% NV0fNum 1.005038 ;Duty cycle 99% NV0fNum 1.000000 ;Duty cycle 100%