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

STM32L011K4 : added PWM cut when making an ADC read.

parent 26bb58ba
No related branches found
No related tags found
No related merge requests found
......@@ -112,7 +112,7 @@
#define mREGAdcStartConversion() (LL_ADC_REG_StartConversion(mREGAdcHandle))
#define mREGAdcStopConversion() (LL_ADC_REG_StopConversion(mREGAdcHandle))
#define mREGAdcReadValue() (LL_ADC_REG_ReadConversionData12(mREGAdcHandle))
#define mREGAdc
#define mREGAdcEndOfConversion() (LL_ADC_IsActiveFlag_EOC(mREGAdcHandle))
/* PWM --------------------------------------------------------*/
#define mREGPwmSetCompareValue(val) (LL_TIM_OC_SetCompareCH1(TIM2, (uint32_t)val))
......
......@@ -342,7 +342,7 @@ static void MX_TIM2_Init(void)
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
TIM_InitStruct.Prescaler = 20;
TIM_InitStruct.Prescaler = 0;
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
TIM_InitStruct.Autoreload = 1023;
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
......
......@@ -13,6 +13,17 @@
#include "temperatureRegulation.h"
#include "register.h"
// ==============
// Local typedefs
// ==============
typedef enum {
REG_MODE_OFF,
REG_MODE_REGUL,
REG_MODE_ADC_PRE,
REG_MODE_ADC_START,
REG_MODE_ADC_WAIT,
}e_REGMode;
// ===============
// Local constants
// ===============
......@@ -43,9 +54,15 @@
#define cREGAdcToTempFilter (50U)
#define cREGAdcAcqCycle (100U)
#define mREGSetPwm(val) (mREGPwmSetCompareValue(cREGPwmMax - val))
// ===============
// Local variables
// ===============
static e_REGMode RegModeCurrent_E;
static bool RegOffState_B;
static bool RegStandbyState_B;
static bool RegErrorState_B;
......@@ -61,6 +78,10 @@ static uint16_t RegTempDegFilt_U16;
static uint16_t RegTempFiltCnt_U16;
static uint32_t RegErrorTimeoutCnt_U32;
// ADC
static uint16_t RegAdcCycleCnt_U16;
static bool RegAdcOngoing_B;
// PID
static int32_t RegEpsilon_S32A[2U];
......@@ -82,6 +103,8 @@ void REGInit (void)
RegOffState_B = true;
RegStandbyState_B = false;
RegModeCurrent_E = REG_MODE_OFF;
// TODO : load NVM values
RegTempSPT_U16 = cREGTempSPTDft;
RegTempSPTStby_U16 = cREGTempSPTStbyDft;
......@@ -93,6 +116,8 @@ void REGStart (void)
mREDAdcEnable();
LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH1);
LL_TIM_EnableCounter(TIM2);
RegModeCurrent_E = REG_MODE_ADC_PRE;
}
void REGStop (void)
......@@ -102,38 +127,62 @@ void REGStop (void)
void REGCyclicTask (void)
{
if (mREGAdcIsReady())
switch (RegModeCurrent_E)
{
// Trigger conversion
mREGAdcStartConversion();
// Read ADC input
RegTempMeasBuffer_U16A[1] = mREGAdcReadValue();
if (RegTempMeasBuffer_U16A[1] >= cREGAdcErrorThres)
case REG_MODE_OFF:
break;
case REG_MODE_REGUL:
if (RegAdcCycleCnt_U16 < cREGAdcAcqCycle)
{
// Error
RegErrorState_B = true;
RegAdcCycleCnt_U16++;
}
}
else
{
// ADC not ready
RegTempMeasBuffer_U16A[1] = 0xFFFF;
}
if (RegTempFiltCnt_U16 < cREGAdcToTempFilter)
else
{
// RegTempFiltCnt_U16++;
if (mREGAdcIsReady())
{
RegAdcOngoing_B = true;
RegModeCurrent_E = REG_MODE_ADC_PRE;
}
else
{
// ADC not ready
RegTempMeasBuffer_U16A[1] = 0xFFFF;
RegAdcCycleCnt_U16 = 0;
}
}
else
break;
case REG_MODE_ADC_PRE:
RegModeCurrent_E = REG_MODE_ADC_START;
break;
case REG_MODE_ADC_START:
mREGAdcStartConversion();
RegModeCurrent_E = REG_MODE_ADC_WAIT;
break;
case REG_MODE_ADC_WAIT:
if (mREGAdcEndOfConversion() == true)
{
// Calculate average
RegTempMeasBuffer_U16A[0] += RegTempMeasBuffer_U16A[1];
RegTempMeasBuffer_U16A[0] = RegTempMeasBuffer_U16A[0] >> 1U;
RegTempMeas_U16[1] = RegTempMeas_U16[0]; // Memorise previous value
RegTempMeas_U16[0] = RegTempMeasBuffer_U16A[0];
// Read ADC input
RegTempMeasBuffer_U16A[1] = mREGAdcReadValue();
// Calculate average
RegTempMeasBuffer_U16A[0] += RegTempMeasBuffer_U16A[1];
RegTempMeasBuffer_U16A[0] = RegTempMeasBuffer_U16A[0] >> 1U;
RegTempMeas_U16[1] = RegTempMeas_U16[0]; // Memorise previous value
RegTempMeas_U16[0] = RegTempMeasBuffer_U16A[0];
if (RegTempMeasBuffer_U16A[1] >= cREGAdcErrorThres)
{
// Error
RegErrorState_B = true;
}
RegAdcCycleCnt_U16 = 0;
RegAdcOngoing_B = false;
RegModeCurrent_E = REG_MODE_REGUL;
}
break;
default:
break;
}
// 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);
......@@ -188,8 +237,17 @@ void REGCyclicTask (void)
}
// Set PWM output duty cycle
// Output is reversed
mREGPwmSetCompareValue(cREGPwmMax - RegPwmDyc_U32);
if (RegAdcOngoing_B == false)
{
// mREGSetPwm(cREGPwmMax);
mREGSetPwm(RegPwmDyc_U32);
//mREGPwmSetCompareValue(cREGPwmMax / 32);
}
else
{
// No switching during ADC acquisition
mREGSetPwm(0U);
}
// Filter physical temperature - for cleaner display
if (RegTempFiltCnt_U16 < cREGAdcToTempFilter)
......
......@@ -224,7 +224,7 @@ SPI1.VirtualType=VM_MASTER
TIM2.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
TIM2.IPParameters=Channel-PWM Generation1 CH1,Period,Prescaler
TIM2.Period=1023
TIM2.Prescaler=20
TIM2.Prescaler=0
TIM21.Channel-PWM\ Generation1\ No\ Output=TIM_CHANNEL_1
TIM21.IPParameters=Channel-PWM Generation1 No Output,Prescaler,Period,Pulse-PWM Generation1 No Output
TIM21.Period=1000
......
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