STM32 ADC.

Let's look at the features of the ADC in STM32.
  • 12-bit resolution
  • Reference voltage from 2.4 to 3.6 volts
  • Sampling rate up to 1MSPS
  • 18 channels (16 external and 2 internal: reference voltage and temperature sensor)
  • Interrupt at the end of regular and injected conversions
  • Interrupt from the window comparator (Analog watchdog)
  • DMA transfer for regular conversions
  • Single and continuous conversion
  • Scan mode over a specified list of channels
  • Self-calibration
  • Result alignment to the selected side
  • Conversion time – 12.5 cycles, sampling time – programmable

STM32 ADC.

Regular conversion mode.
In this mode a group of 1 to 16 channels is defined; they are processed sequentially, and the result is placed in a single 12-bit register, the assumption being that the data will be stored using DMA. I'd like to emphasize once again that you can't simply select one channel: you need to define a group and set the number of channels in it. Conversions can be continuous or periodic and can be triggered by a timer or an external interrupt.
 
Injected conversion mode.
An injected conversion sequence can include up to 4 channels, which have their own registers for storing the result. As soon as injected conversions start, the digitization of regular channels is suspended; then, when the injected ones are finished, the digitization of regular channels resumes. You can write a number into the offset registers that will be automatically subtracted from the conversion result; if the subtraction yields a negative number, the injected conversion register is sign-extended.
 
Window comparator.
In the special registers ADC_HTR and ADC_LTR we set the upper and lower limits of the conversion result; when they are exceeded, the AWD flag is set.
 
STM32 has 1 status register, SR, and two configuration registers, CR1, CR2, for working with the ADC.
 
SR(Status Register)
 
AWD(Analog watchdog flag) — the window comparator flag; it is set to 1 when the conversion result crosses ADC_HTR or ADC_LTR.
 
EOC (End of conversion) – the end of group conversion flag; it is set to 1 at the end of a regular or injected conversion.
 
JEOC(Injected channel end of conversion) — the end of injected conversion flag; it is set to 1 at the end of an injected conversion.
 
JSTRT(Injected channel Start flag) — the injected conversion start flag; it is set to 1 when an injected conversion starts.
 
STRT(Regular channel Start flag) — the regular conversion start flag; it is set to 1 when a regular conversion starts.
The flags described above are set by hardware, but they must be cleared by software!!!
 

ADC_CR1(ADC control register 1)
 
AWDCH[4:0] (Analog watchdog channel select bits) — selects the channel for the window comparator, provided that AWDSGL=1.
00000: Channel 0.
00001: Channel 1.
………
10001: Channel 17.
 
EOCIE(Interrupt enable for EOC) — enables the end of conversion interrupt: 1 — enable, 0 — disable.
 
AWDIE(Analog watchdog interrupt enable) — enables the window comparator interrupt: 1 — enable, 0 — disable.
 
JEOCIE(Interrupt enable for injected channels) — enables the end of injected conversion interrupt: 1 — enable, 0 — disable.
 
SCAN(Scan mode) — enables scanning of channels according to the list given in the ADC_SQRx registers: 1 — enable, 0 — disable.
 
AWDSGL(Enable the watchdog on a single channel in scan mode) — sets the window comparator scan mode: 0 — all channels, 1 — only the channel specified in AWDCH[4:0].
 
JAUTO(Automatic Injected Group conversion) — automatic conversion of the injected channels: 1 — enable, 0 — disable.
 
DISCEN(Discontinuous mode on regular channels) — discontinuous conversion mode for regular channels: 1 — enable, 0 — disable.
 
JDISCEN(Discontinuous mode on injected channels) — discontinuous conversion mode for injected channels: 1 — enable, 0 — disable.
 
DISCNUM[2:0](Discontinuous mode channel count) — the number of regular conversion channels.
000: 1 channel
001: 2 channels
…..
111: 8 channels
 
DUALMOD[3:0](Dual mode selection) — sets the mode in which the two ADCs work together.
 
