diff options
| author | Avi Levkovich <avi@twine-s.com> | 2018-04-26 14:08:20 +0300 |
|---|---|---|
| committer | Avi Levkovich <avi@twine-s.com> | 2018-04-26 14:08:20 +0300 |
| commit | b079f2e9ec3f7f514b1a04228d5f7accdb2f9560 (patch) | |
| tree | feb9e70993a49549f18f84defd14282fa92d5152 /Software/Embedded_SW/Embedded/Drivers/Heater/TemperatureSensor.c | |
| parent | d352d3b3bd785d9eb8a93347333de0b357f7ce0e (diff) | |
| download | Tango-b079f2e9ec3f7f514b1a04228d5f7accdb2f9560.tar.gz Tango-b079f2e9ec3f7f514b1a04228d5f7accdb2f9560.zip | |
update the temperature sensor module
Diffstat (limited to 'Software/Embedded_SW/Embedded/Drivers/Heater/TemperatureSensor.c')
| -rw-r--r-- | Software/Embedded_SW/Embedded/Drivers/Heater/TemperatureSensor.c | 218 |
1 files changed, 198 insertions, 20 deletions
diff --git a/Software/Embedded_SW/Embedded/Drivers/Heater/TemperatureSensor.c b/Software/Embedded_SW/Embedded/Drivers/Heater/TemperatureSensor.c index 86460bbe0..2501fea9b 100644 --- a/Software/Embedded_SW/Embedded/Drivers/Heater/TemperatureSensor.c +++ b/Software/Embedded_SW/Embedded/Drivers/Heater/TemperatureSensor.c @@ -11,6 +11,68 @@ #include "PMR/Hardware/HardwarePidControlType.pb-c.h" #include "Drivers/Heater/ADS1220.h" #include "Drivers/Heater/TemperatureSensor.h" +#include "drivers/FPGA/FPGA_SPI_Comm.h" +#include "modules/control/millisecTask.h" +#include "PT100RTD.h" + +uint32_t TemperatureSensorReadFromFPGA_Res(TEMPERATURE_SENSOR_ID_ENUM SensorId,uint32_t Data); + +typedef enum +{ + SENSOR_CONFIG_REG0, + SENSOR_CONFIG_REG1, + SENSOR_CONFIG_REG2, + SENSOR_CONFIG_REG3, + SENSOR_CONFIG_RESTART_CONVERSIONS, + SENSOR_CONFIG_END + +}SENSOR_CONFIG_STAGES_ENUM; + +SENSOR_CONFIG_STAGES_ENUM SensConfigStages[MAX_TEMPERATURE_SENSOR_ID] = {SENSOR_CONFIG_REG0}; + +uint8_t FPGA_SensorInitConfigReg() +{ + #define Rref 1650 //ohm + //#define Rref 825 //ohm + + uint8_t i; + + for(i=0;i<MAX_TEMPERATURE_SENSOR_ID;i++) // for noe we are using the same configuration to all of them + { + TempSensConfig[i].Reg0.bits.PGA_BYPASS = 0x00 ;//Bit 0 PGA enabled (default) + + if(Rref==1650) + TempSensConfig[i].Reg0.bits.GAIN = 0x04;//Bits 1-3 Gain = 16 + else if(Rref == 825) + TempSensConfig[i].Reg0.bits.GAIN = 0x03;//Bits 1-3 Gain = 8 + + TempSensConfig[i].Reg0.bits.MUX = 0x06;//Bits 5-7 AINP = AIN1, AINN = AIN0 + + TempSensConfig[i].Reg1.bits.BCS = 0x00;//Bit 0 Current sources off (default) + TempSensConfig[i].Reg1.bits.TS = 0x00;//Bit 1 Disables temperature sensor (default) + TempSensConfig[i].Reg1.bits.CM = 0x01;//Bit 2 Continuous conversion mode + TempSensConfig[i].Reg1.bits.MODE = 0x00;//Bits 3-4 Normal mode (256-kHz modulator clock, default) + //TempSensConfig[i].Reg1.bits.DR = 0x06;//Bits 5-7 Data rate 1000 SPS (Sample every 1 mSec) + TempSensConfig[i].Reg1.bits.DR = 0x00;//Bits 5-7 Data rate 20 SPS (Sample every 50 mSec) + + if(Rref==1650) + TempSensConfig[i].Reg2.bits.IDAC = 0x03;//Bits 0-2 IDAC current setting 100 uA + else if(Rref == 825) + TempSensConfig[i].Reg2.bits.IDAC = 0x06;//Bits 0-2 IDAC current setting 1000 uA + + TempSensConfig[i].Reg2.bits.PSW = 0x00;//Bit 3 Switch is always open (default) + TempSensConfig[i].Reg2.bits.HZ50_60 = 0x01;//Bits 4-5 No 50-Hz or 60-Hz rejection (default) + TempSensConfig[i].Reg2.bits.VREF = 0x01;//Bits 6-7 Internal 2.048-V reference selected (default) + + TempSensConfig[i].Reg3.bits.Always_write_0 = 0x00;//Bit 0; + TempSensConfig[i].Reg3.bits.DRDYM = 0x00;//Bit 1 Only the dedicated DRDY pin is used to indicate when data are ready (default) + TempSensConfig[i].Reg3.bits.I2MUX = 0x04;//Bits 2-4 IDAC2 connected to AIN3/REFN1 + TempSensConfig[i].Reg3.bits.I1MUX = 0x03;//Bits 5-7 IDAC1 connected to AIN2 + + } + + return OK; +} uint32_t TemperatureSensorsInit(void) { @@ -25,22 +87,11 @@ uint32_t TemperatureSensorsInit(void) } #endif + FPGA_SensorInitConfigReg(); + return OK; } -typedef enum -{ - SENSOR_CONFIG_REG0, - SENSOR_CONFIG_REG1, - SENSOR_CONFIG_REG2, - SENSOR_CONFIG_REG3, - SENSOR_CONFIG_RESTART_CONVERSIONS, - SENSOR_CONFIG_END - -}SENSOR_CONFIG_STAGES_ENUM; - -SENSOR_CONFIG_STAGES_ENUM SensConfigStages[MAX_TEMPERATURE_SENSOR_ID] = {SENSOR_CONFIG_REG0}; - uint32_t FPGA_SensorConfig_callback(TEMPERATURE_SENSOR_ID_ENUM SensorId, uint32_t ReadValue) { #ifndef EVALUATION_BOARD @@ -57,7 +108,7 @@ uint32_t FPGA_SensorConfig_callback(TEMPERATURE_SENSOR_ID_ENUM SensorId, uint32_ temp = temp << 8; temp |= TempSensConfig[SensorId].Reg0.Byte; - //MillisecWriteToSenor(SensorId, temp, 2, FPGA_SensorConfig_callback); + MillisecWriteToTempSensor(SensorId, temp, 2, NULL); break; case SENSOR_CONFIG_REG1: @@ -66,7 +117,7 @@ uint32_t FPGA_SensorConfig_callback(TEMPERATURE_SENSOR_ID_ENUM SensorId, uint32_ temp = temp << 8; temp |= TempSensConfig[SensorId].Reg1.Byte; - //MillisecWriteToSenor(SensorId, temp, 2, FPGA_SensorConfig_callback); + MillisecWriteToTempSensor(SensorId, temp, 2, NULL); break; case SENSOR_CONFIG_REG2: @@ -75,7 +126,7 @@ uint32_t FPGA_SensorConfig_callback(TEMPERATURE_SENSOR_ID_ENUM SensorId, uint32_ temp = temp << 8; temp |= TempSensConfig[SensorId].Reg2.Byte; - //MillisecWriteToSenor(SensorId, temp, 2, FPGA_SensorConfig_callback); + MillisecWriteToTempSensor(SensorId, temp, 2, NULL); break; case SENSOR_CONFIG_REG3: @@ -84,7 +135,7 @@ uint32_t FPGA_SensorConfig_callback(TEMPERATURE_SENSOR_ID_ENUM SensorId, uint32_ temp = temp << 8; temp |= TempSensConfig[SensorId].Reg3.Byte; - //MillisecWriteToSenor(SensorId, temp, 2, FPGA_SensorConfig_callback); + MillisecWriteToTempSensor(SensorId, temp, 2, NULL); break; case SENSOR_CONFIG_RESTART_CONVERSIONS: @@ -92,7 +143,7 @@ uint32_t FPGA_SensorConfig_callback(TEMPERATURE_SENSOR_ID_ENUM SensorId, uint32_ //Start or restart conversions //The START/SYNC command is used to start a single conversion, or (when sent during an //ongoing conversion) to reset the digital filter, and then restarts a single new conversion - //MillisecWriteToSenor(SensorId, ADS1220_CMD_SYNC, 1, FPGA_SensorConfig_callback); + MillisecWriteToTempSensor(SensorId, ADS1220_CMD_SYNC, 1, NULL); break; case SENSOR_CONFIG_END: break; @@ -115,15 +166,142 @@ uint32_t TemperatureSensorRead(TEMPERATURE_SENSOR_ID_ENUM SensorId) else temperature[SensorId] -= 0.3; return (int)temperature[SensorId]; #else - //return OK; + return TempSensorResponse[SensorId].Temparature; #endif } + + + +uint32_t Filter_Temparature_Measurement(TEMPERATURE_SENSOR_ID_ENUM SensorId, int32_t Current_Measurement_C, uint32_t NoOfAvrSamples) +{ + #define MaxAvrSamples 100 //MAX of average samples + + uint32_t Filtered_Measurement; + + static int32_t Sample_buf[MAX_TEMPERATURE_SENSOR_ID][MaxAvrSamples] = {0}; + + static uint8_t SampleIndex[MAX_TEMPERATURE_SENSOR_ID] = {0}; + + uint8_t i; + uint32_t Status = OK; + + #ifdef TEMPERATURE_MEASUREMENT_HiGH_LIMIT + if (Current_Measurement_C > MAX_TEMPERATURE_MEASUREMENT) + { + Current_Measurement_C = MAX_TEMPERATURE_MEASUREMENT; + Status = ERROR; + } + #endif + + #ifdef TEMPERATURE_MEASUREMENT_LOW_LIMIT + if (Current_Measurement_C < MIN_TEMPERATURE_MEASUREMENT) + { + + Current_Measurement_C = MAX_TEMPERATURE_MEASUREMENT; + Status = ERROR; + } + #endif + + Sample_buf[SensorId][SampleIndex[SensorId]] = Current_Measurement_C; + + SampleIndex[SensorId] = SampleIndex[SensorId]++; + + if(SampleIndex[SensorId] > (NoOfAvrSamples - 1)) + SampleIndex[SensorId] = 0; + + int32_t TempSum = 0; + + for(i=0; i<NoOfAvrSamples; i++) + { + TempSum += Sample_buf[SensorId][i]; + } + + Filtered_Measurement = TempSum / NoOfAvrSamples; + + TempSensorResponse[SensorId].Temparature_C = Filtered_Measurement; + + return Status; +} + uint32_t TemperatureSensorReadFromFPGA(TEMPERATURE_SENSOR_ID_ENUM SensorId) { - return OK; + uint32_t Status = OK; + + // send the command byte + //SPISendFPGARequest( SensorId,ADS1220_CMD_RDATA, 3); + + MillisecReadFromTempSensor(SensorId, ADS1220_CMD_RDATA, 3, TemperatureSensorReadFromFPGA_Res); + + return Status; +} + +uint32_t Calc_Resistance(TEMPERATURE_SENSOR_ID_ENUM SensorId, unsigned int Current_Measurement) +{ + + uint32_t Resistance_100; + + unsigned int Factor_2Rref_E_H; + unsigned int Factor_2Rref_E_L; + + //Resistance = Measurement / (2^23) *2 * Rref / Gain + + Factor_2Rref_E_H = 200 * Rref; // 200 to calculate Resistance*100 + + Factor_2Rref_E_L = 8388608 * (2<<(TempSensConfig[SensorId].Reg0.bits.GAIN -1));//2^23*Gain + + Resistance_100 = (float)Current_Measurement / (float)Factor_2Rref_E_L * (float)Factor_2Rref_E_H; + + return Resistance_100; +} + +int32_t RTD (uint32_t Resistance_100) //Resistance * 100 +{ + int16_t i = 0; + int32_t Temperature_c; + + const int PT100_TABLE_MAXIDX = ((sizeof(Pt100_table) / sizeof(Pt100_table[0])) - 1) ; + + while( (Resistance_100 < Pt100_table[i]) && (i < PT100_TABLE_MAXIDX) ) + { + i++; + } + + Temperature_c = i-200; + + return Temperature_c; } +uint32_t TemperatureSensorReadFromFPGA_Res(TEMPERATURE_SENSOR_ID_ENUM SensorId,uint32_t Data) +{ + uint32_t Status = OK; + + int32_t Current_Measurement_C = 0; + + uint32_t Resistance_100; + + //uint32_t Data; + + //SPIGetFPGAResponse(SensorId, &Data); + + // sign extend data //TODO ? + if (Data & 0x800000) + Data |= 0xff000000; + + Resistance_100 = Calc_Resistance(SensorId, Data); + + Current_Measurement_C = RTD (Resistance_100); + + #ifdef WithTempSensorFiltrer + uint32_t NoOfAvrSamples = 5;//TODO - how many Samples + Status = Filter_Temparature_Measurement(SensorId, Current_Measurement_C, NoOfAvrSamples); + #else + TempSensorResponse[SensorId].Temparature_C = Current_Measurement_C; + #endif + + return Status; +} + |
