| 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 calTemperatureSensor130Value; // 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 |
|
|---|
| 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 calibrationData130;
|
|---|
| 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 | calibrationData30 = calibrationData30 / 4096;
|
|---|
| 58 | calTemperatureSensor30Value = calibrationData30 * 3000 ; // jetzt haben wir die Kalibrierungsspannung in Volt bei 30°C;
|
|---|
| 59 |
|
|---|
| 60 | pCalibrationData = (uint16_t *)TEMPSENSOR_CAL2_ADDR;
|
|---|
| 61 | calibrationData130 = * pCalibrationData;
|
|---|
| 62 |
|
|---|
| 63 | //Berechnung Spannung in mV bei CAL Punk 130°C
|
|---|
| 64 | //Kalbibrierung wurde mit 12 Bit und 3000mV Vref durchgeführt
|
|---|
| 65 | calibrationData130 = calibrationData130 / 4096;
|
|---|
| 66 | calTemperatureSensor130Value = calibrationData130 * 3000; // jetzt haben wir die Kalibrierungsspannung in Volt bei 130°C;
|
|---|
| 67 |
|
|---|
| 68 | // Spannungsdifferenz bei 100 Kelvin Temperatureunterschied
|
|---|
| 69 | calTemperatureSensorDiff = (calTemperatureSensor130Value - calTemperatureSensor30Value);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | void CHIP_TEMPERATURE_Exec(uint32_t chiptemperature)
|
|---|
| 73 | {
|
|---|
| 74 | int32_t voltage;
|
|---|
| 75 | //Aktuelle Spannung am Temp Sensor
|
|---|
| 76 | voltage = (3300 * chiptemperature) / 65536;
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 | voltage = voltage - calTemperatureSensor30Value;
|
|---|
| 80 | voltage = voltage * 100000; //100000 da Kalibrierwerte 100 Kelvin Delta T haben und wir alles in m°C rechnen
|
|---|
| 81 | voltage = voltage / (int32_t)(calTemperatureSensorDiff);
|
|---|
| 82 | voltage = voltage + 30000; //30000 da Erste Kalibrierpunkt bei 30°C --> 30 000 m°C
|
|---|
| 83 |
|
|---|
| 84 | //Durch 10 teilen, damit es in 16 Bit signed modbus register passt
|
|---|
| 85 | sys_data.s.values.chipTemperature = voltage /10 ;
|
|---|
| 86 |
|
|---|
| 87 | calc_temp_compensation();
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | //------------------------------------------------------------------------------
|
|---|
| 91 |
|
|---|
| 92 | void calc_temp_compensation(void)
|
|---|
| 93 | {
|
|---|
| 94 | #define sv sys_data.s.values
|
|---|
| 95 | #define sp sys_data.s.parameter
|
|---|
| 96 |
|
|---|
| 97 | sv.uBatEmptyTempComp = sp.uBatEmpty;
|
|---|
| 98 |
|
|---|
| 99 | if (sv.chipTemperature < sp.uBatEmptyCompStartTemp)
|
|---|
| 100 | {
|
|---|
| 101 | int16_t currentTemp = sv.chipTemperature;
|
|---|
| 102 | if (sv.chipTemperature <= sp.uBatEmptyCompStopTemp) currentTemp = sp.uBatEmptyCompStopTemp;
|
|---|
| 103 |
|
|---|
| 104 | sv.uBatEmptyTempComp = y(sp.uBatEmptyCompStopTemp, // x1 -20°C
|
|---|
| 105 | sp.uBatEmptyCompStopVolt, // y1 2.5V * 4 = 10V oder 2.5V * 8 = 20V
|
|---|
| 106 | sp.uBatEmptyCompStartTemp, // x2 5°C
|
|---|
| 107 | sp.uBatEmpty, // y2 3.1V * 4 = 12.4V oder 3.1V * 8 = 24.8V
|
|---|
| 108 | currentTemp); // x T°C
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | #undef sp
|
|---|
| 112 | #undef sv
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | //------------------------------------------------------------------------------
|
|---|
| 116 |
|
|---|
| 117 | /*!
|
|---|
| 118 | * \brief Linear Rescale function.
|
|---|
| 119 | *
|
|---|
| 120 | * Converts all numbers from range [x1, x2] into range [y1, y2].
|
|---|
| 121 | * x1 must not be equal to x2.
|
|---|
| 122 | *
|
|---|
| 123 | * \param x1: left limit of the input range.
|
|---|
| 124 | * \param x2: right limit of the input range.
|
|---|
| 125 | * \param y1: left limit of the output range.
|
|---|
| 126 | * \param y2: right limit of the output range.
|
|---|
| 127 | * \param x: number to convert from the input range.
|
|---|
| 128 | * \return converted result from the output range.
|
|---|
| 129 | */
|
|---|
| 130 | int16_t y(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x)
|
|---|
| 131 | {
|
|---|
| 132 | int32_t X1 = x1 <= x2 ? x1: x2;
|
|---|
| 133 | int32_t Y1 = y1 <= y2 ? y1: y2;
|
|---|
| 134 | int32_t X2 = x2 > x1 ? x2: x1;
|
|---|
| 135 | int32_t Y2 = y2 > y1 ? y2: y1;
|
|---|
| 136 | int32_t X = x;
|
|---|
| 137 |
|
|---|
| 138 | int32_t div = X2 - X1;
|
|---|
| 139 | if (!div) return 0;
|
|---|
| 140 | int32_t fraction = (Y2 - Y1) * (X - X1) + Y1 * (X2 - X1);
|
|---|
| 141 | int32_t res = fraction / div;
|
|---|
| 142 |
|
|---|
| 143 | return (int16_t)res;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /*************************** End of file ****************************/
|
|---|