JAWDEN(Analog watchdog enable on injected channels) — connects the window comparator to the injected channels: 1 — enable, 0 — disable.
 
AWDEN(Analog watchdog enable on regular channels) — connects the window comparator to the regular channels: 1 — enable, 0 — disable.
 
 
ADC_CR2(ADC control register 2)
 
ADON(A/D converter ON / OFF) — ADC control: 1 — enables the ADC, 0 — stops the conversion/calibration and puts the ADC into power-down mode.
 
CONT(Continuous conversion) — conversion mode: 0 — single, 1 — continuous.
 
CAL(A/D Calibration) — starts calibration: 1 — start calibration, 0 — set by hardware when calibration is complete. Must be done before enabling the ADC, every time after power-up.
 
RSTCAL(Reset calibration) — resets the calibration values: 1 — reset, 0 — set when the reset is complete.
 
DMA(Direct memory access mode) — enables DMA: 1 – enable, 0 — disable.
 
ALIGN(Data alignment) — data alignment: 1 — left, 0 — right. It's worth looking at the picture here, since with injected conversions a sign also appears; in the picture the sign cells are marked SEXT.
 
STM32 ADC.

 
JEXTSEL[2:0](External event select for injected group) – selects the external trigger source for the injected channels.
STM32 ADC.

 
JEXTTRIG(External trigger conversion mode for injected channels) — enables triggering of injected conversion by an external signal: 1 — enable, 0 — disable.
 
EXTSEL[2:0](External event select for regular group) — selects the external trigger source for the regular channels.
STM32 ADC.

 
EXTTRIG(External trigger conversion mode for regular channels) — enables triggering of regular conversion by an external signal: 1 — enable, 0 — disable.
 
JSWSTART(Start conversion of injected channels) — starts conversion of the injected channels: 1 — start conversion; it is cleared by hardware after the conversion starts. Also, to start it, JSWSTART must be selected as the trigger source in JEXTSEL[2:0].
 
SWSTART(Start conversion of regular channels) — starts conversion of the regular channels: 1 — start conversion; it is cleared by hardware after the conversion starts. Also, to start it, SWSTART must be selected as the trigger source in EXTSEL[2:0].
 
TSVREFE(Temperature sensor and VREFINT enable) — enables the temperature sensor and the internal voltage reference: 1 — enable, 0 — disable. Present only in ADC1.
 
 
SMPR[2:1](Channel x Sample time selection).
These registers set the number of cycles between samples, separately for each channel.
SMPR1 covers channels 10 to 17.
 
STM32 ADC.

SMPR2 covers channels 0 to 9.
STM32 ADC.

The conversion takes 12.5 cycles; to find the sampling period, add the SMPx[2:0] value to the conversion time.
STM32 ADC.

T = sampling time + 12.5 cycles
So the minimum sampling period is
T = 12.5 + 1.5 = 14 cycles
The maximum frequency at which the ADC can be clocked is 14MHz, so the minimum sampling period is 1uS.
 
JOFRx(x=1..4)
Offset registers for the injected channels; you can write a value into these registers that will be subtracted from the result at the end of the conversion.
 
HTR and LTR
The upper and lower limits of the window comparator.
 
SQRx[3:1] – this register can be roughly divided into two parts: the L[3:0] bits and the SQx[4:0] bits.
STM32 ADC.

STM32 ADC.

STM32 ADC.

The number of the SQx section determines the position in the regular conversion group, and the number written into the section determines the channel number.
You can find out which pin corresponds to which channel in the datasheet, in the Pinouts and pin description section.
STM32 ADC.

And the L[3:0] bits in the SQR1 register set the length of the regular sequence
0000: 1 conversion
0001: 2 conversions
….......
1111: 16 conversions

For example
L = 0010
SQ1=00000, SQ2=00100, SQ3=00010
means that the sequence length is 3 and it will be executed in the following order: channel 1, channel 5, channel 3.
 
JSQR — these registers set the length of the injected sequence and the conversion order. The JL[1:0] bits set the sequence length
00: 1 conversion
01: 2 conversions
10: 3 conversions
11: 4 conversions

