how many lookup table can use to read ntc with PIC12FXXXX series?

Started by okmn, Jan 01, 2023, 06:07 PM

Previous topic - Next topic

okmn

hi guys  ,
i want to use 12f series pic for ntc read  with lookup tables 
Can I use multiple 255 elements (8 bits) "LookUp" and 85 elements (16 bits) "LookUpL" lookup tables in the same code for precision reading?

temperature reading range will be between 0 and 50 degrees Celsius


Maxi

Maybe you need Steinhart-Hart method

here is a 12F1822 code
Device = 12F1822
Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_ON, MCLRE_OFF, CP_ON, CPD_ON, BOREN_OFF, CLKOUTEN_OFF, IESO_OFF, FCMEN_OFF
Config2 WRT_OFF, PLLEN_OFF, STVREN_OFF, BORV_19, LVP_OFF
Declare Xtal=8

OSCCON.6=1
OSCCON.5=1
OSCCON.4=1
OSCCON.3=0

OSCCON.1=1
TRISA=%000100:PORTA=%000000

ADCON0=%00000011
ADCON1=%11111000
ANSELA=%00000100

Declare Adin_Res 10
Declare Adin_Tad FRC
Declare Adin_Stime 50

Dim RawADC As Float
Dim LastTemp As Byte

Dim Temp As Float
Dim RT As Float   
Dim LnRt As Float
Dim T As Float
Dim KUP As Float
   
'---------------------------
'BOSCH NTC 0 +60 +130
'0C: 5896
'60C: 596
'130C: 89

' ***** PULLUP 1K (rt resistor) ******
Dim Ra As Float
Ra= 0.0013020542532246
Dim Rb As Float
Rb= 0.000259186650396757
Dim Rc As Float
Rc= 0.000000166055357278685
'----------------------------

'LN ------------------
Dim LOG_VALUE As Float
Dim LOG_POWER As Float
Dim LOG_TEMP As Float
Dim LOG_X As Float
Dim LOG_FACTOR As Float
Dim LOG_XSQR As Float
Dim LOG_N As Byte
Dim LOG_TEMP2 As Byte
'----------------------


Main: 

RawADC = ADIn 2 'input adc pin 5
DelayMS 1
   
Temp = (1000 * RawADC)
Temp= Temp /(1023 - RawADC)
   
'---------------
LOG_VALUE = Temp
GoSub LN
LnRt = LOG_VALUE
'---------------

    KUP = (LnRt * LnRt) * LnRt

    T= Ra + (Rb * LnRt)
    T = T + (Rc * KUP)
    T= ( 1 / T )-273.15
   
    LastTemp= T
   
GoTo Main
       

'---------------------------------------------------------
LN:
        If LOG_VALUE.Byte0 = 0 Then LOG_VALUE = 0: Return
       
        If LOG_VALUE.Byte0 <= $7E Then
            LOG_N = $7E - LOG_VALUE.Byte0
            LOG_FACTOR = -0.69314718 * LOG_N
        Else
            LOG_N = LOG_VALUE.Byte0 - $7E
            LOG_FACTOR = 0.69314718 * LOG_N
        EndIf
        LOG_VALUE.Byte0 = $7E
       
       
        LOG_VALUE = LOG_VALUE - 1
        LOG_X = LOG_VALUE
        LOG_XSQR = LOG_VALUE
        LOG_N = 2
        Repeat
            LOG_XSQR = LOG_XSQR * LOG_X
            LOG_VALUE = LOG_VALUE - (LOG_XSQR / LOG_N)
            LOG_XSQR = LOG_XSQR * LOG_X
            LOG_TEMP2 = (LOG_N + 1)
            LOG_VALUE = LOG_VALUE + (LOG_XSQR / LOG_TEMP2)
            LOG_N = LOG_N + 2
        Until LOG_N > 12
        LOG_VALUE = LOG_VALUE + LOG_FACTOR
        Return
'----------------------------------------------------------

John Drew

Keep in mind that the floats in Maxi's code have limited accuracy and could never store the values shown. Check the manual for further info.
John

top204

The 12F devices come in three flavours. Very old, and now very redundant, 12-bit core devices. Old, and now redundant, standard 14-bit core devices, and the newer enhanced 14-bit core devices.

