STM32 ADC using Interrupt method

AS mentioned in the previous posts that there are many ways to write a program for ADC in STM32 like PollForConversioninterrupt and DMA. This post is going to cover the interrupt method. You can check out the PollForConversion and DMA methods too.

An interrupt is a signal sent to the CPU which indicates that a system event has occurred, which needs immediate attention. In case of an interrupt got triggered, CPU stops whatever it is doing and executes a function first. This function is a small piece of code also known as ISR(Interrupt Service Routine).

Interrupt is widely used in embedded systems to ensure that no CPU time is wasted ‘polling‘ the sensor for the data rather an interrupt is triggered whenever the data is available to be read from the sensor.

CubeMx Setup

PA5 is selected as GPIO Output and a LED is connected to it to show how the interrupt method doesn’t affect the normal working of the controller.

 Select the ADC1 channel 0

  • Here Resolution of adc is selected as 12 bit
  • Scan conversion mode is disabled because only one channel is selected
  • Continuous conversion mode is enabled as we want adc to work continuously
  • DMA continuous request is disabled as we are not using DMA here.

Go to NVIC Settings tab and check the “ADC global interrupt”.

Some Insight into the CODE

Let’s begin by writing a Conversion complete callback function. This function will be called whenever ADC finishes the conversion and data is ready to be read.

void HAL_ADC_ConvtCpltCallback (ADC_HandleTypeDef *hadc)
  value = HAL_ADC_GetValue (&hadc1);  // read the value of ADC

Whenever the conversion is complete, following function is executed. The value of ADC will be stored in the value variable. After the completion of the function, control will go back to main function.

In order to start the ADC in interrupt mode, we need to use

HAL_ADC_Start_IT (&hadc1);  // start ADC in interrupt mode


Check out the VIDEO Below

Notify of
Inline Feedbacks
View all comments