An analog to digital converter is a circuit that converts a continuous voltage value (analog) to a binary value (digital) that can be understood by a digital device which could then be used for digital computation. These ADC circuits can be found as an individual ADC ICs by themselves or embedded into a microcontroller. They’re called ADCs for short.
ADC Peripheral features: (STM32F429XX)
1. 12-bit, 10-bit, 8-bit or 6-bit configurable resolution.
2. Interrupt generation at the end of conversion, end of injected conversion, and in case of analog watchdog or
overrun events
3. Single and continuous conversion modes.
4. Scan mode for automatic conversion of channel 0 to channel x.
5. Data alignment with in-built data coherency.
6. Channel-wise programmable sampling time.
7. External trigger option with configurable polarity for both regular and injected conversion.
8. Dual/Triple mode (on devices with 2 ADCs or more).
9. Configurable DMA data storage in Dual/Triple ADC mode.
10. Configurable delay between conversions in Dual/Triple interleaved mode.
11. ADC conversion type (refer to the datasheets).
12. ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at slower speed.
13. ADC input range: VREF(minus) = VIN = VREF(plus).
14. DMA request generation during regular channel conversion.
How to use this driver
Initialize the ADC low level resources by implementing the HAL_ADC_MspInit():
a. Enable the ADC interface clock using __HAL_RCC_ADC_CLK_ENABLE()
b. ADC pins configuration
◦ Enable the clock for the ADC GPIOs using the following function:
__HAL_RCC_GPIOx_CLK_ENABLE()
◦ Configure these ADC pins in analog mode using HAL_GPIO_Init()
c. In case of using interrupts (e.g. HAL_ADC_Start_IT())
◦ Configure the ADC interrupt priority using HAL_NVIC_SetPriority()
◦ Enable the ADC IRQ handler using HAL_NVIC_EnableIRQ()
◦ In ADC IRQ handler, call HAL_ADC_IRQHandler()
d. In case of using DMA to control data transfer (e.g. HAL_ADC_Start_DMA())
◦ Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE()
◦ Configure and enable two DMA streams stream for managing data transfer from peripheral to
memory (output stream)
◦ Associate the initialized DMA handle to the CRYP DMA handle using __HAL_LINKDMA()
◦ Configure the priority and enable the NVIC for the transfer complete interrupt on the two DMA
Streams. The output stream should have higher priority than the input stream.
Execution of ADC conversions
1. ADC driver can be used among three modes: polling, interruption, transfer by DMA.
Polling mode IO operation
• Start the ADC peripheral using HAL_ADC_Start()
• Wait for end of conversion using HAL_ADC_PollForConversion(), at this stage user can specify the value of
timeout according to his end application
• To read the ADC converted values, use the HAL_ADC_GetValue() function.
• Stop the ADC peripheral using HAL_ADC_Stop()
For this blog we are going to use ADC in polling mode
Creating STM32 executable projects steps are available on this link , please follow steps 1 to 10 as per blog, here we will start from step 11. PA9 & PA10 Pin used for debugging purpose as UART Tx & UART Rx respectively.
11. Enable ADC Functionality
Go to System mode > Analog > ADC > Enable IN0 > keep all other settings as its
12.Enable DAC Functionality (only for debug purpose)
13. Press below icon for code generation
14.For code demonstration we will connect DAC Output to ADC Input , already we have configured UART1 for debugging purpose. Output of DAC is set at 2048 which will be input of ADC pin.
15. Sample Code
/* USER CODE BEGIN 2 */ HAL_DAC_Start(&hdac, DAC_CHANNEL_1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, dac_data); HAL_Delay(1000); //1000 ms delay HAL_ADC_Start(&hadc3); if(HAL_ADC_PollForConversion(&hadc3, 50) == HAL_OK) { adcvalue = HAL_ADC_GetValue(&hadc3); } } /* USER CODE END 3 */ }
16. Debug output (ADC in polling mode, debug output over UART Pins)
Software Tools:
- STM32CubeIDE
- STM32CubeMx
- Teraterm
Hardware Setup:
- STM32F429IDISCOVERY board
- Mini USB Cable
- Jumper wire
Conclusion:
Successfully demonstrated ADC functionality in polling mode with default configuration, for testing purpose we have used DAC & UART for debug.
References:
- STM32 HAL Library
- STM32 UM1718 document