The very old 12-bit core devices are a no-no for any serious programs because they are so limited in RAM, call stack and flash memory.

The standard 14-bit core devices will work with flash memory tables of any size that fits within their flash memory.

The enhanced 14-bit core devices operate much better when accessing their flash memory, and again, can store data in as much flash as they have. They also handle floating point calculations better because of their better mnemonics.

It is better to use the Dim As Flashx or Cdata directives to create flash memory data tables, then a simple offset index to them for the value required. The LookUp and LookUpL and LookDown commands are throwbacks to the original BASIC Stamp II syntax that I made the compiler follow many, many years ago, so they are now very outdated. They work well, but are outdated. I kept them in the compilers for backward compatability.

As Maxi stated, you can do it with mathematics using a Steinhart-Hart calculation, and the listing below shows another way of using it to read an NTC thermistor with a 14-bit core device. You will need to find the Beta and t25 values for the thermistor you are using.

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Perform a simple Steinhart-Hart calculation on an NTC thermistor using an enhanced 14-bit core PIC12F1840 device.
' The calculations in the program listing are for a 10K Ohm NTC thermistor with a 10K Ohm resistor attached from it, to +5V
'
' Written by Les Johnson for the Positron8 BASIC compiler.
'
' The temperature constant Beta depends upon the material used in manufacturing the thermistor.
' This parameter also depends on temperature and manufacturing tolerances and as a result of this,
' the linearising equation below is only suitable for describing a restricted range around the rated temperature
' or resistance with sufficient accuracy.
'
'                1
' Tt = ----------------------
'      1/B Ln(Rt/R25) + 1/T25
'
' The Beta values for common NTC thermistors range from 2000K through to 5000K.
' For this application, the popular, and more complicated) Steinhart-Hart equation is more accurate.
'
' The thermistor used did not provide the Beta value within its datasheet, but did give the T25 value.
' i.e. resistance at 25 degrees Centigrade.
' To calculate the Beta value use the equation below.
'
'         Tt * T25         R25
' Beta = ---------- * Ln ------
'         Tt - T25         R100
'
' Or: -
'
'         (25 + 273.13) * (100 + 273.15)         R25
' Beta = -------------------------------- * Ln ------
'                    75                         R100
'
' Giving: -
'
'                     R25
' Beta = 1483.4 * Ln ------
'                     R100
'
' Thus, knowing the resistance at 25 degrees Centigrade and also at 100 degrees Centigrade
' we can use the above calculation to find the value of Beta for a given thermistor.
'
    Device = 12F1840                                            ' Choose the microcontroller the compiler will compile for
    Declare Xtal = 16                                           ' Inform the compiler we are using a device operating at 16MHz
    Declare Float_Display_Type = Fast                           ' More accurate and faster floating point to ASCII
'
' Setup USART1
'  
    Declare Hserial1_Baud = 9600
    Declare HRSOut1_Pin = PORTA.0
'
' Create global variables
'
    Dim fTemperature As Float                                   ' Holds the floating point temperature   
     
'--------------------------------------------------------
' The main program starts here
' Read an NTC thermistor and serially transmit its temperature
'
Main:
    Osc_16MHz()                                                 ' Set the microcontroller to use its internal oscillator at 16MHz       
    ADC_Init()                                                  ' Initialise the ADC
       
    Do
        fTemperature = Get_Temperature()                        ' Read the thermistor's temperature
        HRSOutLn "Temperature = ", Dec1 fTemperature, 176, "C"  ' Transmit the temperature in ASCII
        DelayMS 500                                             ' A delay so we can see what's happening
    Loop

'-----------------------------------------------------------------------------------------------------
' Read the thermistor temperature sensor on AN1 and linearise the result to degrees Centigrade
' Input     : None
' Output    : Returns the temperature in degrees Centigrade
' Notes     : The thermistor is read by the ADC with 10-bit resolution
'           : Then the raw thermistor resistance is run through a Steinhart-Hart calculation
'           : which linearises the value into degrees Centigrade.
'
Proc Get_Temperature(), Float
    Dim wResult As Result.Word0                         ' An alias to hold the 10-bit ADC value
    Dim fLog    As Result                               ' An alias to hold the Log of the thermistor value
    Dim fRes    As Result                               ' An alias to hold the resistance of the thermistor for a given temperature
    Dim fVolts  As Result                               ' An alias to hold the voltage across the thermistor

    Symbol cQuanta = 5000.0 / 1023.0                    ' Quatasising constant for ADC reading to Voltage calculation
    Symbol cT25 = 0.003361                              ' Thermistor's t25 value
    Symbol cBeta = 1.0 / 4050.0                         ' Thermistor's Beta value

    wResult = ADIn 1                                    ' Read the ADC on pin AN1
