Skip to content
Snippets Groups Projects
Commit 1f291497 authored by Flax's avatar Flax
Browse files

STM32L011K4 : updated temperature regulation.

parent 6743590f
No related branches found
No related tags found
No related merge requests found
......@@ -38,6 +38,8 @@
#define cREGAdcErrorThres (4090U)
#define cREGAdcToTempFilter (200U)
// ===============
// Local variables
// ===============
......@@ -52,10 +54,13 @@ static uint16_t RegTempMeasBuffer_U16A[2U];
static int32_t RegPwmDyc_S32;
static uint32_t RegPwmDyc_U32;
static uint16_t RegTempDeg_U16;
static uint16_t RegTempDegFilt_U16;
static uint16_t RegTempFiltCnt_U16;
// PID
static int32_t RegEpsilon_S32A[2U];
// ==========================
// Local functions prototypes
// ==========================
......@@ -81,7 +86,6 @@ void REGInit (void)
void REGStart (void)
{
mREDAdcEnable();
// mREGAdcStartConversion();
}
void REGStop (void)
......@@ -102,6 +106,7 @@ void REGCyclicTask (void)
if (RegTempMeasBuffer_U16A[1] >= cREGAdcErrorThres)
{
// Error
RegErrorState_B = true;
}
}
else
......@@ -116,25 +121,35 @@ void REGCyclicTask (void)
RegTempMeas_U16[1] = RegTempMeas_U16[0]; // Memorise previous value
RegTempMeas_U16[0] = RegTempMeasBuffer_U16A[0];
// Convert to degrees
RegTempDeg_U16 = (uint16_t)(((((uint32_t)ADC_TO_TEMP_GAIN_NUM * (uint32_t)(RegTempMeas_U16[0])) / (uint32_t)ADC_TO_TEMP_GAIN_DEN) + (uint32_t)ADC_TO_TEMP_OFFSET) & (uint32_t)0x0000FFFF);
// PID
RegEpsilon_S32A[1] = RegEpsilon_S32A[0]; // Memorise previous value
RegEpsilon_S32A[0] = RegTempSPT_U16 - RegTempMeas_U16[0];
RegEpsilon_S32A[0] = RegTempSPT_U16 - RegTempDeg_U16;
RegPwmDyc_S32 = (RegEpsilon_S32A[0] * cRegPIDProp)
+ ((RegEpsilon_S32A[0] - RegEpsilon_S32A[1]) * cRegPIDDeriv)
+ (((RegEpsilon_S32A[0] + RegEpsilon_S32A[1]) >> 1U) * cRegPIDInteg);
+ ((RegEpsilon_S32A[0] + RegEpsilon_S32A[1]) * cRegPIDInteg);
// Only keep positive values
if (RegPwmDyc_S32 > 0)
// Check regulation state before processing the output PWM
if ((RegErrorState_B == false) && (RegOffState_B == false))
{
RegPwmDyc_U32 = RegPwmDyc_S32;
}
else if (RegPwmDyc_S32 < (uint32_t)cREGPwmMax)
{
RegPwmDyc_U32 = (uint32_t)cREGPwmMax;
if (RegPwmDyc_S32 > 0)
{// Only keep positive values
RegPwmDyc_U32 = RegPwmDyc_S32;
}
else if (RegPwmDyc_S32 > (int32_t)cREGPwmMax)
{
RegPwmDyc_U32 = (uint32_t)cREGPwmMax;
}
else
{
RegPwmDyc_U32 = 0U;
}
}
else
{
{ // Shut the PWM down
RegPwmDyc_U32 = 0U;
}
......@@ -143,7 +158,15 @@ void REGCyclicTask (void)
// Calculate physical temperature
// Transfer function
RegTempDeg_U16 = (uint16_t)(((((uint32_t)ADC_TO_TEMP_GAIN_NUM * (uint32_t)(RegTempMeas_U16[0])) / (uint32_t)ADC_TO_TEMP_GAIN_DEN) + (uint32_t)ADC_TO_TEMP_OFFSET) & (uint32_t)0x0000FFFF);
if (RegTempFiltCnt_U16 < cREGAdcToTempFilter)
{
RegTempFiltCnt_U16++;
}
else
{ // Sub-sampling for cleaner display
RegTempFiltCnt_U16 = 0U;
RegTempDegFilt_U16 = RegTempDeg_U16;
}
}
bool REGIsError (void)
......@@ -204,7 +227,7 @@ uint16_t REGSPTGet (void)
uint16_t REGTempGet (void)
{
return RegTempMeas_U16[0];
return RegTempDegFilt_U16;
}
bool REGSPTStbySet (uint16_t temp_spt_u16)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment