ntc.c
Go to the documentation of this file.00001
00046 #include "hw/hw_ntc.h"
00047 #include "hw/ntc_map.h"
00048
00049 #include <cfg/debug.h>
00050
00051 #include <drv/ntc.h>
00052
00053 DB(bool ntc_initialized;)
00054
00063 static size_t upper_bound(const res_t *orig_table, size_t size, res_t val)
00064 {
00065 const res_t *table = orig_table;
00066
00067 while (size)
00068 {
00069 size_t pos = size / 2;
00070 if (val > table[pos])
00071 size = pos;
00072 else
00073 {
00074 table += pos+1;
00075 size -= pos+1;
00076 }
00077 }
00078
00079 return table - orig_table;
00080 }
00081
00082
00100 deg_t ntc_read(NtcDev dev)
00101 {
00102 const NtcHwInfo* hw = ntc_hw_getInfo(dev);
00103 const res_t* r = hw->resistances;
00104
00105 float rx;
00106 size_t i;
00107 deg_t degrees;
00108
00109 rx = ntc_hw_read(dev);
00110
00111
00112 i = upper_bound(r, hw->num_resistances, rx);
00113 ASSERT(i <= hw->num_resistances);
00114
00115 if (i >= hw->num_resistances)
00116 return NTC_SHORT_CIRCUIT;
00117 else if (i == 0)
00118 return NTC_OPEN_CIRCUIT;
00119
00120
00121
00122
00123
00124
00125
00126 float tmp;
00127 tmp = 10 * hw->degrees_step * (rx - r[i]) / (r[i - 1] - r[i]);
00128
00129
00130
00131
00132
00133
00134 degrees = (i * hw->degrees_step + hw->degrees_min) * 10 - (int)(tmp);
00135
00136
00137
00138 return degrees;
00139 }
00140
00141
00145 void ntc_init(void)
00146 {
00147 NTC_HW_INIT;
00148 DB(ntc_initialized = true;)
00149 }
00150