| 1 | /******************************************************************************
|
|---|
| 2 | *
|
|---|
| 3 | * @file chipTemperature.c
|
|---|
| 4 | * @author ECS, Joseph Zimmer
|
|---|
| 5 | * @version V1.0.0
|
|---|
| 6 | * @date 24-04-2019
|
|---|
| 7 | * @brief
|
|---|
| 8 | *
|
|---|
| 9 | ******************************************************************************/
|
|---|
| 10 |
|
|---|
| 11 | // --- INCLUDES -----------------------------------------------------------------
|
|---|
| 12 | #include <stdio.h>
|
|---|
| 13 | //#include <stdlib.h>
|
|---|
| 14 | #include "chip_temperature.h"
|
|---|
| 15 | #include "sysdata.h"
|
|---|
| 16 | // --- EXTERNE VARIABLEN --------------------------------------------------------
|
|---|
| 17 |
|
|---|
| 18 | // --- LOKALE DEFINES - bitte hier dokumentieren --------------------------------
|
|---|
| 19 |
|
|---|
| 20 | // --- LOKALE TYPE DEFS - bitte hier dokumentieren-------------------------------
|
|---|
| 21 |
|
|---|
| 22 | // --- DEFINITIONEN GLOBALER VARIABLEN - Bitte in Header dokumentieren ----------
|
|---|
| 23 |
|
|---|
| 24 | // --- LOKALE VARIABLEN - bitte hier dokumentieren ------------------------------
|
|---|
| 25 | uint32_t calTemperatureSensor30Value; // Kalibrierungswert für den Temperatursensor auf dem STM32G0 (Werksmäßig im SCB Bereich gespeichert wird beim Programmstart ausgelesen)
|
|---|
| 26 | uint32_t calTemperatureSensor110Value; // Kalibrierungswert für den Temperatursensor auf dem STM32G0 (Werksmäßig im SCB Bereich gespeichert wird beim Programmstart ausgelesen)
|
|---|
| 27 | uint32_t calTemperatureSensorDiff; // Differenz calTemperatureSensor130Value und calTemperatureSensor30Value wird für die Kalibrierung des internen Temperatursensors bentigt
|
|---|
| 28 | // Daten Temperaturanzeige µProzessor
|
|---|
| 29 | double slope;
|
|---|
| 30 | // --- LOKALE FUNKTIONS PROTOTYPEN ----------------------------------------------
|
|---|
| 31 |
|
|---|
| 32 | void calc_temp_compensation(void);
|
|---|
| 33 | int16_t y(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x);
|
|---|
| 34 |
|
|---|
| 35 | // --- LOKALE FUNKTIONEN - bitte hier dokumentieren -----------------------------
|
|---|
| 36 |
|
|---|
| 37 | /*
|
|---|
| 38 | * @brief
|
|---|
| 39 | * @param kein
|
|---|
| 40 | * @retval kein
|
|---|
| 41 | */
|
|---|
| 42 |
|
|---|
| 43 | // --- GLOBALE FUNKTIONEN - bitte in Header dokumentieren------------------------
|
|---|
| 44 | void CHIP_TEMPERATURE_Calibration(void)
|
|---|
| 45 | {
|
|---|
| 46 | uint16_t * pCalibrationData;
|
|---|
| 47 | float calibrationData30;
|
|---|
| 48 | float calibrationData110;
|
|---|
| 49 |
|
|---|
| 50 | // lade Temperatur Kalibrierungswert (Wert bei 30°C)
|
|---|
| 51 | pCalibrationData = (uint16_t *)TEMPSENSOR_CAL1_ADDR;
|
|---|
| 52 | calibrationData30 = * pCalibrationData;
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | //Berechnung Spannung in mV bei CAL Punk 30°C
|
|---|
| 56 | //Kalbibrierung wurde mit 12 Bit und 3000mV Vref durchgeführt
|
|---|
| 57 | calTemperatureSensor30Value = (calibrationData30 * 3000) / 4096 ; // jetzt haben wir die Kalibrierungsspannung in Volt bei 30°C;
|
|---|
| 58 |
|
|---|
| 59 | pCalibrationData = (uint16_t *)TEMPSENSOR_CAL2_ADDR;
|
|---|
| 60 | calibrationData110 = * pCalibrationData;
|
|---|
| 61 |
|
|---|
| 62 | //Berechnung Spannung in mV bei CAL Punk 110°C
|
|---|
| 63 | //Kalbibrierung wurde mit 12 Bit und 3000mV Vref durchgeführt
|
|---|
| 64 | calTemperatureSensor110Value = (calibrationData110 * 3000 / 4096); // jetzt haben wir die Kalibrierungsspannung in Volt bei 110°C;
|
|---|
| 65 |
|
|---|
| 66 | // Spannungsdifferenz bei 100 Kelvin Temperatureunterschied
|
|---|
| 67 | // Slope enthält die Änderung in mV pro °C
|
|---|
| 68 | calTemperatureSensorDiff = (calTemperatureSensor110Value - calTemperatureSensor30Value);
|
|---|
| 69 | slope = (double) calTemperatureSensorDiff / (TEMPSENSOR_CAL2_TEMP - TEMPSENSOR_CAL1_TEMP);
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | void CHIP_TEMPERATURE_Exec(uint32_t chiptemperature)
|
|---|
| 75 | {
|
|---|
| 76 | double voltage;
|
|---|
| 77 | //Aktuelle Spannung am Temp Sensor
|
|---|
| 78 | voltage = (3000 * chiptemperature) / 65536;
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | voltage = voltage - calTemperatureSensor30Value;
|
|---|
| 82 | voltage = voltage / slope;
|
|---|
| 83 | voltage = voltage + 30.0;
|
|---|
| 84 |
|
|---|
| 85 | //*100 für Kommastellen
|
|---|
| 86 | sys_data.s.values.chipTemperature = voltage * 100 ;
|
|---|
| 87 |
|
|---|
| 88 | calc_temp_compensation();
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | //------------------------------------------------------------------------------
|
|---|
| 92 |
|
|---|
| 93 | void calc_temp_compensation(void)
|
|---|
| 94 | {
|
|---|
| 95 | #define sv sys_data.s.values
|
|---|
| 96 | #define sp sys_data.s.parameter
|
|---|
| 97 |
|
|---|
| 98 | sv.uBatEmptyTempComp = sp.uBatEmpty;
|
|---|
| 99 |
|
|---|
| 100 | if (sv.chipTemperature < sp.uBatEmptyCompStartTemp)
|
|---|
| 101 | {
|
|---|
| 102 | int16_t currentTemp = sv.chipTemperature;
|
|---|
| 103 | if (sv.chipTemperature <= sp.uBatEmptyCompStopTemp) currentTemp = sp.uBatEmptyCompStopTemp;
|
|---|
| 104 |
|
|---|
| 105 | sv.uBatEmptyTempComp = y(sp.uBatEmptyCompStopTemp, // x1 -20°C
|
|---|
| 106 | sp.uBatEmptyCompStopVolt, // y1 2.5V * 4 = 10V oder 2.5V * 8 = 20V
|
|---|
| 107 | sp.uBatEmptyCompStartTemp, // x2 5°C
|
|---|
| 108 | sp.uBatEmpty, // y2 3.1V * 4 = 12.4V oder 3.1V * 8 = 24.8V
|
|---|
| 109 | currentTemp); // x T°C
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | #undef sp
|
|---|
| 113 | #undef sv
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | //------------------------------------------------------------------------------
|
|---|
| 117 |
|
|---|
| 118 | /*!
|
|---|
| 119 | * \brief Linear Rescale function.
|
|---|
| 120 | *
|
|---|
| 121 | * Converts all numbers from range [x1, x2] into range [y1, y2].
|
|---|
| 122 | * x1 must not be equal to x2.
|
|---|
| 123 | *
|
|---|
| 124 | * \param x1: left limit of the input range.
|
|---|
| 125 | * \param x2: right limit of the input range.
|
|---|
| 126 | * \param y1: left limit of the output range.
|
|---|
| 127 | * \param y2: right limit of the output range.
|
|---|
| 128 | * \param x: number to convert from the input range.
|
|---|
| 129 | * \return converted result from the output range.
|
|---|
| 130 | */
|
|---|
| 131 | int16_t y(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x)
|
|---|
| 132 | {
|
|---|
| 133 | int32_t X1 = x1 <= x2 ? x1: x2;
|
|---|
| 134 | int32_t Y1 = y1 <= y2 ? y1: y2;
|
|---|
| 135 | int32_t X2 = x2 > x1 ? x2: x1;
|
|---|
| 136 | int32_t Y2 = y2 > y1 ? y2: y1;
|
|---|
| 137 | int32_t X = x;
|
|---|
| 138 |
|
|---|
| 139 | int32_t div = X2 - X1;
|
|---|
| 140 | if (!div) return 0;
|
|---|
| 141 | int32_t fraction = (Y2 - Y1) * (X - X1) + Y1 * (X2 - X1);
|
|---|
| 142 | int32_t res = fraction / div;
|
|---|
| 143 |
|
|---|
| 144 | return (int16_t)res;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /*************************** End of file ****************************/
|
|---|