t1000e_sensors.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <Arduino.h>
  2. #include "t1000e_sensors.h"
  3. #define HEATER_NTC_BX 4250 // thermistor coefficient B
  4. #define HEATER_NTC_RP 8250 // ohm, series resistance to thermistor
  5. #define HEATER_NTC_KA 273.15 // 25 Celsius at Kelvin
  6. #define NTC_REF_VCC 3000 // mV, output voltage of LDO
  7. #define LIGHT_REF_VCC 2400 //
  8. static unsigned int ntc_res2[136]={
  9. 113347,107565,102116,96978,92132,87559,83242,79166,75316,71677,
  10. 68237,64991,61919,59011,56258,53650,51178,48835,46613,44506,
  11. 42506,40600,38791,37073,35442,33892,32420,31020,29689,28423,
  12. 27219,26076,24988,23951,22963,22021,21123,20267,19450,18670,
  13. 17926,17214,16534,15886,15266,14674,14108,13566,13049,12554,
  14. 12081,11628,11195,10780,10382,10000,9634,9284,8947,8624,
  15. 8315,8018,7734,7461,7199,6948,6707,6475,6253,6039,
  16. 5834,5636,5445,5262,5086,4917,4754,4597,4446,4301,
  17. 4161,4026,3896,3771,3651,3535,3423,3315,3211,3111,
  18. 3014,2922,2834,2748,2666,2586,2509,2435,2364,2294,
  19. 2228,2163,2100,2040,1981,1925,1870,1817,1766,1716,
  20. 1669,1622,1578,1535,1493,1452,1413,1375,1338,1303,
  21. 1268,1234,1202,1170,1139,1110,1081,1053,1026,999,
  22. 974,949,925,902,880,858,
  23. };
  24. static char ntc_temp2[136]=
  25. {
  26. -30,-29,-28,-27,-26,-25,-24,-23,-22,-21,
  27. -20,-19,-18,-17,-16,-15,-14,-13,-12,-11,
  28. -10,-9,-8,-7,-6,-5,-4,-3,-2,-1,
  29. 0,1,2,3,4,5,6,7,8,9,
  30. 10,11,12,13,14,15,16,17,18,19,
  31. 20,21,22,23,24,25,26,27,28,29,
  32. 30,31,32,33,34,35,36,37,38,39,
  33. 40,41,42,43,44,45,46,47,48,49,
  34. 50,51,52,53,54,55,56,57,58,59,
  35. 60,61,62,63,64,65,66,67,68,69,
  36. 70,71,72,73,74,75,76,77,78,79,
  37. 80,81,82,83,84,85,86,87,88,89,
  38. 90,91,92,93,94,95,96,97,98,99,
  39. 100,101,102,103,104,105,
  40. };
  41. static float get_heater_temperature( unsigned int vcc_volt, unsigned int ntc_volt )
  42. {
  43. int i = 0;
  44. float Vout = 0, Rt = 0, temp = 0;
  45. Vout = ntc_volt;
  46. Rt = ( HEATER_NTC_RP * vcc_volt ) / Vout - HEATER_NTC_RP;
  47. for( i = 0; i < 136; i++ )
  48. {
  49. if( Rt >= ntc_res2[i] )
  50. {
  51. break;
  52. }
  53. }
  54. temp = ntc_temp2[i - 1] + 1 * ( ntc_res2[i - 1] - Rt ) / ( float )( ntc_res2[i - 1] - ntc_res2[i] );
  55. temp = ( temp * 100 + 5 ) / 100;
  56. return temp;
  57. }
  58. static int get_light_lv( unsigned int light_volt )
  59. {
  60. float Vout = 0, Vin = 0, Rt = 0, temp = 0;
  61. unsigned int light_level = 0;
  62. if( light_volt <= 80 )
  63. {
  64. light_level = 0;
  65. return light_level;
  66. }
  67. else if( light_volt >= 2480 )
  68. {
  69. light_level = 100;
  70. return light_level;
  71. }
  72. Vout = light_volt;
  73. light_level = 100 * ( Vout - 80 ) / LIGHT_REF_VCC;
  74. return light_level;
  75. }
  76. float t1000e_get_temperature( void )
  77. {
  78. unsigned int ntc_v, vcc_v;
  79. digitalWrite(PIN_3V3_EN, HIGH);
  80. digitalWrite(SENSOR_EN, HIGH);
  81. analogReference(AR_INTERNAL_3_0);
  82. analogReadResolution(12);
  83. delay(10);
  84. vcc_v = (1000.0*(analogRead(BATTERY_PIN) * ADC_MULTIPLIER * AREF_VOLTAGE)) / 4096;
  85. ntc_v = (1000.0 * AREF_VOLTAGE * analogRead(TEMP_SENSOR)) / 4096;
  86. digitalWrite(PIN_3V3_EN, LOW);
  87. digitalWrite(SENSOR_EN, LOW);
  88. return get_heater_temperature (vcc_v, ntc_v);
  89. }
  90. uint32_t t1000e_get_light( void )
  91. {
  92. int lux = 0;
  93. unsigned int lux_v = 0;
  94. digitalWrite(SENSOR_EN, HIGH);
  95. analogReference(AR_INTERNAL_3_0);
  96. analogReadResolution(12);
  97. delay(10);
  98. lux_v = 1000 * analogRead(LUX_SENSOR) * AREF_VOLTAGE / 4096;
  99. lux = get_light_lv( lux_v );
  100. digitalWrite(SENSOR_EN, LOW);
  101. return lux;
  102. }