News:

Let's find out together what makes a PIC Tick!

Main Menu

Reading NTC Thermister

Started by Roshan, Jun 21, 2024, 12:15 PM

Previous topic - Next topic

Roshan

Hi !
I have one arduino code, where i am reading ntc & it is working.

But when i tried to migrating in proton basic then code is not workng.

below is my arduino code.

Thermister one point is connected to vcc, another point is connect with 10K.
10K another point is connected to gnd.
middle point is connect with analog input of pic18F25K22



void loop() {
  totalRes = 0;
  for (int i =0; i < 32; i ++){
    res = analogRead(ThermistorPin);
    delay(1);
    totalRes = totalRes  + res;
  }
  Vo = totalRes >> 5;
  //Vo = analogRead(ThermistorPin);
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  Tc = T - 273.15;
 

  Serial.print("Temperature: ");
  //Serial.print(Tf);
  //Serial.print(" F; ");
  Serial.print(Tc);
  Serial.println(" C");   

  delay(500);
}

can some one convert that code to proton basic?

top204

That's using a Stein-Hart calculation to linearise the NTC thermistor's non-linear resistance to temperature changes.

There have been many examples posted on the forum for this type of calculation, and doing a search on my User folder, I have come up with a few that, I'm sure, I posted on the forum a while back.

For the Stein-Hart calculation to be accurate, you will need to know a few things about the NTC thermistor used, and these can be found in its datasheet.

For example, here's a code listing for an enhanced 14-bit core device, that also creates an Ln procedure for the Logarithm:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Perform a simple Steinhart-Hart calculation on an NTC thermistor.
' Written by Les Johnson for the Positron8 BASIC compiler.
'
    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 'fTemperature
    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

And below is a Stein-Hart code listing for a standard 14-bit core 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
    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                                 ' Right justified for 10=bit operation, with ADCS FOSC/8
    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

And below is a Stein-Hart code listing for the Amicus18 device, which is a standard 18F device. i.e. PIC18F25K20:

'
' Read a thermistor and run through a Steinhart-Hart calculation to get the actual temperature
'
    Include "Amicus18.inc"                  ' Configure the compiler to use a 18F25K20 at 64MHz
    Declare Float_Display_Type = Fast
'
' 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) Stein-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.
'
' Create a variable for the demo
'
    Dim fTemperature As Float               ' Holds the floating point temperature

'-----------------------------------------------------------------------------------------------------
' The main program loop starts here
'
Main:
    Do                                                      ' Create an infinite loop
        fTemperature = Thermistor_Read()                    ' Read and linearise the thermistor
        HRSOutLn Dec1 fTemperature                          ' Transmit the temperature in ASCII
        DelayMS 500                                         ' A delay so we can see what's happening
    Loop                                                    ' Do it forever

'-----------------------------------------------------------------------------------------------------
' Read the thermistor temperature sensor on AN0 (PORTA.0) and linearise the result
' Input     : None
' Output    : Returns the Temperature in degrees centigrade
' Notes     : The thermistor is sampled by the ADC with 10-bit resolution
'           : Then the raw thermistor value is run through a Steinhart-Hart calculation
'           : which linearises the value into degrees Centigrade.
'           : Accuracy depends on the thermistor.
'           : Based upon the thermistor used, this subroutine is accurate to approx +/- 0.5 degrees Centigrade.
'
Proc Thermistor_Read(), Float
'
' Constant values used for the Steinhart-Hart thermistor linearisation routine
'
    Symbol cQuanta = 5000.0 / 1024.0        ' Quatasising constant for ADC reading to Voltage calculation
    Symbol cT25 = 0.0033540                 ' Thermistor's t25 value
    Symbol cBeta = 1.0 / 3900.0             ' Thermistor's B value

    Dim fResistance As Float                ' Holds the resistance of the thermistor for a given temperature
    Dim fTemperature As Float               ' Holds the floating point temperature
    Dim fVoltage As Float                   ' Holds the voltage across the thermistor
    Dim fTemp As Float                      ' Temporary variable for the thermistor linearisation
    Dim fLn As Float                        ' Used to hold the Log of the thermistor value

    Dim ADC_wResult As ADRESL.Word

    ADCON2bits_ADFM = 1                                     ' Right justify the ADC result
    ANSELbits_ANS0 = 0                                      ' Make pin AN0 analogue
    ADC_wResult = ADIn 0                                    ' Get a reading from AN0
'
' Perform a Stein-Hart calculation to linearise the thermistor into degrees Centigrade
'
    fVoltage = ADC_wResult * cQuanta                        ' \
    fTemp = 5000.0 - fVoltage                               ' | Find the resistance across the thermistor
    fResistance = (fVoltage * 10000) / fTemp                ' /
    fLn = Log(fResistance / 10000)                          ' \
    Result = (cBeta * fLn) + cT25                           ' / Linearise the result using the Steinhart-Hart algorithm
    Result = (1.0 / Result) - 273.15                        ' Convert from Kelvin to Centigrade