'
' Perform a Steinhart-Hart calculation to linearise the thermistor into degrees Centigrade
'
    fVolts = wResult * cQuanta                          ' \
    fRes = (fVolts * 10000) / (5000.0 - fVolts)         ' / Find the resistance across the thermistor  
    fLog = Ln(fRes / 10000)                             ' \
    Result = (cBeta * fLog) + cT25                      ' / Linearise the result using the steinhart-hart algorithm  
    Result = (1.0 / Result) - 273.15                    ' Convert from Kelvin to Centigrade
EndProc

'----------------------------------------------------------------------
' Floating Point Ln procedure
' Input     : pValue holds the value to perform the LN on
' Output    : Returns the result of the Ln
' Notes     : fFactor bTemp1 to get result >= 0.5 fX <= 1.0 and get bTemp1*ln2
'             The Exponent of the floating point values is bTemp1.Byte0
'             If bTemp1.Byte0 = $7E then the number is between 0.5 and 1.0 (2^-1 and 2^0)
'
Proc Ln(pValue As Float), pValue
    Dim fX      As Float
    Dim fFactor As Float
    Dim fXsqr   As Float
    Dim bTemp1  As Byte
    Dim bTemp2  As Byte
'
' We can't have ln(1) so we must return a zero if it is
'
    If pValue.Byte0 = 0 Then
        Result = 0
        ExitProc
    EndIf
'
' The difference between bTemp1.Byte0 and $7E will be the
' amount of 2^bTemp1's that we want to multiply times ln(2)
'
    If pValue.Byte0 <= $7E Then
        bTemp1 = $7E - pValue.Byte0
        fFactor = -0.69314718 * bTemp1
    Else
        bTemp1 = pValue.Byte0 - $7E
        fFactor = 0.69314718 * bTemp1
    EndIf
    pValue.Byte0 = $7E
'
' Begin the taylor series expansion
' ln(1+fX) = fX - (fX^2/2) + (fX^3/3) -+...
'
    pValue = pValue - 1
    fX = pValue
    fXsqr = pValue
    bTemp1 = 2
    Repeat
        fXsqr = fXsqr * fX
        pValue = pValue - (fXsqr / bTemp1)
        fXsqr = fXsqr * fX
        bTemp2 = (bTemp1 + 1)
        pValue = pValue + (fXsqr / bTemp2)
        bTemp1 = bTemp1 + 2
    Until bTemp1 > 12
    Result = pValue + fFactor
EndProc

'-------------------------------------------------------
' Initialise the ADC on a PIC12F1840 device
' Input     : None
' Output    : None
' Notes     : Setup for 10-bit operation
'           : Clock of Fosc/8
'           : Set pin AN1 as analogue input
'
Proc ADC_Init()
    ADCON1 = 0b10010000             ' Right justified for 10-bit operation, with ADCS FOSC/8 
    ANSELAbits_ANSA1 = 1            ' Make pin AN1 an analogue input
EndProc
      
'-------------------------------------------------------
' Setup the internal oscillator to operate at 16MHz on a PIC12F1840 device
' Input     : None
' Output    : None
' Notes     : None

Proc Osc_16MHz()    
    OSCCON = $7A                    ' 16MHz_HF
    OSCTUNE = $00
    BORCON = $00
EndProc
               
'-------------------------------------------------------
' Setup the config fuses for internal oscillator on a PIC12F1840 device
'   
    Config1 FOSC_INTOSC, WDTE_OFF, MCLRE_ON, CP_OFF, CPD_OFF, BOREN_OFF
    Config2 PLLEN_OFF, LVP_OFF, WRT_OFF, STVREN_OFF


top204

