cancel
Showing results for 
Search instead for 
Did you mean: 

Problems with ADC interrupt

Keyshav Mor
Associate II
Posted on December 27, 2016 at 06:55

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 */
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

#stm32f103 #adc
10 REPLIES 10
Posted on December 30, 2016 at 09:08

Thank you so much for the straightforward help Tymoteusz . That is really helpful . I am using the regular ConvCpltCallback function and it is working fine. But I will try the call back functions suggested by you as well.

________________

Attachments :

image001.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hz0l&d=%2Fa%2F0X0000000bFE%2Ff9eLRk0V1IC_ZmeoTR4vgk_YHFpPxTz6lbuI1FByYPQ&asPdf=false