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 PollForConversion, interrupt 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.
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.
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
- Interface WS2812 with STM32 April 16, 2021
- PWM with DMA in STM32 April 8, 2021
- Control Stepper motor using Rotary Angle Sensor March 21, 2021
- External Interrupt using Registers March 17, 2021
- STM32F103 Clock Setup using Registers March 3, 2021
- STM32 I2C Configuration using Registers February 26, 2021