The same Steinhart-Hart calculations can be performed on a standard 14-bit core device, as long as it has enough RAM for the floating point routines and expressions. Below is a Steinhart-Hart demo listing for use on a PIC12F683 device:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Perform a simple Steinhart-Hart calculation on an NTC thermistor using a PIC12F683 device.
'
' Written by Les Johnson for the Positron8 BASIC compiler.
'
' The temperature constant Beta depends upon the material used in manufacturing the thermistor.
' This parameter also depends on temperature and manufacturing tolerances and as a result of this,
' the linearising equation below is only suitable for describing a restricted range around the rated temperature
' or resistance with sufficient accuracy.
'
'                1
' Tt = ----------------------
'      1/B Ln(Rt/R25) + 1/T25
'
' The Beta values for common NTC thermistors range from 2000K through to 5000K.
' For this application, the popular, and more complicated) Steinhart-Hart equation is more accurate.
'
' The thermistor used did not provide the Beta value within its datasheet, but did give the T25 value.
' i.e. resistance at 25 degrees Centigrade.
' To calculate the Beta value use the equation below.
'
'         Tt * T25         R25
' Beta = ---------- * Ln ------
'         Tt - T25         R100
'
' Or: -
'
'         (25 + 273.13) * (100 + 273.15)         R25
' Beta = -------------------------------- * Ln ------
'                    75                         R100
'
' Giving: -
'
'                     R25
' Beta = 1483.4 * Ln ------
'                     R100
'
' Thus, knowing the resistance at 25 degrees Centigrade and also at 100 degrees Centigrade
' we can use the above calculation to find the value of Beta for a given thermistor.
'
    Device = 12F683                                             ' Choose the microcontroller the compiler will compile for
    Declare Xtal = 8                                            ' Inform the compiler we are using a device operating at 8MHz
    Declare Float_Display_Type = Fast                           ' Use the more accurate and faster floating point to ASCII library routine
'
' Setup the Software Serial port
'
    Declare Serial_Baud = 9600
    Declare RSOut_Pin = GPIO.0
    Declare RSOut_Mode = 0
'
' Setup the ADC for the ADin command
'
    Declare Adin_Tad = FRC
    Declare Adin_Stime = 100
'
' Create global variables
'
    Dim fTemperature As Float                                   ' Holds the floating point temperature

'--------------------------------------------------------
' The main program starts here
' Read an NTC thermistor and serially transmit its temperature
'
Main:
    Setup()                                                     ' Setup the program and peripherals

    Do
        fTemperature = Get_Temperature()                        ' Read the thermistor's temperature
        RsOutLn "Temperature = ", Dec1 fTemperature, 176, "C"   ' Transmit the temperature in ASCII
        DelayMS 500                                             ' A delay so we can see what's happening
    Loop

'-----------------------------------------------------------------------------------------------------
' Read the thermistor temperature sensor on AN1 and linearise the result to degrees Centigrade
' Input     : None
' Output    : Returns the temperature in degrees Centigrade
' Notes     : The thermistor is read by the ADC with 10-bit resolution
'           : Then the raw thermistor resistance is run through a Steinhart-Hart calculation
'           : which linearises the value into degrees Centigrade.
'
Proc Get_Temperature(), Float
    Dim wResult As Result.Word0                             ' An alias to hold the 10-bit ADC value
    Dim fLog    As Result                                   ' An alias to hold the Log of the thermistor value
    Dim fRes    As Result                                   ' An alias to hold the resistance of the thermistor for a given temperature
    Dim fVolts  As Result                                   ' An alias to hold the voltage across the thermistor

    Symbol cVref = (5.0 * 1000)                             ' Voltage used for the ADC's +Vref and the thermistor's VDD
    Symbol cRes = 10000.0                                   ' Resistance of the thermistor and the feed resistor
    Symbol cQuanta = (cVref / 1023.0)                       ' Quantasising constant for ADC reading to voltage calculation
    Symbol cT25 = 0.003358                                  ' Thermistor's t25 value
    Symbol cBeta = (1.0 / 4050.0)                           ' Thermistor's Beta value

    wResult = ADIn 1                                        ' Read the ADC on pin AN1
