Skip to main content
Visitor II
October 3, 2024
Solved

STM32F103C8T6 - Stop and start timers timebase inside an interruption

  • October 3, 2024
  • 1 reply
  • 1163 views

Hi! I'm working in an application where I want to start a series of ADC conversions every 10 seconds, take a hundred samples and stop the conversions. The way I thought of it is to use a timer (timer 1 in my case) to measure the 10 seconds and use another timer (timer 3) to trigger the ADC conversions. Once the 100 conversions are made, the timer 3 stops until the timer 1 counts to 10 seconds again.
The problem that I have is that the timer 3 does the cycle only one time and after disabling the time base inside the ADC callback function I can't start it again from the TIM1 callback function. This works fine only if I don't use HAL_TIM_Base_Stop(&htim3).
Am I doing something wrong or this method is not possible?

 

 

 

 

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
	HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_9);
	HAL_TIM_Base_Start(&htim3);
}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, 1);
	counter++;
	sumVoltage += adcResults[0];
	sumBattCurrent += adcResults[1];

	if (counter == 100){
		voltage = sumVoltage / 100.0 * (3.285 / 4095.0);
		battCurrent = ((sumBattCurrent / 100.0) - 3080.0) / 61.0;

		counter = 0;
		sumVoltage = 0;
		sumBattCurrent = 0;
		HAL_TIM_Base_Stop(&htim3);
	}
	
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, 0);
}

static void MX_ADC1_Init(void)
{
 ADC_ChannelConfTypeDef sConfig = {0};

 hadc1.Instance = ADC1;
 hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
 hadc1.Init.ContinuousConvMode = DISABLE;
 hadc1.Init.DiscontinuousConvMode = DISABLE;
 hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T3_TRGO;
 hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
 hadc1.Init.NbrOfConversion = 3;
 if (HAL_ADC_Init(&hadc1) != HAL_OK)
 {
 Error_Handler();
 }

 sConfig.Channel = ADC_CHANNEL_9;
 sConfig.Rank = ADC_REGULAR_RANK_1;
 sConfig.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
 {
 Error_Handler();
 }

 sConfig.Channel = ADC_CHANNEL_8;
 sConfig.Rank = ADC_REGULAR_RANK_2;
 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
 {
 Error_Handler();
 }
 sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
 sConfig.Rank = ADC_REGULAR_RANK_3;
 sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
 {
 Error_Handler();
 }
}

static void MX_TIM1_Init(void)
{
 TIM_ClockConfigTypeDef sClockSourceConfig = {0};
 TIM_MasterConfigTypeDef sMasterConfig = {0};

 htim1.Instance = TIM1;
 htim1.Init.Prescaler = 1835;
 htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim1.Init.Period = 65358;
 htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim1.Init.RepetitionCounter = 0;
 htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
 if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
 {
 Error_Handler();
 }
 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
 if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
 {
 Error_Handler();
 }
 sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
 {
 Error_Handler();
 }
}
static void MX_TIM3_Init(void)
{
 TIM_ClockConfigTypeDef sClockSourceConfig = {0};
 TIM_MasterConfigTypeDef sMasterConfig = {0};

 htim3.Instance = TIM3;
 htim3.Init.Prescaler = 0;
 htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim3.Init.Period = 419;
 htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
 if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
 {
 Error_Handler();
 }
 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
 if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
 {
 Error_Handler();
 }
 sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
 {
 Error_Handler();
 }
}

 

 

 

 

 

    This topic has been closed for replies.
    Best answer by waclawek.jan

    > I can't start it again

    What is it that you can't start? TIM3 or the ADC conversions? What are exactly the symptoms? What are TIM3 registers content, mainly TIM3_CR1.CEN?

    JW

    1 reply

    Super User
    October 4, 2024

    > I can't start it again

    What is it that you can't start? TIM3 or the ADC conversions? What are exactly the symptoms? What are TIM3 registers content, mainly TIM3_CR1.CEN?

    JW

    EMart.10Author
    Visitor II
    October 4, 2024

    Thanks! I'm new to this and don't know much about the registers and low level stuff, with your reply I managed to start the timer 3 and trigger the conversions again after changing the CEN bit in CR1 with TIM3 -> CR1 |= (1 << 0).