Skip to main content
Associate II
March 24, 2024
Question

GENERATE PWM using ADC values

  • March 24, 2024
  • 1 reply
  • 640 views

if (HAL_GPIO_ReadPin(USER_Btn_GPIO_Port, USER_Btn_Pin))
{
HAL_GPIO_WritePin(TIGGER_GPIO_Port, TIGGER_Pin, GPIO_PIN_SET);
HAL_ADC_Start_IT(&hadc1);
HAL_TIM_PWM_Start_DMA(&htim1, TIM_CHANNEL_1,(uint32_t*) pwmData1, 300);
HAL_GPIO_WritePin(TIGGER_GPIO_Port, TIGGER_Pin, GPIO_PIN_RESET);

}

 

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
digitalValue2 = (((HAL_ADC_GetValue(&hadc1)) * 3.3f) / 4095.0f);

HAL_ADC_Stop_IT(&hadc1);

if( (digitalValue2 >= 0.0) && (digitalValue2 < 1.0))
{

for (int j = 0; j <= 300; j++)
{
pwmData1[j] = 100;
pwmData1[299] = 0;
}
}
else if ((digitalValue2 >= 1.0) && (digitalValue2 < 2.0))
{

for (int j = 0; j <= 300; j++)
{
pwmData1[j] = 300;
pwmData1[299] = 0;
}
}
else if ((digitalValue2 >= 2.0) && (digitalValue2 < 3.4))
{

for (int j = 0; j <= 300; j++)
{
pwmData1[j] = 200;
pwmData1[299] = 0;
}
}
else
{

for (int j = 0; j <= 300; j++)
{
pwmData1[j] = 50;
pwmData1[299] = 0;
}

}

HAL_ADC_Start_IT(&hadc1);
}

i am using this function using PWM generate and ADC interrupt function

using this every time updating the width it is taking 2ms delay

    1 reply

    MM..1
    Chief III
    March 24, 2024

    Primary dont start stop ADC, secondary your for cycles is ugly. And for any type of two device data transfers you require sync it.