'
' Perform a Steinhart-Hart calculation to linearise the thermistor into degrees Centigrade
'
    fVolts = wResult * cQuanta                              ' \
    fRes = (fVolts * cRes) / (cVref - fVolts)               ' / Find the resistance across the thermistor  
    fLog = Ln(fRes  / cRes)                                 ' \
    Result = (cBeta * fLog) + cT25                          ' / Linearise the result using the steinhart-hart algorithm  
    Result = (1.0 / Result) - 273.15                        ' Convert from Kelvin to Centigrade
EndProc

'----------------------------------------------------------------------
' Floating Point Ln procedure
' Input     : pValue holds the value to perform the LN on
' Output    : Returns the result of the Ln
' Notes     : fFactor bTemp1 to get result >= 0.5 fX <= 1.0 and get bTemp1*ln2
'             The Exponent of the floating point values is bTemp1.Byte0
'             If bTemp1.Byte0 = $7E then the number is between 0.5 and 1.0 (2^-1 and 2^0)
'
Proc Ln(pValue As Float), pValue
    Dim fX      As Float
    Dim fFactor As Float
    Dim fXsqr   As Float
    Dim bTemp1  As Byte
    Dim bTemp2  As Byte
'
' We can't have ln(1) so we must return a zero if it is
'
    If pValue.Byte0 = 0 Then
        Result = 0
        ExitProc
    EndIf
'
' The difference between bTemp1.Byte0 and $7E will be the
' amount of 2^bTemp1's that we want to multiply times ln(2)
'
    If pValue.Byte0 <= $7E Then
        bTemp1 = $7E - pValue.Byte0
        fFactor = -0.69314718 * bTemp1
    Else
        bTemp1 = pValue.Byte0 - $7E
        fFactor = 0.69314718 * bTemp1
    EndIf
    pValue.Byte0 = $7E
'
' Begin the taylor series expansion
' ln(1+fX) = fX - (fX^2/2) + (fX^3/3) -+...
'
    pValue = pValue - 1
    fX = pValue
    fXsqr = pValue
    bTemp1 = 2
    Repeat
        fXsqr = fXsqr * fX
        pValue = pValue - (fXsqr / bTemp1)
        fXsqr = fXsqr * fX
        bTemp2 = (bTemp1 + 1)
        pValue = pValue + (fXsqr / bTemp2)
        bTemp1 = bTemp1 + 2
    Until bTemp1 > 12
    Result = pValue + fFactor
EndProc

'-------------------------------------------------------
' Setup the program to be used on a PIC12F683 device
' Input     : None
' Output    : None
' Notes     : Setup the device to operate at 8MHz with its internal oscillator
'           : Setup the ADC for 10-bit operation
'           : Set pin AN1 as analogue input
'
Proc Setup()
    OSCCON = 0b01110000                                 ' Setup the microcontroller to operate on its internal oscillator running at 8MHz
    ADCON0bits_ADFM = 1                                 ' Make the ADC right justified for 10-bit operation
    ANSELbits_ANS1 = 1                                  ' Make pin AN1 an analogue input
EndProc

'-------------------------------------------------------
' Set the PIC12F683 device's config fuses to use the internal oscillator and the OSC pins as I/O pins
'
    Config INTOSCIO, WDT_OFF, PWRTE_ON, CP_OFF, MCLRE_ON

Even with all the floating point expressions, the above program listing only takes 64 bytes of RAM and 1533 words of flash memory on a PIC12F683 device. Not too bad at all. :-)

Maxi

Les, 12F683 code have some error
>general syntax error

*thanks HAL
problem solved!


HAL

Hi Maxi

I think if you delete the line where the compiler highlights the error (the line between Main: and Setup() then the code will compile.  Some extra character must have got in that line when transferring the program from the posted code. 

Give it a try - it worked for me.    ;) 

Best regards
Hal

top204

When copying from a screen listing, and depending on what web viewer is being used, extra, invisible, control characters can be added by it, so when the text is pasted into an IDE these are also added to the text. This will cause the compiler to give an error because the control values are ASCII, but are also made invisible in some IDEs.

If I am copying from a screen listing, I always copy from the screen listing, then paste into UltraEdit, then copy from UltraEdit into the IDE. UltraEdit removes some control characters automatically or shows them as a "?" character, so you can see that they are there and delete them.