EndProc

Pepe

Demo proteus

'****************************************************************
'*  Name    : UNTITLED.BAS                                      *
'*  Author  : [select VIEW...EDITOR OPTIONS]                    *
'*  Notice  : Copyright (c) 2024 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 21/6/2024                                         *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 18F25K22

Config_Start
  FOSC = INTIO67 ;Internal oscillator block
  PLLCFG = On ;Oscillator multiplied by 4
  PRICLKEN = On ;Primary clock enabled
  FCMEN = OFF ;Fail-Safe Clock Monitor disabled
  IESO = OFF ;Oscillator Switchover mode disabled
  PWRTEN = OFF ;Power up timer disabled
  BOREN = SBORDIS ;Brown-out Reset enabled in hardware only (SBOREN is disabled)
  BORV = 190 ;VBOR set to 1.90 V nominal
  WDTEN = OFF ;Watch dog timer is always disabled. SWDTEN has no effect.
  WDTPS = 32768 ;1:32768
  CCP2MX = PORTC1 ;CCP2 input/output is multiplexed with RC1
  PBADEN = OFF ;PORTB<5:0> pins are configured as digital I/O on Reset
  CCP3MX = PORTB5 ;P3A/CCP3 input/output is multiplexed with RB5
  HFOFST = On ;HFINTOSC output and ready status are not delayed by the oscillator stable status
  T3CMX = PORTC0 ;T3CKI is on RC0
  P2BMX = PORTB5 ;P2B is on RB5
  MCLRE = INTMCLR ;RE3 input pin enabled; MCLR disabled
  STVREN = OFF ;Stack full/underflow will not cause Reset
  LVP = OFF ;Single-Supply ICSP disabled
  XINST = OFF ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
  Debug = OFF ;Disabled
  Cp0 = OFF ;Block 0 (000800-001FFFh) not code-protected
  CP1 = OFF ;Block 1 (002000-003FFFh) not code-protected
  CP2 = OFF ;Block 2 (004000-005FFFh) not code-protected
  CP3 = OFF ;Block 3 (006000-007FFFh) not code-protected
  CPB = OFF ;Boot block (000000-0007FFh) not code-protected
  CPD = OFF ;Data EEPROM not code-protected
  WRT0 = OFF ;Block 0 (000800-001FFFh) not write-protected
  WRT1 = OFF ;Block 1 (002000-003FFFh) not write-protected
  WRT2 = OFF ;Block 2 (004000-005FFFh) not write-protected
  WRT3 = OFF ;Block 3 (006000-007FFFh) not write-protected
  WRTC = OFF ;Configuration registers (300000-3000FFh) not write-protected
  WRTB = OFF ;Boot Block (000000-0007FFh) not write-protected
  WRTD = OFF ;Data EEPROM not write-protected
  EBTR0 = OFF ;Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
  EBTR1 = OFF ;Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
  EBTR2 = OFF ;Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
  EBTR3 = OFF ;Block 3 (006000-007FFFh) not protected from table reads executed in other blocks
  EBTRB = OFF ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
Config_End

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
Declare Xtal = 64
Declare Optimiser_Level = 3
Declare Watchdog off

Declare Create_Coff On
Declare Hserial_Baud = 9600 ' Set the Baud rate for Hrsout
Declare Hserial_Clear = On
Declare HRSOut_Pin = PORTC.6


Symbol PLLEN = OSCTUNE.6 ' PLL enable
Symbol PLLRDY = OSCCON2.7 ' PLL run status

Symbol ADFM = ADCON2.7   ' A/D Result Format Select bit


 Declare Adin_Res 10
 Declare Adin_Tad 64_FOSC
 Declare Adin_Stime 20
 ANSELA = 1
 ANSELB = 0
 ANSELC = 0
 ADFM = 1


Dim Vo As Word
Dim R1 As Float = 10000
Dim logR2 As Float
Dim R2 As Float
Dim T As Float
Dim Tc As Float
Dim totalres As Word
Dim i As Byte
Dim wres As Word
Symbol c1 = 0.001009249522
Symbol c2 = 0.0002378405444
Symbol c3 = 0.0000002019202697


  OSCCON = $7c
  PLLEN = 1                                    ' Enable PLL 4x 16MHz = 64Mhz

  While PLLRDY = 0 : Wend

Do
  totalres = 0
  For i = 0 To 31
    wres = ADIn 0
    DelayMS 1
    totalres = totalres  + wres
  Next
  Vo = totalres >> 5
 
  R2 = R1 * ((1023.0 / Vo) - 1.0)
  logR2 = Log(R2)
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2))
  Tc = T - 273.15
 
  HRSOutLn "Temperature: ", Dec Tc , " C" ' Display the Tc value on a serial terminal

  DelayMS 500
Loop

keytapper

Quote from: Roshan on Jun 21, 2024, 12:15 PMbelow is my arduino code.
Your code missing a lot of variable definitions. Otherwise it would be a snap to make a conversion.
Ignorance comes with a cost

