ADC in STM32F103C8T6 is 12-bit and a successive approximation analog-to-digital converter.
It has up to 18 multiplexed channels allowing it measure signals from sixteen external and two internal sources. The result of the ADC is stored in a left-aligned or right-aligned 16-bit data register.
ADC converter supports several conversion modes:
- Single mode, which converts only one channel, in single-shot or continuous mode.
- Scan mode, which converts a complete set of pre-defined programmed input channels, in single-shot or continuous mode.
- Discontinuous mode, converts only a single channel at each trigger signal from the list of pre-defined programmed input channels.
- In regular mode, ADC read channels sequentially in a loop and convert them regularly.
- In injected mode conversion is triggered by an external event or by software. An injected conversion has higher priority in comparison to a regular conversion and thus interrupts the regular conversions.

Temperature sensor:
The temperature sensor is connected to channel ADCx_IN16 and the internal reference voltage VREFINT is connected to ADCx_IN17.
The recommended sampling time for the temperature sensor is 17.1 µs.
The TSVREFE bit must be set to enable both internal channels: ADCx_IN16 (temperature sensor) and ADCx_IN17 (VREFINT) conversion.
Let's use ADC 1 to convert the analog value to digital from internal temperature sensor.
Program:
For more details on RCC configuration, see previous post.
Steps:
4. Single mode one-shot conversion: CONT bit in CR2 register is 0
- Conversion in regular channel
- Data alignment: ALIGN bit in CR2 register is 0 - Right alignment
5. Assign the channels you want to convert in the regular sequence register and the number of conversion in L[3:0] in ADC_SQR1 register
6. Sampling Time: Configure RCC and ADC prescaler to get desired time
ADC samples the input voltage for a number of ADC_CLK cycles which can be modified using the SMP[2:0] bits in the ADC_SMPR1 and ADC_SMPR2 registers.
Each channel can be sampled with a different sample time.
We are going to modify ADC_16 channel sampling time to which temperature sensor is connected.
The total conversion time is calculated as follows:
Tconv = Sampling time + 12.5 cycles
With an ADCCLK = 4 MHz and a sampling time of 55.5 cycles to get sampling time of 17.1 µs for temperature sensor:
Tconv = 55.5 + 12.5 = 68 cycles
68 * 1 / 4 MHz = 17 µs (approx.)
7. ADC on-off control:
Conversion starts when ADON bit is set for a second time by software after ADC power-up time (tSTAB). The conversion can be stopped, and the ADC put in power down mode by resetting the ADON bit.
In a regular channel:
– The converted data is stored in the 16-bit ADC_DR register
– The EOC (End Of Conversion) flag is set
– and an interrupt is generated if the EOCIE is set.
ADC 1 and 2 have same interrupt handler - global interrupt
Once we get the data, we could convert the digital value to temperature (in this case) or to any other sensor physical value
11. If more than one channel conversion is done, in order to avoid data loss, use DMA.
12. ADC watchdog can be used to sense if the measured voltage gets above or below high / low voltage threshold.
13. To Print output on a Serial Terminal, use UART. For UART in STM32, see this post
For DMA in STM32, see this post
For RTOS in STM32, see this post
Connecting Bluetooth with STM32 - post
Comments
Post a Comment