I'm sure some other notepad type applications will do similar things, but I have used UltraEdit since the late 1990s, and I always use it for editing code and compiling etc... I wrote my books using UltraEdit for the code listings and demos. :-)

Both program listing above were compiled and tested in a simulator before I placed them on the forum. But my Photoshop 7 has stopped working, so I could not place a screenshot with the posts. That is one of my tasks today, get Photoshop 7 working, and my BCB C++ compiler so I can re-compile Positron16 and correct the stack anomaly and get the update posted on the forum with the corrections for Positron8 as well.

I made the mistake of allowing Windows 10 to do some updates and it has broken a lot of my programs that have worked for many years on this machine, so "NEVER" again will I allow it to update.

Gods

Quote from: top204 on Jan 04, 2023, 11:04 AMI made the mistake of allowing Windows 10 to do some updates and it has broken a lot of my programs that have worked for many years on this machine, so "NEVER" again will I allow it to update.

This is why i am still using Windows_7 on my main PC... I have one dual boot, Win7 and Linux Mint. Also, i am running windows_10 on a virtual machine. (my PC have a digital win10 license, so is actived even running on a virtual environement)
(sorry for my bad english)

tnencon

''*********************************************************
'x = The ratio of the value of NTC at the measured temperature to the NTC at 25 degrees
'Result= ln(x)
'ln (x)= 2 [ (x-1)/(x+1)  + (1/3)( (x-1)/(x+1) )^3 + (1/5) ( (x-1)/(x+1) )^5 + (1/7) ( (x-1)/(x+1) )^7 + ... ]    (x > 0) 
''*********************************************************
Proc ln_x(x As Float),Float
    If x = 0 Then
       Result = 0
       ExitProc
    EndIf
    sum = 1
    Value = 1
    y = (x-1)/(x+1)
    For i = 3 To 1000 Step 2
        Value = Value *(y*y)
        sum = sum + Value/i
    Next i
    Result = 2*y*sum
   
EndProc

top204

Thanks for the procedure outline.

I'll try the new Ln procedure out later today.

The procedure cannot compile as it stands because variables are missing within it, and single character variable names are not suported as local variables. So I will make it into a compilable procedure and check it and post it later today.

top204

I tried the new Ln procedure from tnencon and it works well. See below:

'---------------------------------------------------------------------------------  
' Floating Point Ln procedure
' Input     : pValue hold the value to calculate the Ln from
' Output    : Returns the Ln value
' Notes     : ln (pValue)= 2 [(pValue-1)/(pValue+1) + (1/3)( (pValue-1)/(pValue+1))^3 + (1/5) ((pValue-1)/(pValue+1))^5 + (1/7) ((pValue-1)/(pValue+1))^7 + ...] (pValue > 0)
'
Proc Ln_x(pValue As Float), Float
    Dim wLoop As Word
    Dim fTemp As Float
    Dim fSum As Float
    Dim fValue As Float
   
    If pValue.Byte0 = 0 Then
       Result = 0
       ExitProc
    EndIf
   
    fSum = 1
    fValue = 1
    fTemp = (pValue - 1) / (pValue + 1)
    For wLoop = 3 To 1000 Step 2
        fValue = fValue * (fTemp * fTemp)
        fSum = fSum + (fValue / wLoop)
    Next
    Result = (2 * fTemp) * fSum 
EndProc

However, because of the large loop, it takes a lot more time to perform the calculations and produces the same values as my original Ln procedure. For example, the new Ln_x procedure takes 155ms to operate on a 16MHz 18F device, while my original Ln procedure takes 3.56ms on the same device operating at the same speed. :-)

But many thanks for posting it.

tnencon

Quote from: top204 on Jan 05, 2023, 11:49 AMI tried the new Ln procedure from tnencon and it works well. See below:

However, because of the large loop, it takes a lot more time to perform the calculations and produces the same values as my original Ln procedure. For example, the new Ln_x procedure takes 155ms to operate on a 16MHz 18F device, while my original Ln procedure takes 3.56ms on the same device operating a the same speed. :-)

But many thanks for posting it.



First of all, thanks Les.
I think it works well even if the cycle used is done with 20-25 repetitions.