SteinHartEquation

SwordfishUser.SteinHartEquation History

Hide minor edits - Show changes to output

Changed lines 4-8 from:
Include "LCD.bas"
Include "math.bas"
Include "ADC.bas"
Include
"convert.bas"
to:
// LCD proton development board
    #option LCD_DATA = PORTD.4
    #option LCD_RS = PORTE.0
    #option LCD_EN = PORTE.1

    Include "LCD.bas"
    Include "math.bas"
    Include "ADC.bas"
   
Include "convert.bas"
Changed line 15 from:
 Thermistor linearisation using a Stein-Hart equasion
to:
 Thermistor linearization using a Stein-Hart equation
Changed line 21 from:
 +5 Volts
to:
  +5 Volts
Changed line 25 from:
   /  10k Ohm 1% (Or better) Resistor
to:
   /  10k Ohm 1% (Or better) Resistor
Added lines 40-74:

 The Stein-Hart 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 B 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 B value within its datasheet, but did give the T25 value.
 i.e. RESISTANCE At 25 degrees Centigrade.
 To calculate the B value use the equation below.

 Tt * T25 R25
 B = ---------- * LN ------
 Tt - T25 R100

 Or: -

 (25 + 273.13) * (100 + 273.15) R25
 B = -------------------------------- * LN ------
 75 R100

 giving: -

 R25
 B = 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 B For a given thermistor.
 
---------------------------------------------------------------------------------------
Changed lines 77-112 from:
// sort a word array... 
Sub BubbleSort(ByRef pArray() As Word)
  Dim SwapOccured As Boolean
  Dim index As Byte
  Dim SwapTemp As Word 
  Repeat
     SwapOccured = false
     index = 0
      Repeat
 
        If pArray(index) > pArray(index + 1) Then
            SwapTemp = pArray(index)
        pArray(index) = pArray(index + 1)
       pArray(index + 1) = SwapTemp
       SwapOccured = true
    EndIf
    Inc(index)
     Until index = Bound(pArray)
  Until Not SwapOccured
End Sub
 

// Get the median
Function GetMedian
(ByRef pSamples() As Word) As Word
  Dim index As Byte
  BubbleSort(pSamples)
  result = pSamples(Bound(pSamples) / 2)
End Function

// Perform the Stein Hart linierisation

Function SteinHart(pValue As Float) As Float
  Const T25Val = 0.0033540                    // Thermistor's t25 value
 
  Const ThermistorBVal = 1.0 / 3489.0         // Thermistor's B value
  Dim  LogValue As Float
      
  LogValue = log(pValue / 10000)            // Calculate the log
   Result = ThermistorBVal * LogValue + T25Val // Linearise using the stein-hart algorithm
End Function
to:
    // sort a word array...
    Sub BubbleSort(ByRef pArray() As Word)
     Dim SwapOccured As Boolean
 
    Dim index As Byte
      Dim SwapTemp As Word
      Repeat
 
         SwapOccured = false
          index = 0
          Repeat
             If pArray(index) > pArray(index + 1) Then
              SwapTemp = pArray(index)
 
            pArray(index) = pArray(index + 1)
                pArray(index + 1) = SwapTemp
             SwapOccured = true
   
       EndIf
 
            Inc(index)
          Until index = Bound(pArray)
       Until Not SwapOccured
    End Sub

   // Get the median of an array
 
  Function GetMedian(ByRef pSamples() As Word) As Word
      Dim index As Byte
      BubbleSort(pSamples)
      result = pSamples(Bound(pSamples) / 2)
 
   End Function

    // Perform the Stein Hart linearization taking into account the thermistors characteristics
    Function SteinHart(pValue As Float) As Float
        Const T25Val = 0.0033540                    // Thermistor's t25 value
        Const ThermistorBVal = 1.0 / 3489.0        // Thermistor's B value
        Dim  LogValue As Float

        LogValue = log(pValue / 10000)              // Calculate the log
        Result = ThermistorBVal * LogValue + T25Val  // linearize using the stein-hart algorithm
    End Function

//-----------------------------------------------------------------
Changed lines 116-131 from:
//-----------------------------------------------------------------
to:
   Const SymDegree(8) As Byte = ($06,$09,$09,$06,0,0,0,0) // Data for deg symbol in cgram char 0


    Dim SampleArray(20) As Word
    Dim TempF As Float
    Dim Temperature As Float
    Dim index As Byte
    Dim Resistance As Float
    Dim ThermistorValue As Word
    Const Quanta = 5000.0 / 1024.0                // Quantizing constant for ADC reading to Voltage calculation

    ADCON1 = $07                                  // PORTE as digital (LCD)
    TRISA.0 = 1                                    // configure AN0 as an input
    ADCON1.7 = 1                                  // set analogue input on PORTA.0

    DelayMS(200)
