cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F373 firmware to continually update SDADC gain in a loop

JLee.161
Associate III

Hi, I'm using STM32F373 for a biosignal measurement project, and I need some help writing firmware to automatically update the SDADC channel gain during execution.

For example, I'm sampling at 100Hz, and after every 100 samples, i want to change the gain based on the minimum and the maximum values during that period.

---------------------

So, my code to determine the maximum and minimum values is:

uint16_t ECG = 0;

uint16_t ECG_channel_max = 0;

uint16_t ECG_channel_min = 0xFFFF;

if (ECG > ECG_channel_max)

{

ECG_channel_max = ECG;

}

if (ECG < ECG_channel_min)

{

ECG_channel_min = ECG;

}

---------------------

The SDADC_Init code to setup the SDADC gain looks something like this:

ConfParamStruct.InputMode = SDADC_INPUT_MODE_SE_ZERO_REFERENCE;

 ConfParamStruct.Gain = SDADC_GAIN_4;

 ConfParamStruct.CommonMode = SDADC_COMMON_MODE_VSSA;

 ConfParamStruct.Offset = 0;

 if (HAL_SDADC_PrepareChannelConfig(&hsdadc1, SDADC_CONF_INDEX_0, &ConfParamStruct) != HAL_OK)

 {

   Error_Handler();

 }

 if (HAL_SDADC_AssociateChannelConfig(&hsdadc1, SDADC_CHANNEL_6, SDADC_CONF_INDEX_0) != HAL_OK)

 {

   Error_Handler();

 }

---------------------

Then, I'm trying to use a custom function after every 100 samples as followed;

static void updateGain(void)

{

SDADC_ConfParamTypeDef ConfParamStruct = {0};

if (ECG_channel_max < 0xFFFF)

{

if (ECG_channel_max-ECG_channel_min < 0x4FFF) // about 1/3 of full scale

{

ConfParamStruct.Gain += 1;

}

}

if (ECG_channel_max >= 0xF000)

{

ConfParamStruct.Gain -= 1;

}

if (ECG_channel_max-ECG_channel_min >= 0xA000) // about 2/3 of full scale

{

ConfParamStruct.Gain -= 1;

}

if (HAL_SDADC_PrepareChannelConfig(&hsdadc1, SDADC_CONF_INDEX_0, &ConfParamStruct) != HAL_OK)

{

Error_Handler();

}

if (HAL_SDADC_AssociateChannelConfig(&hsdadc1, SDADC_CHANNEL_6, SDADC_CONF_INDEX_0) != HAL_OK)

  {

   Error_Handler();

 }

// reset the min and max values

ECG_channel_max = 0;

ECG_channel_min = 0xFFFF;

}

The thing is, I want to use something like this, but 'ConfParamStruct.Gain' cannot be incremented as seen above. I could use alot of if-statements to change the gain (say from SDADC_GAIN_4 to SDADC_GAIN_8), but I wanted a more elegant solution.

Does anyone have a good idea on how to accomplish this? Thank you in advance!

1 REPLY 1
TDK
Guru

Gains can only be changed by a factor of 2 for each step. So implement rules based on that:

If ECG_channel_max < 40% of 0xFFFF --> increase gain by 2x

if ECG_channel_max > 90% of 0xFFFF --> decrease gain by 2x

I don't think ECG_channel_min should be part of your calculation.

If you feel a post has answered your question, please click "Accept as Solution".