Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ntc.h
00001 /* 00002 * mbed library for NTC analog pin that translates an A:D input 00003 * voltage to a temperature value. 00004 * 00005 * 00006 * topology: Vref -> SeriesRes -> A:D -> NTC -> GND 00007 * 00008 */ 00009 00010 #ifndef MBED_NTC 00011 #define MBED_NTC 00012 00013 /** 00014 * Private data structure for NTC data values. 00015 * 00016 **/ 00017 typedef struct { 00018 float vref ; /*!< Referense voltage*/ 00019 float ad_resolution ; /*!< A:D resolution*/ 00020 int ntc_res ; /*!< NTC resistance value at 25C*/ 00021 int ntc_tol ; /*!< NTC initial tolerance %*/ 00022 int ntc_beta_0050 ; /*!< NTC Beta, B0/50*/ 00023 int ntc_beta_2550 ; /*!< NTC Beta, B25/50*/ 00024 int ntc_beta_2580 ; /*!< NTC Beta, B25/80*/ 00025 int ntc_beta_2585 ; /*!< NTC Beta, B25/85*/ 00026 int ntc_beta_25100 ; /*!< NTC Beta, B25/100*/ 00027 int ntc_beta_other ; /*!< NTC Beta, other value vs temp*/ 00028 int ntc_beta_tol ; /*!< NTC Beta tolerance %*/ 00029 int sres_res ; /*!< Series resistor value*/ 00030 int sres_tol ; /*!< Series resistor tolerance %*/ 00031 int sres_ppm ; /*!< Series resistor tempco in ppm*/ 00032 } NTC_TypeDef; 00033 00034 /** 00035 * Default values for data structure above 00036 * 00037 **/ 00038 const NTC_TypeDef ntc_std_paramtr = { 00039 // Vref 00040 3.0f, // Vref 00041 16384.0f, // A:D resolution 00042 // muRata NCP15XH103J03RC 00043 20000, // NTC resistance 00044 3, // NTC initial tolerance 00045 0, // NTC B0/50, none 00046 3300, // NTC B25/50 00047 3322, // NTC B25/80 00048 3333, // NTC B25/85 00049 3344, // NTC B25/100 00050 0, // NTC other Beta value 00051 2, // NTC beta tolerance 00052 // 3.32k 1% 100ppm 00053 4750, // Series resistor value 00054 1, // Series resistor tolerance 00055 200 // Series Resistor tempco ppm 00056 }; 00057 00058 /** NTC temperature A:D conversion 00059 * 00060 * @code 00061 * #include "mbed.h" 00062 * #include "ntc.h" 00063 * 00064 * #define NTC_VREF 3.3f 00065 * #define NTC_AD_RESOL 65536.0f 00066 * 00067 * // Connections: 00068 * 00069 * // 3.3V (or other Vref) 00070 * // --+-- 00071 * // | 00072 * // -+- 00073 * // | | 00074 * // | | 00075 * // | | Series 00076 * // | | Resistor 00077 * // | | 00078 * // -+- 00079 * // | 00080 * // +---> To A:D 00081 * // | 00082 * // -+- 00083 * // | | 00084 * // | | 00085 * // | | NTC 00086 * // | | 00087 * // | | 00088 * // -+- 00089 * // | 00090 * // --+-- 00091 * // --- Ground 00092 * // - 00093 * 00094 * 00095 * const NTC_TypeDef ntc_my_paramtr = { 00096 * // Vref 00097 * NTC_VREF, // Vref 00098 * NTC_AD_RESOL, // A:D 16-bit resolution 00099 * // muRata NCP15XH103J03RC 00100 * 10000, // NTC resistance 00101 * 5, // NTC initial tolerance 00102 * 0, // NTC B0/50 (none) 00103 * 3380, // NTC B25/50 00104 * 3428, // NTC B25/80 00105 * 3434, // NTC B25/85 00106 * 3355, // NTC B25/100 00107 * 0, // NTC other beta (none) 00108 * 1, // NTC beta tolerance 00109 * // 3.32k 1% 100ppm 00110 * 3320, // Series resistor value 00111 * 1, // Series resistor tolerance 00112 * 100 // Series Resistor tempco ppm 00113 * }; 00114 * 00115 * NTC ntc(A1, &ntc_my_paramtr); //initialize NTC temperature A:D 00116 * 00117 * main() { 00118 * printf("\r\n\r\n-------------------------------------------\r\n"); 00119 * printf("NTC Res: %5d B0/50: %4d B25/50: %4d B25/80: %4d B25/85: %4d B25/100: %4d B_OTHER: %4d SeriesR: %d\r\n", 00120 * ntc.get_ntc_res(), ntc.get_ntc_beta(NTC::B0_50), ntc.get_ntc_beta(NTC::B25_50), ntc.get_ntc_beta(NTC::B25_80), ntc.get_ntc_beta(NTC::B25_85), 00121 * ntc.get_ntc_beta(NTC::B25_100), ntc.get_ntc_beta(NTC::B_OTHER), ntc.get_series_res()); 00122 * uint16_t ad = ntc.read_ad_reg(); 00123 * printf("NTC A:D Val: %5d Volt A:D: %.6f NTC-R_now: %7.1f Temp: %+.2f\r\n", ad, 00124 * NTC_VREF / NTC_AD_RESOL * (float)ad, ntc.get_ntc_res_viaAD(ad), ntc.get_ntc_temp(NTC::B25_85, ad)); 00125 * 00126 * while(1) { 00127 * printf("Temp: %+.2f\r\n", ntc.get_ntc_temp(NTC::B25_85)); 00128 * wait(2.0); 00129 * } 00130 * 00131 * } 00132 * @endcode 00133 */ 00134 00135 class NTC 00136 { 00137 public: 00138 /** 00139 * NTC Beta curves to choose from for Temperature calculations 00140 * 00141 **/ 00142 enum ntcBetaCurve { 00143 B0_50 , /*!< use B0/50 curve */ 00144 B25_50 , /*!< use B25/50 curve */ 00145 B25_80 , /*!< use B25/80 curve */ 00146 B25_85 , /*!< use B25/85 curve */ 00147 B25_100 , /*!< use B25/100 curve */ 00148 B_OTHER , /*!< use other curve */ 00149 }; 00150 00151 /** Configure gpio pin 00152 * @param A:D I/O pin 00153 * @param parameters for NTC (NTC_TypeDef) 00154 */ 00155 NTC(PinName p_ana, const NTC_TypeDef *ntc_parameter); 00156 00157 /** Configure gpio pin 00158 * @param A:D I/O pin 00159 * uses default parameters 00160 */ 00161 NTC(PinName p_ana); 00162 00163 /** Read A:D register value 00164 * @param none 00165 * @return A:D value from 0-65535 00166 */ 00167 uint16_t read_ad_reg(); 00168 00169 /** Get the NTC resistance value 00170 * @param none 00171 * @return resistance of NTC at 25C 00172 */ 00173 int get_ntc_res(); 00174 00175 /** Get the NTC beta value 00176 * @param ntcBetaCurve selection 00177 * @param B25_85 default value 00178 * @return beta curve value 00179 */ 00180 int get_ntc_beta(int beta = B25_85 ); 00181 00182 /** Get the resistance value for the series resistor 00183 * @param none 00184 * @return resistance of series resistor 00185 */ 00186 int get_series_res(); 00187 00188 /** calculate NTC resistance based on A:D reading 00189 * @param none 00190 * @param din optional read from read_ad_reg(). 00191 * @param din -> if missing or 0, read_ad_reg() value is used for calculation 00192 * @param din -> if != 0, then value of din is used for calculation 00193 * @return resistance of NTC 00194 */ 00195 float get_ntc_res_viaAD(uint16_t din = 0); 00196 00197 /** calculate NTC temperaure 00198 * @param curve Beta curve to use. See ntcBetaCurve {}; 00199 * @param din optional read from read_ad_reg() 00200 * @param din -> if missing or 0, read_ad_reg() value is used for calculation 00201 * @param din -> if != 0, then value of din is used for calculation 00202 * @return temperature of NTC 00203 * @return -100 if NTC Beta selected = 0 00204 */ 00205 float get_ntc_temp(int curve, uint16_t din = 0); 00206 00207 protected: 00208 AnalogIn _ana; 00209 00210 private: 00211 NTC_TypeDef ntc_set_data; 00212 uint16_t rawdata; 00213 00214 }; 00215 00216 #endif // MBED_NTC
Generated on Tue Jul 19 2022 19:28:44 by
 1.7.2