Changed lines 133-156 from:
Dim SampleArray(20) As Word
Dim testval As Float
Dim testvalres As Float
Dim index As Byte
Dim Resistance As Word
Dim tempw As Word
Const Quanta = 5000.0 / 1024.0
               // Quatasising constant for ADC reading to Voltage
                                              // calculation
   
ADCON1 = $07 // PORTE as digital (LCD)

While true
                                   // Make a loop       
  For index = 0 To Bound(SampleArray)        // Take 20 samples and store them in the array
      SampleArray(index) = ADC.Read(0)
  Next     
   tempw = GetMedian(SampleArray)            // Get the median of the array     
  testvalQuanta * tempw                 // Work out the voltage      
 
   // Convert that value into a resistance       
  testvalres = SteinHart(testval)           //            
  LCD.Cls                                    // Clear the LCD screen
  LCD.WriteAt(1,1, DecToStr(testvalres))
  DelayMS(500)                              // delay 500ms       
Wend
to:
   LCD.WriteItem(SymDegree)                      // Write the custom char data to the LCD
       
    While 1 = 1

        For index = 0 To Bound(SampleArray)
            SampleArray(index) = ADC.Read(0)      // Take 20 samples and store them in the array
        Next
        ThermistorValue = GetMedian(SampleArray)  // Then get the median of the array
       
        Temperature = ThermistorValue * Quanta    // Work out the voltage
       TempF = 5000 - Temperature               // Find the resistance across the thermistor

      Resistance = (Temperature * 10000) / TempF
 
      Temperature = SteinHart(Resistance)       // linearize with the Stein Hart calculation

        Temperature = (1 / Temperature) - 273.15  // Convert from Kelvin To Centigrade

        LCD.Cls

       LCD.WriteAt(1,1, FloatToStr(Temperature),0,"C ")

        DelayMS(500)                             // Delay 500ms

    Wend

    End
Added lines 1-101:
A Thermistor linearisation program using a Stein-Hart equation, originally written for the PROTON compiler by Les Johnston

=code [=
Include "LCD.bas"
Include "math.bas"
Include "ADC.bas"
Include "convert.bas"

{
 Thermistor linearisation using a Stein-Hart equasion
 based on a program written for the Proton compiler By
 Les Johnston

 The Program below is based On a 10K Ohm resistor As part of the thermistor's circuit: -

 +5 Volts
 --------
    |
    \
    /  10k Ohm 1% (Or better) Resistor
    \
    /
    |
    o-------------O To PORTA.0 of the PICmicro
    |
    \
  o/
    \  Thermistor
    /
    |
    |
  -----
  ---
    -
}

// sort a word array... 
Sub BubbleSort(ByRef pArray() As Word)
  Dim SwapOccured As Boolean
  Dim index As Byte
  Dim SwapTemp As Word 
  Repeat
      SwapOccured = false
      index = 0
      Repeat
        If pArray(index) > pArray(index + 1) Then
            SwapTemp = pArray(index)
        pArray(index) = pArray(index + 1)
        pArray(index + 1) = SwapTemp
        SwapOccured = true
    EndIf
    Inc(index)
      Until index = Bound(pArray)
  Until Not SwapOccured
End Sub 

// Get the median
Function GetMedian(ByRef pSamples() As Word) As Word
  Dim index As Byte
  BubbleSort(pSamples)
  result = pSamples(Bound(pSamples) / 2)
End Function

// Perform the Stein Hart linierisation
Function SteinHart(pValue As Float) As Float
  Const T25Val = 0.0033540                    // Thermistor's t25 value
  Const ThermistorBVal = 1.0 / 3489.0        // Thermistor's B value
  Dim  LogValue As Float
       
  LogValue = log(pValue / 10000)              // Calculate the log
  Result = ThermistorBVal * LogValue + T25Val // Linearise using the stein-hart algorithm
End Function
   
//-----------------------------------------------------------------
   
Dim SampleArray(20) As Word
Dim testval As Float
Dim testvalres As Float
Dim index As Byte
Dim Resistance As Word
Dim tempw As Word
Const Quanta = 5000.0 / 1024.0                // Quatasising constant for ADC reading to Voltage
                                              // calculation
   
ADCON1 = $07 // PORTE as digital (LCD)

While true                                    // Make a loop       
  For index = 0 To Bound(SampleArray)        // Take 20 samples and store them in the array
      SampleArray(index) = ADC.Read(0)
  Next     
  tempw = GetMedian(SampleArray)            // Get the median of the array     
  testval =  Quanta * tempw                  // Work out the voltage       
 
  // Convert that value into a resistance       
  testvalres = SteinHart(testval)            //           
  LCD.Cls                                    // Clear the LCD screen
  LCD.WriteAt(1,1, DecToStr(testvalres))
  DelayMS(500)                              // delay 500ms       
Wend
=]