STM32 ADC to PWM

437 Views Asked by At

I have 4 ADC input channels and I want to convert the data (from potentiometer) to a timer 2 pwm output.

I have already configured the ADC and the PWM signals. Is there a way to check which adc channel triggered the interrupt?

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
  if(channel == 10)
  {
    TIM2->CCR1 = (int)((HAL_ADC_GetValue(&hadc1) / 4092.0) * 100.0);
  }
  else if(channel == 11)
  {
    TIM2->CCR2 = (int)((HAL_ADC_GetValue(&hadc1) / 4092.0) * 100.0);
  }
  ...
}

int main()
{
  ...
  TIM2->CCR1 = 50;  // set duty cycle channel 1
  TIM2->CCR2 = 50;  // set duty cycel channel 2 
  TIM2->CCR3 = 50;  // set duty cycel channel 3 
  TIM2->CCR4 = 50;  // set duty cycel channel 4
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1); // start pwm generation channel 1
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2); // start pwm generation channel 2
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3); // start pwm generation channel 3
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4); // start pwm generation channel 4
 
  HAL_ADCEx_Calibration_Start(&hadc1);
  HAL_ADC_Start_IT(&hadc1);
  ...
}

Is there a way to differenciate the channels in the callback?

0

There are 0 best solutions below