From e788c825ae1e0797c2111231754a70a0b517d696 Mon Sep 17 00:00:00 2001 From: Jan Michel Date: Wed, 5 Aug 2015 17:44:31 +0200 Subject: [PATCH] added functions for converting adc to resistance and resistance to temperature --- pt100/main.c | 2 +- pt100/main.h | 3 ++- pt100/tempmeas.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pt100/main.c b/pt100/main.c index 791f638..e54de4d 100644 --- a/pt100/main.c +++ b/pt100/main.c @@ -33,7 +33,7 @@ void read_calib() { calib_settings.offset_res[i] = eeprom_read(i); } calib_settings.nominal_offset = eeprom_read(8); - calib_settings.gain_current = eeprom_read(10)<<16 | eeprom_read(9); + calib_settings.gain_current = eeprom_read(9); } /****************** diff --git a/pt100/main.h b/pt100/main.h index e2a63a2..dda6c1b 100644 --- a/pt100/main.h +++ b/pt100/main.h @@ -50,9 +50,10 @@ extern volatile uint8_t keys_pressed; extern uint8_t connected_sensors; extern volatile uint16_t time; extern volatile uint8_t measurement_active; +extern struct calib_t calib_settings; struct calib_t { uint16_t offset_res[8]; //precise offset in Milliohm minus nominal value uint16_t nominal_offset; //approximate offset resistor, in Ohm, 100 as default - uint32_t gain_current; //gain times current in nA (about 20E6) + uint16_t gain_current; //2^29 / (gain * current[uA]) (about 53700) }; diff --git a/pt100/tempmeas.c b/pt100/tempmeas.c index c4affef..72decf0 100644 --- a/pt100/tempmeas.c +++ b/pt100/tempmeas.c @@ -4,6 +4,39 @@ uint8_t connected_sensors = 0x1a; uint8_t measurement_step; + +//factors: gain_current has 2^29, ADC has 2^7 steps per mV +//hence shift by 36 necessary, 6 as part of multiplication by 1E6 +int16_t adc_to_res(int16_t adc, uint8_t chan) { + int32_t value; + value = (int32_t)adc * (uint32_t)calib_settings.gain_current; + value >>= 14; + value *= 15625; + value >>= 16; + value += calib_settings.offset_res[chan]; + return (int16_t)value; + } + + +//T = -1 + 41919 * 2^-14 * R + 135 * 2^-27 * R^2 +//units in mOhm, mK + +int32_t res_to_temp(int16_t res) { + int32_t result = -1; + int32_t t = (int32_t)res * 41919; + result += t >> 14; + + t = (int32_t)res * res; + t >>= 7; + t *= 135; + result += t >> 20; + + return result; + } + + + + //measurement_active gets set once per second //do_measurement_step is called once every 2ms by main as long as measurement_active is set. //'measurement_step' gives the number of the current step -- 2.43.0