The number of the JSQx section determines the position in the injected conversion group, and the number written into the section determines the channel number.
You can find out which pin corresponds to which channel in the datasheet, in the Pinouts and pin description section.
If the number of conversions is less than 4, for example 3, then the conversion order for the setting
ADC_JSQR[21:0] = 10 00011 00011 00111 00010
will be channel 7, 3, 3, not 2, 7, 3.

JDR(x = 1...4)
Injected conversion data registers.
 
DR
Regular conversion data register.

Below is an example of code for starting the ADC.

#include "stm32f10x.h"            

unsigned temp;

void ADC1_2_IRQHandler(void)
{
       if(ADC1->SR & ADC_SR_EOC)
	{
	     //write the result to a variable 
             temp =ADC1->DR;
	}
        //clear all flags in the status register
	ADC1->SR=0;
}
int main(void)
{
	//enable port A clocking
	RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
        //configure the ADC pin in analog input mode
	GPIOA->CRL &= ~GPIO_CRL_CNF1;
	//since the ADC clock frequency must not exceed 14MHz
	RCC->CFGR |= RCC_CFGR_ADCPRE_DIV8;
	//enable ADC clocking
	RCC->APB2ENR |= RCC_APB2ENR_ADC1EN ;
        //someone in the comments pointed out that revision 17 says the ADC must be turned on before calibration
        ADC1->CR2 = | ADC_CR2_ADON;
        //start calibration and wait until it completes; this doesn't work in the simulator, but on hardware it's mandatory
       /* ADC1->CR2 |= ADC_CR2_CAL; 
         while (ADC1->CR2 & ADC_CR2_CAL)*/
	//enable the ADC interrupt
	NVIC->ISER[0] |= NVIC_ISER_SETENA_18;
	//7.5 cycles between samples for the first channel
	ADC1->SMPR2 = ADC_SMPR2_SMP1_0; 
	//enable end-of-conversion interrupts
	ADC1->CR1 = ADC_CR1_EOCIE;
	//enable triggering from SWSTART, enable the ADC, enable triggering by external signals, continuous conversion
	ADC1->CR2 = ADC_CR2_EXTSEL | ADC_CR2_EXTTRIG | ADC_CR2_CONT ;
	//sequence length is 1, first channel
	ADC1->SQR1 = 0x00000000;
	ADC1->SQR2 = 0x00000000;
	ADC1->SQR3 = 0x00000001;
	//start the ADC
	ADC1->CR2 |= ADC_CR2_SWSTART;
	while(1)
	{
		/*stop the project, type include sine.ini in the command line
		     then type ADC1_IN1_Sine ()*/
	}
}


Clearly, you can't check whether the project works in the simulator, since nothing is fed to the ADC input. The question also arises of what the commented-out lines in the while loop are. You can test the project in the simulator, and the commented-out lines remind you how to do it.

First, create a text document, write the following code into it and save it as sine.ini

SIGNAL void ADC1_IN1_Sine (void)  {
  float volts;        // peak-to-peak voltage
  float frequency;    // output frequency in Hz
  float offset;       // voltage offset
  float duration;     // duration in Seconds
  float val;
  long  i, end;
   
  volts     = 1.5;
  offset    = 1.5;
  frequency = 50;
  duration  = 0.01;

  printf ("Sine Wave Signal on AD Channel 1.\\n");

  end = (duration * 1000000000);
  for (i = 0 ; i < end; i++)  {
    val = __sin (2 * 3.1415926 * frequency * (((float) STATES) / STCLK));
    ADC1_IN1 = (val * volts) + offset;
    swatch (0.00001);                // in 10 uSec steps
  }
}
DEFINE BUTTON "ADC1_IN1 Sin","ADC1_IN1_Sine()"

Move sine.ini to the project folder; after starting debugging, do what is written in the commented-out line, and as a result we get a sine wave at the ADC input, so now the ADC data will change. You can enlarge the picture by clicking on it.
STM32 ADC.