Autor Tema: control temperatura con Termistor ntc y basic plus proton  (Leído 5629 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado augustocortes

  • PIC10
  • *
  • Mensajes: 4
control temperatura con Termistor ntc y basic plus proton
« en: 13 de Enero de 2013, 20:06:34 »
Saludos a todos soy novato y siempre me ha apasionado la electronica, cuantos años que se ha perdido por no tener internet al vivir el el campo, que bien encontrar sitios como este para aprender y compartir experiencias.
Estoy intentando hacer mi segundo proyecto  con un pic 18f4550 , que es para empezar hacer un controlador de temperatura  y despues seguire con el modulo gsm de telefono por teclado, demasiado proyecto para un novato verdad y sin  experiencia en programacion recien estoy empezando. Ahora he empezado ha estudiar la ecuacion para la linealizacion de la ntc, ya he compredido algo y ahi algunas calculadoras Steinhart-Hart Thermistor en internet.
Voy a empezar a ver si se puede cambiar  Symbol thermistor = PORTA.0 y Symbol TestLED = PORTB.0 por registros para que funcione un  lcd de dos lineas ,Declare LCD_Lines 2,Declare LCD_DTPin PORTB.0,Declare LCD_ENPin PORTB.5 ,Declare LCD_RSPin PORTB.4 ,que es como yo lo entiendo. Si alguien quiere participar gracias, asi aprendere mas rapido.
Aqui os dejo el codigo que he encontrado en internet . Calculo de ntc de 10k


Device 18F4550              'PIC Utilizado
Xtal = 4                         'Oscilador de 4MHZ
'===== ALIAS DEFINITIONS =======================================================
    Symbol Thermistor = PORTA.0    'Thermistor Divider Circuit input
    Symbol TestLED = PORTB.0       'LED Used for various purposes
'===============================================================================
'FLOAT_DISPLAY_TYPE = FAST   'More accurate floating point to decimal routine
'===== VARIABLE DEFINITIONS ====================================================
    Dim i As Word ' Create a generic Word size variable

    Dim ThermistorInput_Adval As Word 'Variable to store the thermistor A to D value (0-1023)

    Dim Steinhart_a As Float  'Thermistor Constant Values
    Dim Steinhart_b As Float  'Thermistor Constant Values
    Dim Steinhart_c As Float  'Thermistor Constant Values

    Dim x0 As Float 'General Purpose variables used for for storage during calculations      
    Dim x1 As Float 'General Purpose variables used for for storage during calculations    
    Dim x2 As Float 'General Purpose variables used for for storage during calculations    
    Dim x3 As Float 'General Purpose variables used for for storage during calculations    
    Dim x4 As Float 'General Purpose variables used for for storage during calculations    
    Dim x5 As Float 'General Purpose variables used for for storage during calculations    
    Dim mV As Float 'Resolution * Adval
    Dim mVolts As Float

    Dim TEMPORARY As Float 'Temporary variable for the thermistor linearisation
    Dim RESISTANCE As Float  'Holds the resistance of the thermistor for a given temperature
    Dim lnResistance As Float  'Holds the "ln(Resistance)" value
    Dim lnResistanceCubed As Float  'Holds the "ln(Resistance)³" value
    Dim Kelvin As Float 'Holds the floating point temperature in degrees Kelvin
    Dim Fahrenheit As Float 'Holds the floating point temperature in degrees Fahrenheit
    Dim Celsius As Float 'Holds the floating point temperature in degrees Celsius
'===============================================================================

'===== CONSTANTS DEFINITIONS ===================================================
    'Steinhart-Hart Algorithm Constants
    'Constant values used for the Steinhart-Hart thermistor linearisation routine
    'T = 1/ ( a + b(lnR) + c(lnR)³)
    Steinhart_a = 0.001125498166      'Thermistor's a-value: From Data Sheet
    Steinhart_b = 0.0002346771694     'Thermistor's b-value: From Data Sheet
    Steinhart_c = 0.00000008579674698 'Thermistor's c-value: From Data Sheet
    Symbol Vref = 3300.00   'Constant used for Vref
    Symbol Resolution = Vref / 1024.00 'Quatasising constant for ADC reading to Voltage calculation
    Symbol Resistor_value = 16900         'Value of Resistor in Resistor/Thermistor voltage divider circuit
'===============================================================================


'===== Main Loop ==============================================================
GetTemp:
    GoSub GetAdval
    GoSub ConvertAdvalToTemp
    GoSub DisplayTemp
    DelayMS 500
Return
'===============================================================================

'===== SUBROUTINE ==============================================================
GetAdval:
' Read the value from the Analogue to Digital Converter AN0
ThermistorInput_Adval = ADIn 0
Return
'===============================================================================

'===== SUBROUTINE ==============================================================
ConvertAdvalToTemp:
    'Start Steinhart-Hart Linerization Algorithm
    ' FORMULA relating Thermistor Resistance to Temperature:
        '[ T = 1/ ( a + b(lnR) + c(lnR)³) ]
    'Where:
        'T = Temp in Kelvin
        'lnR = Natural Log of Resistance
        'a, b & c, are Thermistor constants from data sheet
        
        mV = Resolution * ThermistorInput_Adval
        TEMPORARY = Vref - mV
        x0 = Resolution * ThermistorInput_Adval
        x1 = x0 * Resistor_value
        RESISTANCE = x1 / TEMPORARY

    'Start log calculation here
    Calculate_lnResistance:              '...Command not availible for 16F PICs
        lnResistance = Log RESISTANCE    'Deduce the Natural Logarithm a value
  
    'Continue Steinhart-Hart Algorithm
    Steinhart_Hart_Algorithm:
        x2 = lnResistance * lnResistance
        lnResistanceCubed = lnResistance * x2
        x3 = Steinhart_b * lnResistance
        x4 = Steinhart_c * lnResistanceCubed
        x5 = (Steinhart_a + x3 + x4)
        
        Kelvin = 1 / x5
        Celsius = Kelvin - 273.15
        Fahrenheit = (Kelvin * 1.8) - 459.67

    'End of Steinhart-Hart Algorithm
Return
'===============================================================================

'===== SUBROUTINE ==============================================================
DisplayTemp: ' Transmit the result serially
    HRSOut "Result (In Fahrenheit) = ", Dec1 Fahrenheit, 13,10  '13=CR & 10=LF
    DelayMS 100
    HRSOut "Result (In Celsius) = ", Dec1 Celsius, 13,10        '13=CR & 10=LF
    DelayMS 100
    HRSOut "Result (In Kelvin) = ", Dec1 Kelvin, 13,10,13,10    '13=CR & 10=LF
    DelayMS 100
Return
« Última modificación: 14 de Enero de 2013, 06:18:42 por augustocortes »

Desconectado Picuino

  • Moderadores
  • DsPIC33
  • *****
  • Mensajes: 5892
    • Picuino
Re: control temperatura con Termistor ntc y basic plus proton
« Respuesta #1 en: 14 de Enero de 2013, 11:22:23 »
Interesante proyecto.

Veo que el código utiliza muchas variables para almacenamiento temporal.

¿Has pensado en programarlo en C?

Saludos.

Desconectado dogflu66

  • Moderador Local
  • DsPIC30
  • *****
  • Mensajes: 3549
Re: control temperatura con Termistor ntc y basic plus proton
« Respuesta #2 en: 14 de Enero de 2013, 11:33:41 »
Parece igual o muy parecido al Basic del PSI, aunque le veo algún fallo en la estructura del código voy a compilarlo a ver que pasa.
Saludos desde Granada, España.

Desconectado augustocortes

  • PIC10
  • *
  • Mensajes: 4
Re: control temperatura con Termistor ntc y basic plus proton
« Respuesta #3 en: 14 de Enero de 2013, 19:03:49 »
Gracias Picuino por la indicacion de programarlo en C , tambien seria interesante, he visto que circulan bastantes ejemplos en la web,pero todabia no se como funciona el copilador, estoy empezando con lo copiladores Basic  que he conseguido que funcionen bien y me parece mejor la comprension del codigo.
« Última modificación: 15 de Enero de 2013, 08:12:42 por augustocortes »

Desconectado augustocortes

  • PIC10
  • *
  • Mensajes: 4
Re: control temperatura con Termistor ntc y basic plus proton
« Respuesta #4 en: 14 de Enero de 2013, 19:12:41 »
Si lo copilas es posible que te salga un mensaje de aventencia Float variable "Resistance crossing bank boundary access" ,pero la copilacion sale bien al 100% en verde, ¿funcionara?,habra  que probar en la protoboard.

Desconectado dogflu66

  • Moderador Local
  • DsPIC30
  • *****
  • Mensajes: 3549
Re: control temperatura con Termistor ntc y basic plus proton
« Respuesta #5 en: 14 de Enero de 2013, 19:45:49 »
He hecho la adaptación a mi lenguaje y también corregí un error grave en la rutina Main Loop (gettemp).

'===== Main Loop ==============================================================
gettemp:
   Gosub getadval
   Gosub convertadvaltotemp
   Gosub displaytemp
   WaitMs 5
Goto gettemp  'return

Adjunto una imagen del resultado, no he comprobado si es correcto pero en caso de error seria cuestión de revisar los cálculos.

http://imageshack.us/photo/my-images/833/adaptacinlecturantcapsi.jpg

Con respecto a que funcione sobre un hardware ya es otro tema, porque no se han definido los registros de los módulos del pic y, que funcione como está será suerte y depende de lo que el compilador programe por defecto y, coincida con la configuración de fuses que necesitamos.

Código: FreeBasic
  1. 'device 18f4550  'PIC Utilizado
  2. Define CLOCK_FREQUENCY = 4  'Oscilador de 4MHZ
  3. Hseropen 4800
  4. '===== ALIAS DEFINITIONS =======================================================
  5. Symbol thermistor = PORTA.0  'Thermistor Divider Circuit input
  6. Symbol testled = PORTB.0  'LED Used for various purposes
  7. '===============================================================================
  8. 'single_DISPLAY_TYPE = FAST   'More accurate singleing point to decimal routine
  9. '===== VARIABLE DEFINITIONS ====================================================
  10. Dim i As Word  'Create a generic Word size variable
  11.  
  12. Dim thermistorinput_adval As Word  'Variable to store the thermistor A to D value (0-1023)
  13.  
  14. Dim steinhart_a As Single  'Thermistor Constant Values
  15. Dim steinhart_b As Single  'Thermistor Constant Values
  16. Dim steinhart_c As Single  'Thermistor Constant Values
  17.  
  18. Dim x0 As Single  'General Purpose variables used for for storage during calculations
  19. Dim x1 As Single  'General Purpose variables used for for storage during calculations
  20. Dim x2 As Single  'General Purpose variables used for for storage during calculations
  21. Dim x3 As Single  'General Purpose variables used for for storage during calculations
  22. Dim x4 As Single  'General Purpose variables used for for storage during calculations
  23. Dim x5 As Single  'General Purpose variables used for for storage during calculations
  24. Dim mv As Single  'Resolution * Adval
  25. Dim mvolts As Single
  26.  
  27. Dim temporary As Single  'Temporary variable for the thermistor linearisation
  28. Dim resistance As Single  'Holds the resistance of the thermistor for a given temperature
  29. Dim lnresistance As Single  'Holds the "ln(Resistance)" value
  30. Dim lnresistancecubed As Single  'Holds the "ln(Resistance)³" value
  31. Dim kelvin As Single  'Holds the singleing point temperature in degrees Kelvin
  32. Dim fahrenheit As Single  'Holds the singleing point temperature in degrees Fahrenheit
  33. Dim celsius As Single  'Holds the singleing point temperature in degrees Celsius
  34. '===============================================================================
  35. Dim resolution As Single
  36. '===== CONSTANTS DEFINITIONS ===================================================
  37. 'Steinhart-Hart Algorithm Constants
  38. 'Constant values used for the Steinhart-Hart thermistor linearisation routine
  39. 'T = 1/ ( a + b(lnR) + c(lnR)³)
  40. steinhart_a = 0.00112549  '8166  'Thermistor's a-value: From Data Sheet
  41. steinhart_b = 0.00023467  '71694  'Thermistor's b-value: From Data Sheet
  42. steinhart_c = 0.00000008  '579674698  'Thermistor's c-value: From Data Sheet
  43. Const vref = 3300  'Constant used for Vref
  44. 'define resolution = vref  / 1024  'Quatasising constant for ADC reading to Voltage calculation
  45. Const resistor_value = 16900  'Value of Resistor in Resistor/Thermistor voltage divider circuit
  46. '===============================================================================
  47.  
  48.  
  49. '===== Main Loop ==============================================================
  50. gettemp:
  51.         Gosub getadval
  52.         Gosub convertadvaltotemp
  53.         Gosub displaytemp
  54.         WaitMs 5
  55. Goto gettemp  'return
  56. End                                              
  57. '===============================================================================
  58. 'define resolution = vref  / 1024  'Quatasising constant for ADC reading to Voltage calculation
  59. _resolution:
  60.         resolution = vref / 1024
  61. Return                                            
  62. '===== SUBROUTINE ==============================================================
  63. getadval:
  64. 'Read the value from the Analogue to Digital Converter AN0
  65. Adcin 0, thermistorinput_adval  '= adin 0
  66. Return                                            
  67. '===============================================================================
  68.  
  69. '===== SUBROUTINE ==============================================================
  70. convertadvaltotemp:  '(Donde está el Return de esta subrutina?..)
  71. 'Start Steinhart-Hart Linerization Algorithm
  72. 'FORMULA relating Thermistor Resistance to Temperature:
  73. '[ T = 1/ ( a + b(lnR) + c(lnR)³) ]
  74. 'Where:
  75. 'T = Temp in Kelvin
  76. 'lnR = Natural Log of Resistance
  77. 'a, b & c, are Thermistor constants from data sheet
  78.  
  79.         Gosub _resolution
  80.         mv = resolution * thermistorinput_adval
  81.         temporary = vref - mv
  82.         x0 = resolution * thermistorinput_adval
  83.         x1 = x0 * resistor_value
  84.         resistance = x1 / temporary
  85. 'Return?..
  86.  
  87. 'start Log calculation here
  88. calculate_lnresistance:  '...Command not availible for 16F PICs
  89.         lnresistance = Log resistance  'Deduce the Natural Logarithm a value
  90.         'Continue Steinhart-Hart Algorithm
  91.         steinhart_hart_algorithm:
  92.         x2 = lnresistance * lnresistance
  93.         lnresistancecubed = lnresistance * x2
  94.         x3 = steinhart_b * lnresistance
  95.         x4 = steinhart_c * lnresistancecubed
  96.         x5 = (steinhart_a + x3 + x4)
  97.  
  98.         kelvin = 1 / x5
  99.         celsius = kelvin - 273.15
  100.         fahrenheit = kelvin * 1.001 - 459.67
  101. 'End of steinhart - hart algorithm
  102. Return                                            
  103. '===============================================================================
  104.  
  105. '===== SUBROUTINE ==============================================================
  106. displaytemp:  'Transmit the result serially
  107.         Hserout "Result (In Fahrenheit) = ", #fahrenheit, 13, 10  '13=CR & 10=LF
  108.         WaitMs 1  '00
  109.         Hserout "Result (In Celsius) = ", #celsius, 13, 10  '13=CR & 10=LF
  110.         WaitMs 1  '00
  111.         Hserout "Result (In Kelvin) = ", #kelvin, 13, 10, 13, 10  '13=CR & 10=LF
  112.         WaitMs 1  '00
  113. Return
« Última modificación: 14 de Enero de 2013, 19:51:13 por dogflu66 »
Saludos desde Granada, España.


 

anything