AnsweredAssumed Answered

Problems with ADC interrupt

Question asked by Mor.Keyshav on Dec 27, 2016
Latest reply on Dec 30, 2016 by Mor.Keyshav

I am trying to generate an instantaneous PWM signal using ADC Single channel interrupt on stm32f103rbt6 nucleo board. I want to vary the speed of the dc motor using the instantaneous adc interrupt. But I am getting latency in my code, which is right now trying to just start and stop the motor on adc interrupt. Can anyone please help me? Here is the code. There is a lot of latency in the code.

 

/* Private variables ---------------------------------------------------------*/
uint32_t ADC_raw;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc1)
{
if (__HAL_ADC_GET_FLAG(hadc1, ADC_FLAG_EOC))
{
ADC_raw = HAL_ADC_GetValue(hadc1);
HAL_ADC_Start_IT(hadc1);
}
}
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
HAL_ADC_Start_IT((ADC_HandleTypeDef*) &hadc1);
/* USER CODE END 2 */
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_4);
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
/* USER CODE END WHILE */
HAL_Delay(71);
/* USER CODE BEGIN 3 */
HAL_ADC_Start_IT((ADC_HandleTypeDef*) &hadc1);
}
/* USER CODE END 3 */
}

void ADC1_2_IRQHandler(void)
{
/* USER CODE BEGIN ADC1_2_IRQn 0 */
uint32_t adcValue;
adcValue=HAL_ADC_GetValue(&hadc1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_SET);
if(adcValue>1000)
{
uint32_t motor=0;
for(motor=0;motor<=1000;motor++)
{
__HAL_TIM_SetCompare(&htim1,TIM_CHANNEL_4,motor);
HAL_Delay(10);
// HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_4);
}
//HAL_Delay(500);
}

/* USER CODE END ADC1_2_IRQn 0 */
HAL_ADC_IRQHandler(&hadc1);
/* USER CODE BEGIN ADC1_2_IRQn 1 */
/* USER CODE END ADC1_2_IRQn 1 */
}
void ADC1_2_IRQHandler2(void)
{
uint32_t adcValue;
adcValue=HAL_ADC_GetValue(&hadc1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_SET);
if(adcValue<1000)
{
uint32_t motor=0;
for(motor=0;motor<=1000;motor=motor+100)
{
__HAL_TIM_SetCompare(&htim1,TIM_CHANNEL_4,motor);
HAL_Delay(10);
}
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_RESET);
}
HAL_ADC_IRQHandler(&hadc1);
}
/**
* @brief This function handles EXTI line[15:10] interrupts.
*/

void EXTI15_10_IRQHandler(void)
{
/* USER CODE BEGIN EXTI15_10_IRQn 0 */
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_4);
uint32_t motor=0;
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_SET);
for(motor=0;motor<=1000;motor++)
{
__HAL_TIM_SetCompare(&htim1,TIM_CHANNEL_4,motor);
HAL_Delay(10);
// HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_4);
}
//HAL_Delay(500);
HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_4);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_RESET);
/* USER CODE END EXTI15_10_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13);
/* USER CODE BEGIN EXTI15_10_IRQn 1 */
/* USER CODE END EXTI15_10_IRQn 1 */
}

Outcomes