STM32 ADC USING HAL
Most of us know the importance of ADC and the fact that simpler it is to use the ADC in any device, the more easier it is to make any project. Most of the sensors use ADC for data transmission to the microcontroller and that’s why ADC plays an important role in any embedded system design.
Earlier microcontrollers, such as 8051, didn’t had ADC built in to the microcontroller and so there were external ADC connectors, making design more complex. But with the introduction of AVR and PIC, things changed and users started using these instead of 8051. ADC in STM32 is very advanced and very complex. But we will start from basics and take one step at a time. First let’s look into some features of ADC:
- 12-bit, 10-bit, 8-bit or 6-bit configurable resolution
- Interrupt generation at the end of conversion, end of injected conversion, and in case of analog watchdog or overrun events
- Single and continuous conversion modes
- Scan mode for automatic conversion of channel 0 to channel ‘n’
- Data alignment with in-built data coherency
- Channel-wise programmable sampling time
- External trigger option with configurable polarity for both regular and injected conversions
- Discontinuous mode
- ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at slower speed
- ADC input range from Vref- to Vref+.
- DMA request generation during regular channel conversion
MODES used in ADC
- Continuous Conversion mode. If enabled, ADC will Continuously sample and convert. If disabled, only one sampling and conversion will be performed and than A/D will stop.
- Scan Conversion mode. Configures the sequencer of groups. If it is disabled, conversion is performed in a single conversion mode (the one defined in rank 1). If enabled, conversions are performed in sequence mode up the rank.
- Discontinuous Conversion mode. Specifies whether the conversions sequence of regular group is performed in Complete-sequence/Discontinuous-sequence (main sequence subdivided in successive parts).Discontinuous mode is used only if sequencer is enabled (parameter ‘ScanConvMode’). If sequencer is disabled, this parameter is discarded.
ADC needs the trigger in order to start conversion. Trigger signal are of two types:-
- Software Trigger. This will start A/D conversion from the code.
- Hardware Trigger. This will start A/D conversion in case of any hardware events eg- timer event.
There are many ways to program ADC in STM devices. We are going to cover all of them, starting with the simplest, PollForConversion method.
Select the ADC CHANNEL
Go to the configure tab and select ADC. Now make sure your configuration is as in the picture below
- Here Resolution of adc is selected as 12 bit
- Scan conversion mode is disabled because only one channel is selected and sequence is not needed at this time.
- Continuous conversion mode is enabled as we want adc to work continuously.
- DMA continuous request is disabled as we are not using DMA here.
Some Insight into the CODE
- Here we START the ADC
- Poll for the conversion to finish
- Get the ADC value
- Finally STOP the ADC
To test this I am using a IR motion sensor, which works with ADC. I am not going to go in depth of how this sensor works. You can consider it as a potentiometer or a variable resistor to test ADC.
So if the sensor does not detect any obstacle, it’s resistance will be 100%, which is 4095 in our 12 bit ADC mode.
With something in front of it, the second LED glows and the value in the debugger decreases.
NOTE:- As the pollforconversion function is in the while loop, this procedure will continue forever and microcontroller can not perform any other function. To avoid this, we will use interrupt in the upcoming tutorial. Also I will use the STM-studio to watch runtime variables.
. . . . . DOWNLOAD . . . . .
You can buy me acontrollerstech
coffeeSensor by clicking DONATE OR Just click DOWNLOAD to download the code
- WavePlayer using STM32 Discovery January 16, 2021
- How to use SPI in LPC2148 December 26, 2020
- STM32 ADC MULTI CHANNEL WITHOUT DMA December 24, 2020
- Send and Receive data to PC without UART (STM32 USB COM) December 10, 2020
- STM32 GPIO INPUT Configuration December 4, 2020
- STM32 GPIO OUTPUT Config using REGISTERS November 24, 2020