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

STM32L011K4 : Added temperature regulation (draft).

parent 126b6819
No related branches found
No related tags found
No related merge requests found
......@@ -101,4 +101,13 @@
#define mREGInputsENC2ExtiEnableIT() (LL_EXTI_EnableIT_0_31(mREGInputsENC1ExtiLine))
#define mREGInputsENC2ExtiClearFlag() (LL_EXTI_ClearFlag_0_31(mREGInputsENC1ExtiLine))
/* ADC --------------------------------------------------------*/
#define mREGAdcHandle ADC1
#define mREGAdcStartConversion() (LL_ADC_REG_StartConversion(mREGAdcHandle))
#define mREGAdcStopConversion() (LL_ADC_REG_StopConversion(mREGAdcHandle))
#define mREGAdcReadValue() (LL_ADC_REG_ReadConversionData12(mREGAdcHandle))
/* PWM --------------------------------------------------------*/
#define mREGPwmSetCompareValue(val) (LL_TIM_OC_SetCompareCH1(TIM2, (uint32_t)val))
#endif /* INC_REGISTER_H_ */
......@@ -11,6 +11,7 @@
// Local includes
// ==============
#include "temperatureRegulation.h"
#include "register.h"
// ===============
// Local constants
......@@ -18,22 +19,31 @@
#define cREGTempSPTMax (400U)
#define cREGTempSPTMin (50U)
#define cREGPwmMax (65535)
#define cRegPIDProp (10)
#define cRegPIDInteg (10)
#define cRegPIDDeriv (10)
// ===============
// Local variables
// ===============
bool RegOffState_B;
bool RegStandbyState_B;
bool RegErrorState_B;
static bool RegOffState_B;
static bool RegStandbyState_B;
static bool RegErrorState_B;
uint16_t RegTempSPT_U16;
uint16_t RegTempMeas_U16;
static uint16_t RegTempSPT_U16;
static uint16_t RegTempMeas_U16[2U];
static uint16_t RegTempMeasBuffer_U16A[2U];
static int32_t RegPwmDyc_S32;
static uint32_t RegPwmDyc_U32;
// PID
static int32_t RegEpsilon_S32A[2U];
// ==========================
// Local functions prototypes
// ==========================
static void RegGetInputs (void);
static void RegUpdate (void);
static void RegSetOutputs (void);
// ===========================
// Local functions definitions
......@@ -51,17 +61,51 @@ void REGInit (void)
void REGStart (void)
{
mREGAdcStartConversion();
}
void REGStop (void)
{
mREGAdcStopConversion();
}
void REGCyclicTask (void)
{
uint8_t cnt_u8;
// Read ADC input
RegTempMeasBuffer_U16A[1] = mREGAdcReadValue();
// Calculate average
RegTempMeasBuffer_U16A[0] += RegTempMeasBuffer_U16A[1];
RegTempMeasBuffer_U16A[0] >> 1U;
RegTempMeas_U16[1] = RegTempMeas_U16[0]; // Memorise previous value
RegTempMeas_U16[0] = RegTempMeasBuffer_U16A[0];
// PID
RegEpsilon_S32A[1] = RegEpsilon_S32A[0]; // Memorise previous value
RegEpsilon_S32A[0] = RegTempSPT_U16 - RegTempMeas_U16[0];
RegPwmDyc_S32 = (RegEpsilon_S32A[0] * cRegPIDProp)
+ ((RegEpsilon_S32A[0] - RegEpsilon_S32A[1]) * cRegPIDDeriv)
+ (((RegEpsilon_S32A[0] + RegEpsilon_S32A[1]) >> 1U) * cRegPIDInteg);
// Only keep positive values
if (RegPwmDyc_S32 > 0)
{
RegPwmDyc_U32 = RegPwmDyc_S32;
}
else if (RegPwmDyc_S32 < (uint32_t)cREGPwmMax)
{
RegPwmDyc_U32 = (uint32_t)cREGPwmMax;
}
else
{
RegPwmDyc_U32 = 0U;
}
// Set PWM output duty cycle
mREGPwmSetCompareValue(RegPwmDyc_U32);
}
bool REGIsError (void)
......@@ -122,5 +166,5 @@ uint16_t REGSPTGet (void)
uint16_t REGTempGet (void)
{
return RegTempMeas_U16;
return RegTempMeas_U16[0];
}
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