News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

PIC16 Series Float Square Root Operation

Started by tnencon, Jul 13, 2023, 05:52 PM

Previous topic - Next topic

tnencon

Hello everyone;
How can I get square root as floating point with PIC16F1827?

top204

See the sample file: "ACos_ASin_Sqrt.bas" at "C:\Users\User Name\PDS\Samples\New Samples\"

It has some floating point trigonometry procedures for 14-bit core and 18F devices, and these include Float_Sqrt.

trastikata

Quote from: top204 on Jul 13, 2023, 06:57 PMSee the sample file: "ACos_ASin_Sqrt.bas" at "C:\Users\User Name\PDS\Samples\New Samples\"
It has some floating point trigonometry procedures for 14-bit core and 18F devices, and these include Float_Sqrt.

This is the floating point approximation method, or I am mistaken?

I was wondering if absolute accuracy is required, how much processing time will take for the square root "long division" method to be implemented using Byte arrays and integer math?

I might give it a try these days if I find some free time.

tnencon

#3

Proc Square_Root(Temp As Word),Float
Temp = Temp - Offset_Value
Temp = Abs(Temp)
  Adc_Value = Temp / 250 'FVR = 4,096V
  Adc_Value = Adc_Value * Adc_Value 
  Rms_Value = Rms_Value + Adc_Value
  Sample_Count =Sample_Count + 1
  If Sample_Count = 64 Then
    Rms_Value = Rms_Value / Sample_Count
    FloatValueIn = Rms_Value
    FloatValueIn.Byte0 = (FloatValueIn.Byte0 + 127) >> 1
    Repeat
        Result = FloatValueIn
        FloatValueIn = FloatValueIn + (Rms_Value / FloatValueIn)
        FloatValueIn.Byte0 = FloatValueIn.Byte0 - 1
    Until Result = FloatValueIn
      Print At 2,2, "RMS : " ,Dec2 FloatValueIn,"  "
      Sample_Count = 0
      Rms_Value = 0
  EndIf
EndProc

top204

#4
I think it is based upon Newton's theory, but it also manipulates the floating point format bytes for some extra speed.

If memory serves, I found its principle in a book many years ago for the old 8-bit computers, so I adapted it for the compiler and added some finishing touches to it.

With trigonometry, I never went into much detailed study at school, and I am of the thought process: "If I do not need it, do not waste time studying it, as long as the knowledge that it exists is there, and the fundamentals of maths is understood fully. So if I ever need it, I can then work it out". :-)

So when I was adding floating point and trigonometry routines into the compilers, I studied them for a while, added them, then forgot what I had learned because I rarely use them at that fundamental level. But because I knew they existed and had a very basic knowledge of what they did, and can do all the other "real" mathematics well, they fell into place. But I still would not know what to do to find an adjacent arctan of something or other, or what a sine or cosine of a circle actually represents in any detail. LOL

I now need to learn to stop using commas in statements as much, but my thought process comes in waves with a small gap between statements, so a comma seems to fit in my mind. :-)

tnencon

#5
Do

    If Calculation = 1 Then
          FloatValueIn = 512
          For Dongu = 0 To 5
              FloatValueIn = (FloatValueIn+(Rms_Value_Old/FloatValueIn))/2 
          Next Dongu
            'FloatValueIn = FloatValueIn' - 512
            Print At 2,2,"RMS :",#Rms_Value_Old,"  "
            Print At 3,2,"SQRT :",Dec3 FloatValueIn,"  "
            Rms_Value_Old = 0
            Sample_Count = 0
            Calculation = 0       

    EndIf
Loop


If  PIR1bits_TMR1IF = 1 Then '312.5uS 20mS/312.5uS = 64 samples
    TMR1H = $D8
    TMR1L = $F0               
    Temp = ADIn 0
    CT_Value = Temp' - Offset_Value
    CT_Value = 260'Abs(CT_Value) 'This Value*************!!!!!!!!!
    CT_Value = CT_Value * CT_Value 
    Rms_Value = Rms_Value + CT_Value
    Sample_Count =Sample_Count + 1
    If Sample_Count = 64 Then  '64
      Calculation = 1
      Rms_Value_Old = Rms_Value/Sample_Count'>>6
      Sample_Count = 0
      Rms_Value = 0
    EndIf 

Greetings to all; In the above code, CT_Value is defined as Float type. If I give CT_Value = 256,257,258,260, it calculates incorrectly. It calculates correctly for 255,259, 261. I did not have a chance to try other values, but I could not find the reason. I would be very happy if you help.


**********************************************************************
FloatValueIn = 512
          For Dongu = 0 To 5
              FloatValueIn = (FloatValueIn+(Rms_Value_Old/FloatValueIn))/2 
          Next Dongu
***********************************************************************
This part of the code takes approximately the square root.