System Management code
Dependencies: mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP
Fork of SystemManagement by
Revision 17:c9ce210f6654, committed 2014-10-25
- Comitter:
- pspatel321
- Date:
- Sat Oct 25 03:28:55 2014 +0000
- Parent:
- 16:a72ffe7d879d
- Child:
- 18:915a235bc099
- Commit message:
- Parth's edits for 10/24/14, items remaining: DC-DC, XBee, reading AMS/IMD error, main code
Changed in this revision
--- a/CoulombCounter/CoulombCounter.cpp Fri Oct 24 22:38:20 2014 +0000 +++ b/CoulombCounter/CoulombCounter.cpp Sat Oct 25 03:28:55 2014 +0000 @@ -2,10 +2,10 @@ const float MSEC_HRS = 2.77778e-7; // Multiplier to convert milliseconds to hours const float BAT_ISENSE_MULTIPLIER = 6.2299; // Multiplier to convert float to amps -const float BAT_ISENSE_OFFSET = 0.5*BAT_ISENSE_MULTIPLIER; // Offset to convert float to amps +const float BAT_ISENSE_OFFSET = -0.5*BAT_ISENSE_MULTIPLIER; // Offset to convert float to amps const float BAT_ISENSE_LIMS = 3.0; // Over-current limit = +/- 3A -CoulombCounter::CoulombCounter(PinName _IsensePin, int mSec, int _rtcGPREG_counter, int _rtcGPREG_capacity) : BatISense(p19) { +CoulombCounter::CoulombCounter(int _mSec, int _rtcGPREG_counter, int _rtcGPREG_capacity) : BatISense(p19) { mSec = _mSec; rtcGPREG_counter = _rtcGPREG_counter; rtcGPREG_capacity = _rtcGPREG_capacity; @@ -19,15 +19,16 @@ sampler.attach_us(this, &CoulombCounter::sample, mSec*1000); } -void CouloumbCounter::sample() { +void CoulombCounter::sample() { // Take the reading currentSample = BatISense.read()*BAT_ISENSE_MULTIPLIER+BAT_ISENSE_OFFSET; // Signal error on over current - if (currentSample < -BAT_ISENSE_LIMS || currentSample > BAT_ISENSE_LIMS) { + if (abs(currentSample) > BAT_ISENSE_LIMS) { overCurrent = true; } else overCurrent = false; // Integrate - store.write(ampHours()+currentSample*mSec*MSEC_HRS, rtcGPREG_counter); + float f = ampHours()+currentSample*mSec*MSEC_HRS; + store.write(f, rtcGPREG_counter); } \ No newline at end of file
--- a/CoulombCounter/CoulombCounter.h Fri Oct 24 22:38:20 2014 +0000 +++ b/CoulombCounter/CoulombCounter.h Sat Oct 25 03:28:55 2014 +0000 @@ -3,7 +3,7 @@ #include "mbed.h" #include "CANBuffer.h" -#include "Store_RTC.h" +#include "RTCStore.h" RTCStore store; @@ -31,11 +31,11 @@ private: Ticker sampler; // Used to capture next sample and coulomb count void sample(); - int mSec; - float currentSample; - int rtcGPREG_counter; - int rtcGPREG_capacity; + volatile int mSec; + volatile float currentSample; + volatile int rtcGPREG_counter; + volatile int rtcGPREG_capacity; AnalogIn BatISense; // Analog input pin }; #endif \ No newline at end of file
--- a/CoulombCounter/RTCStore.h Fri Oct 24 22:38:20 2014 +0000 +++ b/CoulombCounter/RTCStore.h Sat Oct 25 03:28:55 2014 +0000 @@ -16,7 +16,7 @@ //battery.write(10.92,4); printf("LPC_RTC->GPREG4:%f\n\r",battery.read(4)); } -*/ +*/ #ifndef _RTC_STORE_H #define _RTC_STORE_H @@ -28,10 +28,10 @@ LPC_SC->PCONP |= (1<<9); // Enable RTC Peripheral } void write(float data, int block) { - *((float*)(&LPC_RTC->GPREG0)+sizeof(float)*block) = data; + *((float*)(&LPC_RTC->GPREG0)+block) = data; } float read(int block) { - return *((float*)((&LPC_RTC->GPREG0)+sizeof(float)*block)); + return *((float*)(&LPC_RTC->GPREG0)+block); } }; #endif
--- a/DC_DC/DC_DC.cpp Fri Oct 24 22:38:20 2014 +0000 +++ b/DC_DC/DC_DC.cpp Sat Oct 25 03:28:55 2014 +0000 @@ -18,13 +18,13 @@ return status; } -void DC::set(bool s){ +void DC::set(bool s){/* status = s; if(!status){ fanPump->shutdown_all(); } - dc_pin = !status; + dc_pin = !status;*/ } void update_dcdc(const void *arg){
--- a/FanPump/FanPump.cpp Fri Oct 24 22:38:20 2014 +0000 +++ b/FanPump/FanPump.cpp Sat Oct 25 03:28:55 2014 +0000 @@ -1,56 +1,81 @@ #include "FanPump.h" -static FanPump* instance[6] = { NULL }; +static FanPump* instance[6] = { NULL }; // Access pwm object by channel# +const int PCLK = 24e6; // 24Mhz clock // Interrupt handler, must be called from static context, calls all the slew functions void pwmIRQ() { + int sum = 0; + int items = 0; if (LPC_PWM1->IR & 1) { for (int i = 0; i < 6; i++) { - if (instance[i] != NULL) instance[i]->slew(); + if (instance[i] != NULL) { + items++; + sum += instance[i]->slew(); + } } } - LPC_PWM1->IR = 0x73F; // Clear interrupts + LPC_PWM1->IR = 0x73F; // Clear interrupts + if (items == sum) LPC_PWM1->MCR = 0; // Detach all the interrupts, every pin is already where it needs to be } // Called on each timer expire for each pwm object -void FanPump::slew() { - uint32_t currPulseT = *(&LPC_PWM1->MR0+pin); - uint32_t currPulse_us = currPulseT / 24; - uint32_t setPointT = setPoint_us * 24; // Convert us into ticks - if (currPulseT == setPointT) return; // Nothing to slew here, already at its setpoint - +int FanPump::slew() { + uint32_t currPulseT = *((uint32_t*)(&LPC_PWM1->MR0)+chan); // Get the current pulsewidth ticks + uint32_t setPointT = setPoint_us * (PCLK/1e6); // Convert us into ticks + if (currPulseT == setPointT) return 1; // Nothing to slew here, already at its setpoint + + uint32_t currPulse_us = currPulseT / (PCLK/1e6); // Convert to us if (currPulseT < setPointT) { - if (setPoint_us - currPulse_us < maxChange_us) pwm.pulsewidth_us(setPoint_us); // Close to the setpoint, write it directly + if (setPoint_us - currPulse_us <= maxChange_us) pwm.pulsewidth_us(setPoint_us); // Close to the setpoint, write it directly else pwm.pulsewidth_us(currPulse_us + maxChange_us); } else { - if (currPulse_us - setPoint_us < maxChange_us) pwm.pulsewidth_us(setPoint_us); + if (currPulse_us - setPoint_us <= maxChange_us) pwm.pulsewidth_us(setPoint_us); // Close to the setpoint, write it directly else pwm.pulsewidth_us(currPulse_us - maxChange_us); } + return 0; } FanPump::FanPump(PinName pin, float period, float slew) : pwm(pin) { + + // Match the pin# to the PWM object for the interrupt + int chan=0; + if (pin == p26 || pin == LED1) chan = 1; + if (pin == p25 || pin == LED2) chan = 2; + if (pin == p24 || pin == LED3) chan = 3; + if (pin == p23 || pin == LED4) chan = 4; + if (pin == p22) chan = 5; + if (pin == p21) chan = 6; + if (chan == 0) return; // Invalid pin + instance[chan-1] = this; + setPoint_us = 0; period_us = period / 1.0e6; pwm.period_us(period_us); maxChange_us = (period / slew) * period_us; - - // Match the pin# to the PWM object for the interrupt - if (pin == p26 || pin == LED1) pin = 1; - if (pin == p25 || pin == LED2) pin = 2; - if (pin == p24 || pin == LED3) pin = 3; - if (pin == p23 || pin == LED4) pin = 4; - if (pin == p22) pin = 5; - if (pin == p21) pin = 6; - instance[pin-1] = this; - - LPC_PWM1->IR = 0x73F; // Clear interrupts + + LPC_PWM1->IR = 0x73F; // Clear interrupts NVIC_SetVector(PWM1_IRQn, (uint32_t)&pwmIRQ); NVIC_SetPriority(PWM1_IRQn, 0); NVIC_EnableIRQ(PWM1_IRQn); - LPC_PWM1->MR |= 1; // Enable interrupt on MR0 (when the pwm period expires) + LPC_PWM1->MCR = 1; // Enable interrupt on MR0 (when the pwm period expires) } void FanPump::write(float duty) { if (duty < 0) duty = 0; if (duty > 1) duty = 1; setPoint_us = duty * period_us; + LPC_PWM1->MCR = 1; // Enable interrupt on MR0 (when the pwm period expires) +} +void FanPump::directOff() { + __disable_irq(); + pwm.pulsewidth_us(0); + setPoint_us = 0; + __enable_irq(); +} +float FanPump::read() { + return (float)(setPoint_us)/(float)(period_us); +} +float FanPump::readRaw() { + uint32_t currPulseT = *((uint32_t*)(&LPC_PWM1->MR0)+chan); // Get the current pulsewidth ticks + return ((float)(currPulseT) / (float)(PCLK/1e6)) / (float)(period_us); } \ No newline at end of file
--- a/FanPump/FanPump.h Fri Oct 24 22:38:20 2014 +0000 +++ b/FanPump/FanPump.h Sat Oct 25 03:28:55 2014 +0000 @@ -11,13 +11,13 @@ void write(float duty); float read(); // Read the last setpoint float readRaw(); // Read the raw current duty (may be mid-transition) - - void slew(); // Slew rate callback function + void directOff(); // Turn off the channel immediately (no slew) + int slew(); // Slew rate callback function private: PwmOut pwm; // mbed PWM out - int pin; // pwm channel# - static uint32_t period_us; // Period in microseconds (shared by all channels) - uint32_t setPoint_us; - uint32_t maxChange_us; // Max pulsewidth change allowed to achieve the slew rate + volatile int chan; // pwm channel# + static uint32_t period_us; // Period in microseconds (shared by all channels) + volatile uint32_t setPoint_us; + volatile uint32_t maxChange_us; // Max pulsewidth change allowed to achieve the slew rate }; #endif \ No newline at end of file
--- a/IMD/IMD.cpp Fri Oct 24 22:38:20 2014 +0000 +++ b/IMD/IMD.cpp Sat Oct 25 03:28:55 2014 +0000 @@ -6,10 +6,9 @@ const uint32_t PCLK = 24000000; // Timer counting clock = 24Mhz const uint32_t TIMEOUT_TICKS = PCLK*ZERO_HZ_TIMEOUT; // Zeroing timeout in clock ticks = seconds * PCLK const float EXTRA = 0.01; // Margin on the IMD PWM limits -const float ULIM = 50000000; // Max value produced by IMD // Interrupt function, must be static context -void TIM0_IRQ() { +void tim0_IRQ() { // Capture event was found if (LPC_TIM0->IR & 1<<4) instance->edgeIRQ(); @@ -25,11 +24,11 @@ widthTicks = 0; // Zero low, so that duty = 0/1 = 0% periodTicks = 1; - LPC_SC->PCONP |= (1<<2); // Power on timer1 - LPC_SC->PCLKSEL0 &= ~(3<<4); // Clock at 24MHz + LPC_SC->PCONP |= (1<<1); // Power on timer0 + LPC_SC->PCLKSEL0 &= ~(3<<2); // Clock at 24MHz - *(p1_26.pinsel_addr) |= 3 << p1_26.selmode_num; // Set pin as capture pin - *(p1_26.pinmode_addr) |= (3 << p1_26.selmode_num); // Pull down + *(p1_26.pinsel_addr) |= 3 << p1_26.selmode_num; // Set pin as capture pin + *(p1_26.pinmode_addr) |= 3 << p1_26.selmode_num; // Pull down LPC_TIM0->TCR=2; // Stop counter and hold at 0, for configuration, set to 1 to start operation LPC_TIM0->IR=0x3F; // Clear any interrupt flags @@ -37,9 +36,9 @@ LPC_TIM0->PR=0; // No prescale value, clock at full pclk=24Mhz LPC_TIM0->EMR=0; // Do not use external match pins - NVIC_SetVector(TIMER1_IRQn, (uint32_t)&TIM0_IRQ); // Point to the edge interrupt handler - NVIC_SetPrioriry(TIMER1_IRQn, 0); // Highest Priority - NVIC_EnableIRQ(TIMER1_IRQn); // Enable IRQ + NVIC_SetVector(TIMER0_IRQn, (uint32_t)&tim0_IRQ); // Point to the interrupt handler + NVIC_SetPriority(TIMER0_IRQn, 0); // Highest Priority + NVIC_EnableIRQ(TIMER0_IRQn); // Enable IRQ LPC_TIM0->CCR = RISING; // Generate interrupt on capture, capture on rising edge to start LPC_TIM0->MCR = 1; // Interrupt on Match0 to establish the zero speed timeout LPC_TIM0->MR0 = LPC_TIM0->TC+TIMEOUT_TICKS; @@ -47,7 +46,7 @@ } void IMD::edgeIRQ() { - enum EdgeT type = LPC_TIM0->CCR; + uint32_t type = LPC_TIM0->CCR; uint32_t capTime = LPC_TIM0->CR0; LPC_TIM0->MR0 = capTime+TIMEOUT_TICKS; // Set the next zeroing timeout @@ -66,12 +65,12 @@ } // Switch interrupt types to capture the next edge - if (type == RISING) LPC_TIM0->CCR = FALIING; + if (type == RISING) LPC_TIM0->CCR = FALLING; if (type == FALLING) LPC_TIM0->CCR = RISING; } void IMD::zeroIRQ() { - enum EdgeT type = LPC_TIM0->CCR; + uint32_t type = LPC_TIM0->CCR; periodTicks = 1; first = true; @@ -94,23 +93,23 @@ char IMD::status() { float freq = frequency(); - if (freq == 0) return 0; // IMD off - else if (05 < freq && freq <= 15) return 1; // 10Hz normal mode - else if (15 < freq && freq <= 25) return 2; // 20Hz undervoltage mode - else if (25 < freq && freq <= 35) return 3; // 30Hz speed start mode - else if (45 < freq && freq <= 55) return 4; // 40Hz IMD error - else if (55 < freq && freq <= 65) return 5; // 50Hz Ground error - else return -1; // Invalid + if (freq == 0) return OFF; // IMD off + else if (05 < freq && freq <= 15) return NORMAL; // 10Hz normal mode + else if (15 < freq && freq <= 25) return UNDERVOLT; // 20Hz undervoltage mode + else if (25 < freq && freq <= 35) return SPEEDSTART; // 30Hz speed start mode + else if (45 < freq && freq <= 55) return ERROR; // 40Hz IMD error + else if (55 < freq && freq <= 65) return GROUNDERR; // 50Hz Ground error + else return INVALID; // Invalid } float IMD::resistance() { - char status = status(); - float duty = duty(); + char stat = status(); + float dut = duty(); // In normal or undervoltage mode, where Rf is available - if (status == 1 || status == 2) { - if (0.05-EXTRA <= duty && duty >= 0.95+EXTRA) { - float rf = (0.9*1200e3/(duty-0.05)) - 1200e3; + if (stat == 1 || stat == 2) { + if (0.05-EXTRA <= dut && dut >= 0.95+EXTRA) { + float rf = (0.9*1200e3/(dut-0.05)) - 1200e3; if (rf < 0) rf = 0; if (rf > 50e6) rf = 50e6; return rf; @@ -118,10 +117,10 @@ else return -1; } // In speed start, where only good/bad estimate is available - if (status == 3) { - if (0.05-EXTRA <= duty && duty >= 0.10+EXTRA) return 50e6; // Good - else if (0.90-EXTRA <= duty && duty >= 0.95+EXTRA) return 0; // Bad + if (stat == 3) { + if (0.05-EXTRA <= dut && dut >= 0.10+EXTRA) return 50e6; // Good + else if (0.90-EXTRA <= dut && dut >= 0.95+EXTRA) return 0; // Bad else return -1; } - return NaN; + return NAN; // Measurement not available in this mode } \ No newline at end of file
--- a/IMD/IMD.h Fri Oct 24 22:38:20 2014 +0000 +++ b/IMD/IMD.h Sat Oct 25 03:28:55 2014 +0000 @@ -13,22 +13,22 @@ FALLING=6, BOTH=7, }; + +enum IMDstatus { + OFF=0, + NORMAL=1, + UNDERVOLT=2, + SPEEDSTART=3, + ERROR=4, + GROUNDERR=5, + INVALID=6, +}; + class IMD{ public: IMD(); - // Get the IMD status - /* - Status State - 0 IMD off - 1 Normal - 2 under voltage - 3 Speed Start - 4 IMD error - 5 Ground error - */ char status(); - // Gets the insulation resistance reading // Returns 0 to 50,000,000 in normal/UV modes // Returns 0 or 50,000,000 in speed start (good/bad only) @@ -44,10 +44,10 @@ private: float frequency(); float duty(); - uint32_t startTime; - uint32_t widthTicks; - uint32_t periodTicks; - bool first; + volatile uint32_t startTime; + volatile uint32_t widthTicks; + volatile uint32_t periodTicks; + volatile bool first; }; #endif \ No newline at end of file
--- a/PollSwitch/PollSwitch.cpp Fri Oct 24 22:38:20 2014 +0000 +++ b/PollSwitch/PollSwitch.cpp Sat Oct 25 03:28:55 2014 +0000 @@ -1,64 +1,33 @@ #include "PollSwitch.h" -LPC_pin PollPin[12]={p1_0, p1_1, p1_4, p1_8, p1_9, p1_10, p1_14, p1_15, p1_16, p1_17, p1_27, p1_28}; -LPCDigitalOut poll[12]={ LPCDigitalIn(PollPin[0], PullDown), - LPCDigitalIn(PollPin[1], PullDown), - LPCDigitalOut(PollPin[2]), - LPCDigitalOut(PollPin[3]), - LPCDigitalOut(PollPin[4]), - LPCDigitalOut(PollPin[5]), - LPCDigitalOut(PollPin[6]), - LPCDigitalOut(PollPin[7]), - LPCDigitalOut(PollPin[8]), - LPCDigitalOut(PollPin[9]), - LPCDigitalOut(PollPin[10]), - LPCDigitalOut(PollPin[11])}; - -CANBuffer *tx_Poll_Buffer; +LPCDigitalIn sw[]={ LPCDigitalIn(p1_0, PullDown), + LPCDigitalIn(p1_1, PullDown), + LPCDigitalIn(p1_4, PullDown), + LPCDigitalIn(p1_8, PullDown), + LPCDigitalIn(p1_9, PullDown), + LPCDigitalIn(p1_10, PullDown), + LPCDigitalIn(p1_14, PullDown), + LPCDigitalIn(p1_15, PullDown), + LPCDigitalIn(p1_16, PullDown), + LPCDigitalIn(p1_17, PullDown), + LPCDigitalIn(p1_27, PullDown), + LPCDigitalIn(p1_28, PullDown) + }; -union int_to_char { - char ch[2]; - uint16_t i; -} converter; - -PollSwitch::PollSwitch(CANBuffer *can){ - tx_Poll_Buffer = can; - - /* - for(int i = 0; i < 12; i++){ - poll[i].mode(PullDown); - } - */ +PollSwitch::PollSwitch(){ + switchState = 0; } -uint16_t poll_switches(){ - uint16_t a = 0; +uint16_t PollSwitch::poll(){ int i = 0; // if a low signal is detected, previous switch is broken - for(i = 1; i < 12; i++){ - if(!poll[i].read()) + for(i = 1; i < sizeof(sw)/sizeof(sw[0]); i++){ + if(!sw[i].read()) break; } // bit on: switch may be broken - a = 0 & (0xFF >> (i-1)); - return a; -} - -void update_poll(const void *arg){ - char data[2] = {0}; - while(1){ - converter.i = poll_switches(); - data[0] = converter.ch[0]; - data[1] = converter.ch[1]; - CANMessage txMessage(TX_POLL_ID, data, 2); - tx_Poll_Buffer->txWrite(txMessage); - - Thread::wait(100); //10 Hz update - } -} - -void PollSwitch::start_update(){ - Thread update_thread(update_poll); + switchState = (1 << i); + return (1 << i); } \ No newline at end of file
--- a/PollSwitch/PollSwitch.h Fri Oct 24 22:38:20 2014 +0000 +++ b/PollSwitch/PollSwitch.h Sat Oct 25 03:28:55 2014 +0000 @@ -1,16 +1,16 @@ #ifndef _FILE_POLLSWITCH_H #define _FILE_POLLSWITCH_H -#include "LPCDigitalOut.h" #include "LPCDigitalIn.h" -#include "CANBuffer.h" -#include "rtos.h" - -const int TX_POLL_ID = ((4 << 8) | 0x02); class PollSwitch{ public: - PollSwitch(CANBuffer *can); - void start_update(); + PollSwitch(); + + // Returns new state, updates last known + uint16_t poll(); + + // Stores the last known state + uint16_t switchState; }; #endif \ No newline at end of file
--- a/SysMngmt.cpp Fri Oct 24 22:38:20 2014 +0000 +++ b/SysMngmt.cpp Sat Oct 25 03:28:55 2014 +0000 @@ -15,7 +15,7 @@ #include "DC_DC.h" #include "PollSwitch.h" #include "IMD.h" -#include "CurrentMonitor.h" +#include "CoulombCounter.h" CANBuffer rxBuffer(CAN1, MEDIUM); Watchdog wdt;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TemperatureRead/TemperatureRead.cpp Sat Oct 25 03:28:55 2014 +0000 @@ -0,0 +1,30 @@ +#include "TemperatureRead.h" + +TemperatureRead::TemperatureRead(PinName pin, struct TempTable _table) : ana(pin) { + usingAna = true; + table = _table; + temperature = 0; +} +TemperatureRead::TemperatureRead(struct TempTable _table) { + usingAna = false; + table = _table; + temperature = 0; +} + +float TemperatureRead::convert(float in) { + if (in < table.input[0]) { temperature = -INFINITY; return temperature; } // Out of range of the table + if (in > table.input[table.numEntries-1]) { temperaure = -INFINITY; return temperature; } // Out of range of the table + int lowerIndex = 0; + int upperIndex = table.numEntries-1; + for (int i = 0; i < table.numEntries; i++) { // Converge on the entries that surround the input + if (in >= table.input[lowerIndex]) { lowerIndex = i; } + if (in <= table.input[upperIndex]) { upperIndex = table.numEntries-1 - i; } + } + // Interpolate and return + temperature = table.output[lowerIndex] + (table.output[upperIndex] - table.output[lowerIndex]) * ((in - table.input[lowerIndex]) / (table.input[upperIndex] - table.input[lowerIndex])); + return temperature; +} +float TemperatureRead::read() { + if (!usingAna) { temperature = 0; return temperature; + else return convert(ana.read()); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TemperatureRead/TemperatureRead.h Sat Oct 25 03:28:55 2014 +0000 @@ -0,0 +1,24 @@ +#ifndef _TEMPERATURE_READ_H +#define _TEMPERATURE_READ_H + +// Look up table for temperatures from the GLV Battery Charge Regulator MOSFET internal to the rear accy. box +const struct TempTable NXFT15XH103_Table { + float input[] = {0.951781202,0.937262775,0.919578592,0.898354357,0.873281379,0.844154225,0.810924767,0.77373518,0.732941648,0.689103062,0.642984648,0.595469256,0.547490837,0.5,0.453820525,0.409646378,0.368048534,0.329354168,0.293785311,0.261393013,0.232186732,0.205908044,0.182539034,0.161706765,0.143322197,0.127018769,0.112688554,0.100071994,0.088921283,0.079189687,0.07054559,0.06305631,0.056425741,0.050512723}; + float ouput[] = {-40,-35,-30,-25,-20,-15,-10,-5,0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125}; + int numEntries = sizeof(input)/sizeof(input[0]); +}; + +class TemperatureRead { +public: + TemperatureRead(PinName pin, const struct TempTable _table); + TemperatureRead(struct TempTable _table) + float convert(float in); + float temperature; + +private: + AnalogIn ana; + bool usingAna; + struct TempTable table; +}; + +#endif /*_TEMPERATURE_READ_*/ \ No newline at end of file
--- a/Temperature_Read/TemperatureRead.h Fri Oct 24 22:38:20 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,271 +0,0 @@ -/* -Test Code -#include "mbed.h" -#include "TemperatureRead.h" - -AnalogIn Vin(p15); -int main() -{ - float resistance,temperature,Vadc=0; - while(1) - { - Vadc=Vin.read()*VDD; - //printf("Vadc:%f\n\r",Vadc); - while(Vadc>0) - { - Vadc=Vin.read()*VDD; - printf("Vadc:%f\n\r",Vadc); - resistance=((float)R10K*Vadc)/((float)VDD + Vadc); - temperature=ReadTemp(TR_USP10982_Map, resistance, TABLE_SIZE_USP10982); - printf("Resistance:%f \n\r",resistance); - printf("Temperature:%f \n\r",temperature); - wait(0.5); - } - } -} -*/ -#ifndef _TEMPERATURE_READ_ -#define _TEMPERATURE_READ_ - -#define TABLE_SIZE_NXFT15XH103FA 34 -#define TABLE_SIZE_NTCLP00E3103H 26 -#define TABLE_SIZE_USP10982 121 -#define R10K 10 //Consider Resistance in Kohms -#define VDD 3.33 -typedef struct -{ - float x; - float y; -}Temp_Resistance; - -Temp_Resistance TR_NXFT15XH103FA_Map[TABLE_SIZE_NXFT15XH103FA] ={ - { .x= 197.388 , .y= -40 }, - { .x= 149.395 , .y= -35 }, - { .x= 114.345 , .y= -30 }, - { .x= 88.381 , .y= -25 }, - { .x= 68.915 , .y= -20 }, - { .x= 54.166 , .y= -15 }, - { .x= 42.889 , .y= -10 }, - { .x= 34.196 , .y= -5 }, - { .x= 27.445 , .y= 0 }, - { .x= 22.165 , .y= 5 }, - { .x= 18.01 , .y= 10 }, - { .x= 14.72 , .y= 15 }, - { .x= 12.099 , .y= 20 }, - { .x= 10 , .y= 25 }, - { .x= 8.309 , .y= 30 }, - { .x= 6.939 , .y= 35 }, - { .x= 5.824 , .y= 40 }, - { .x= 4.911 , .y= 45 }, - { .x= 4.16 , .y= 50 }, - { .x= 3.539 , .y= 55 }, - { .x= 3.024 , .y= 60 }, - { .x= 2.593 , .y= 65 }, - { .x= 2.233 , .y= 70 }, - { .x= 1.929 , .y= 75 }, - { .x= 1.673 , .y= 80 }, - { .x= 1.455 , .y= 85 }, - { .x= 1.27 , .y= 90 }, - { .x= 1.112 , .y= 95 }, - { .x= 0.976 , .y= 100 }, - { .x= 0.86 , .y= 105 }, - { .x= 0.759 , .y= 110 }, - { .x= 0.673 , .y= 115 }, - { .x= 0.598 , .y= 120 }, - { .x= 0.532 , .y= 125 }, - }; - -Temp_Resistance TR_NTCLP00E3103H_Map[TABLE_SIZE_NTCLP00E3103H] ={ - { .x= 332.094 , .y= -40 }, - { .x= 239.9 , .y= -35 }, - { .x= 175.2 , .y= -30 }, - { .x= 129.287 , .y= -25 }, - { .x= 96.358 , .y= -20 }, - { .x= 72.5 , .y= -15 }, - { .x= 55.046 , .y= -10 }, - { .x= 42.157 , .y= -5 }, - { .x= 32.554 , .y= 0 }, - { .x= 25.339 , .y= 5 }, - { .x= 19.872 , .y= 10 }, - { .x= 15.698 , .y= 15 }, - { .x= 12.488 , .y= 20 }, - { .x= 10 , .y= 25 }, - { .x= 8.059 , .y= 30 }, - { .x= 6.535 , .y= 35 }, - { .x= 5.33 , .y= 40 }, - { .x= 4.372 , .y= 45 }, - { .x= 3.605 , .y= 50 }, - { .x= 2.989 , .y= 55 }, - { .x= 2.49 , .y= 60 }, - { .x= 2.084 , .y= 65 }, - { .x= 1.753 , .y= 70 }, - { .x= 1.481 , .y= 75 }, - { .x= 1.256 , .y= 80 }, - { .x= 1.07 , .y= 85 }, - - }; - -Temp_Resistance TR_USP10982_Map[TABLE_SIZE_USP10982] ={ - { .x= 336.479 , .y= -40 }, - { .x= 314.904 , .y= -39 }, - { .x= 294.848 , .y= -38 }, - { .x= 276.194 , .y= -37 }, - { .x= 258.838 , .y= -36 }, - { .x= 242.681 , .y= -35 }, - { .x= 227.632 , .y= -34 }, - { .x= 213.61 , .y= -33 }, - { .x= 200.539 , .y= -32 }, - { .x= 188.349 , .y= -31 }, - { .x= 176.974 , .y= -30 }, - { .x= 166.356 , .y= -29 }, - { .x= 156.441 , .y= -28 }, - { .x= 147.177 , .y= -27 }, - { .x= 138.518 , .y= -26 }, - { .x= 130.421 , .y= -25 }, - { .x= 122.847 , .y= -24 }, - { .x= 115.759 , .y= -23 }, - { .x= 109.122 , .y= -22 }, - { .x= 102.906 , .y= -21 }, - { .x= 97.081 , .y= -20 }, - { .x= 91.621 , .y= -19 }, - { .x= 86.501 , .y= -18 }, - { .x= 81.698 , .y= -17 }, - { .x= 77.19 , .y= -16 }, - { .x= 72.957 , .y= -15 }, - { .x= 68.982 , .y= -14 }, - { .x= 65.246 , .y= -13 }, - { .x= 61.736 , .y= -12 }, - { .x= 58.434 , .y= -11 }, - { .x= 55.329 , .y= -10 }, - { .x= 52.407 , .y= -9 }, - { .x= 49.656 , .y= -8 }, - { .x= 47.066 , .y= -7 }, - { .x= 44.626 , .y= -6 }, - { .x= 42.327 , .y= -5 }, - { .x= 40.159 , .y= -4 }, - { .x= 38.115 , .y= -3 }, - { .x= 36.187 , .y= -2 }, - { .x= 34.368 , .y= -1 }, - { .x= 32.65 , .y= 0 }, - { .x= 31.029 , .y= 1 }, - { .x= 29.498 , .y= 2 }, - { .x= 28.052 , .y= 3 }, - { .x= 26.685 , .y= 4 }, - { .x= 25.392 , .y= 5 }, - { .x= 24.17 , .y= 6 }, - { .x= 23.013 , .y= 7 }, - { .x= 21.918 , .y= 8 }, - { .x= 20.882 , .y= 9 }, - { .x= 19.901 , .y= 10 }, - { .x= 18.971 , .y= 11 }, - { .x= 18.09 , .y= 12 }, - { .x= 17.255 , .y= 13 }, - { .x= 16.463 , .y= 14 }, - { .x= 15.712 , .y= 15 }, - { .x= 14.999 , .y= 16 }, - { .x= 14.323 , .y= 17 }, - { .x= 13.681 , .y= 18 }, - { .x= 13.072 , .y= 19 }, - { .x= 12.493 , .y= 20 }, - { .x= 11.942 , .y= 21 }, - { .x= 11.419 , .y= 22 }, - { .x= 10.922 , .y= 23 }, - { .x= 10.45 , .y= 24 }, - { .x= 10 , .y= 25 }, - { .x= 9.572 , .y= 26 }, - { .x= 9.165 , .y= 27 }, - { .x= 8.777 , .y= 28 }, - { .x= 8.408 , .y= 29 }, - { .x= 8.057 , .y= 30 }, - { .x= 7.722 , .y= 31 }, - { .x= 7.402 , .y= 32 }, - { .x= 7.098 , .y= 33 }, - { .x= 6.808 , .y= 34 }, - { .x= 6.531 , .y= 35 }, - { .x= 6.267 , .y= 36 }, - { .x= 6.015 , .y= 37 }, - { .x= 5.775 , .y= 38 }, - { .x= 5.545 , .y= 39 }, - { .x= 5.326 , .y= 40 }, - { .x= 5.117 , .y= 41 }, - { .x= 4.917 , .y= 42 }, - { .x= 4.725 , .y= 43 }, - { .x= 4.543 , .y= 44 }, - { .x= 4.368 , .y= 45 }, - { .x= 4.201 , .y= 46 }, - { .x= 4.041 , .y= 47 }, - { .x= 3.888 , .y= 48 }, - { .x= 3.742 , .y= 49 }, - { .x= 3.602 , .y= 50 }, - { .x= 3.468 , .y= 51 }, - { .x= 3.34 , .y= 52 }, - { .x= 3.217 , .y= 53 }, - { .x= 3.099 , .y= 54 }, - { .x= 2.986 , .y= 55 }, - { .x= 2.878 , .y= 56 }, - { .x= 2.774 , .y= 57 }, - { .x= 2.675 , .y= 58 }, - { .x= 2.579 , .y= 59 }, - { .x= 2.488 , .y= 60 }, - { .x= 2.4 , .y= 61 }, - { .x= 2.316 , .y= 62 }, - { .x= 2.235 , .y= 63 }, - { .x= 2.157 , .y= 64 }, - { .x= 2.083 , .y= 65 }, - { .x= 2.011 , .y= 66 }, - { .x= 1.942 , .y= 67 }, - { .x= 1.876 , .y= 68 }, - { .x= 1.813 , .y= 69 }, - { .x= 1.752 , .y= 70 }, - { .x= 1.693 , .y= 71 }, - { .x= 1.637 , .y= 72 }, - { .x= 1.582 , .y= 73 }, - { .x= 1.53 , .y= 74 }, - { .x= 1.48 , .y= 75 }, - { .x= 1.432 , .y= 76 }, - { .x= 1.385 , .y= 77 }, - { .x= 1.34 , .y= 78 }, - { .x= 1.297 , .y= 79 }, - { .x= 1.255 , .y= 80 }, - }; - -float ReadTemp(const Temp_Resistance *table, float value, size_t SIZE) -{ - //Search using Binary Search. Logarithmic passes - int result=SIZE, slope=0; - size_t firstPos=0; - size_t lastPos=SIZE; - size_t middlePos=0; - size_t temp_mid=1; - //printf("Value:%f\n\r",value); - while(temp_mid!=middlePos) - { - temp_mid=middlePos; - middlePos = firstPos + (lastPos-firstPos)/2; - if(table[middlePos].x == value) - { - lastPos = middlePos; - result = middlePos; - } - - if(table[middlePos].x < value) - { - lastPos = middlePos; - result = middlePos; - } - - else if(table[middlePos].x > value) - { - firstPos = middlePos; - } - } - //printf("Result:%d\n\r",result); - if(result == 0) - return table[result].y; - else if(result == SIZE) - return table[result - 1].y; - - slope = (table[result].y - table[result-1].y)/(table[result].x - table[result-1].x); - return (slope*(value - table[result].x) + table[result].y); -} -#endif /*_TEMPERATURE_READ_*/ \ No newline at end of file