Pepe


Roshan

Quote from: Pepe on Jun 21, 2024, 03:23 PMDemo proteus

'****************************************************************
'*  Name    : UNTITLED.BAS                                      *
'*  Author  : [select VIEW...EDITOR OPTIONS]                    *
'*  Notice  : Copyright (c) 2024 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 21/6/2024                                         *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 18F25K22

Config_Start
  FOSC = INTIO67 ;Internal oscillator block
  PLLCFG = On ;Oscillator multiplied by 4
  PRICLKEN = On ;Primary clock enabled
  FCMEN = OFF ;Fail-Safe Clock Monitor disabled
  IESO = OFF ;Oscillator Switchover mode disabled
  PWRTEN = OFF ;Power up timer disabled
  BOREN = SBORDIS ;Brown-out Reset enabled in hardware only (SBOREN is disabled)
  BORV = 190 ;VBOR set to 1.90 V nominal
  WDTEN = OFF ;Watch dog timer is always disabled. SWDTEN has no effect.
  WDTPS = 32768 ;1:32768
  CCP2MX = PORTC1 ;CCP2 input/output is multiplexed with RC1
  PBADEN = OFF ;PORTB<5:0> pins are configured as digital I/O on Reset
  CCP3MX = PORTB5 ;P3A/CCP3 input/output is multiplexed with RB5
  HFOFST = On ;HFINTOSC output and ready status are not delayed by the oscillator stable status
  T3CMX = PORTC0 ;T3CKI is on RC0
  P2BMX = PORTB5 ;P2B is on RB5
  MCLRE = INTMCLR ;RE3 input pin enabled; MCLR disabled
  STVREN = OFF ;Stack full/underflow will not cause Reset
  LVP = OFF ;Single-Supply ICSP disabled
  XINST = OFF ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
  Debug = OFF ;Disabled
  Cp0 = OFF ;Block 0 (000800-001FFFh) not code-protected
  CP1 = OFF ;Block 1 (002000-003FFFh) not code-protected
  CP2 = OFF ;Block 2 (004000-005FFFh) not code-protected
  CP3 = OFF ;Block 3 (006000-007FFFh) not code-protected
  CPB = OFF ;Boot block (000000-0007FFh) not code-protected
  CPD = OFF ;Data EEPROM not code-protected
  WRT0 = OFF ;Block 0 (000800-001FFFh) not write-protected
  WRT1 = OFF ;Block 1 (002000-003FFFh) not write-protected
  WRT2 = OFF ;Block 2 (004000-005FFFh) not write-protected
  WRT3 = OFF ;Block 3 (006000-007FFFh) not write-protected
  WRTC = OFF ;Configuration registers (300000-3000FFh) not write-protected
  WRTB = OFF ;Boot Block (000000-0007FFh) not write-protected
  WRTD = OFF ;Data EEPROM not write-protected
  EBTR0 = OFF ;Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
  EBTR1 = OFF ;Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
  EBTR2 = OFF ;Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
  EBTR3 = OFF ;Block 3 (006000-007FFFh) not protected from table reads executed in other blocks
  EBTRB = OFF ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
Config_End

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
Declare Xtal = 64
Declare Optimiser_Level = 3
Declare Watchdog off

Declare Create_Coff On
Declare Hserial_Baud = 9600 ' Set the Baud rate for Hrsout
Declare Hserial_Clear = On
Declare HRSOut_Pin = PORTC.6


Symbol PLLEN = OSCTUNE.6 ' PLL enable
Symbol PLLRDY = OSCCON2.7 ' PLL run status

Symbol ADFM = ADCON2.7   ' A/D Result Format Select bit


 Declare Adin_Res 10
 Declare Adin_Tad 64_FOSC
 Declare Adin_Stime 20
 ANSELA = 1
 ANSELB = 0
 ANSELC = 0
 ADFM = 1


Dim Vo As Word
Dim R1 As Float = 10000
Dim logR2 As Float
Dim R2 As Float
Dim T As Float
Dim Tc As Float
Dim totalres As Word
Dim i As Byte
Dim wres As Word
Symbol c1 = 0.001009249522
Symbol c2 = 0.0002378405444
Symbol c3 = 0.0000002019202697


  OSCCON = $7c
  PLLEN = 1                                    ' Enable PLL 4x 16MHz = 64Mhz

  While PLLRDY = 0 : Wend

Do
  totalres = 0
  For i = 0 To 31
    wres = ADIn 0
    DelayMS 1
    totalres = totalres  + wres
  Next
  Vo = totalres >> 5
 
  R2 = R1 * ((1023.0 / Vo) - 1.0)
  logR2 = Log(R2)
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2))
  Tc = T - 273.15
 
  HRSOutLn "Temperature: ", Dec Tc , " C" ' Display the Tc value on a serial terminal

  DelayMS 500
Loop

Thanks sir, workking perfect :)