Working with ADC and STM32 part1: Single Channel Single Conversion

ADC Symbol

In this guide, we shall learn how to use the ADC of the STM32 for single channel Single Conversion mode using only registers.

The ADC is commonly used to measure the voltage from a sensor for example temperature sensor such as LM35 which can provide voltage proportional to the temperature.

This guide will cover the following

  • What is the ADC
  • Required Parts
  • Schematic
  • Code
  • Results

1.1 What is the ADC

In electronics, an analog-to-digital converter (ADCA/D, or A-to-D) is a system that converts an analog signal, such as a sound picked up by a microphone or light entering a digital camera, into a digital signal that can be read by STM32.

ADCs can vary greatly between microcontroller. The ADC on the STM32F411 is a 12-bit ADC meaning it has the ability to detect 4096(2^12) discrete analog levels (which is also called Resolution). Some microcontrollers have 8-bit ADCs (2^8 = 256 discrete levels) and some have 16-bit ADCs (2^16 = 65,536 discrete levels).

The way an ADC works is fairly complex. There are a few different ways to achieve this feat (see Wikipedia for a list), but one of the most common technique uses the analog voltage to charge up an internal capacitor and then measure the time it takes to discharge across an internal resistor. The microcontroller monitors the number of clock cycles that pass before the capacitor is discharged. This number of cycles is the number that is returned once the ADC is complete. Other methods used called Successive-approximation ADC which you can read about it in details from this wikipedia page (Link)

For more information about how the adc of STM32F4 works, refer to the user manual (RM0383).

1.2 Relating ADC Value to Voltage

The ADC reports a ratio metric value. This means that the ADC assumes 3.3V is 4095 and anything less than 3.3V will be a ratio between 3.3V and 4095.

For example if the sensor has output voltage of 1.66V hence the ADC output will be (4095/3.3)*1.66=2059.

1.3 ADC registers needed in this guid

The registers of ADC in STM32F411 are:

  • ADC_SR (Status Register).
  • ADC_CR1 (Control Register 1).
  • ADC_CR2 (Control Register 2).
  • ADC_SQ3 (Sequence 3).
  • ADC_DR (Data Register).

2 Required Parts

You will need the following parts:

  • STM32F411 Nucleo-64
  • BreadBoard
  • 1KOhm Potentiometer
  • Hookup wires (male-male)

We shall use Keil uVision (MDK-ARM) IDE (Link)

3 Schematic

Connection Diagram
actual connection

4.1 Code

//read the analog voltage on PA1

#include "stm32f4xx.h"                  // Device header
#include "stdint.h"
void ADC_init(void);
void delayMs(int delay);
uint16_t adcValue;

int main(void){
ADC_init();

	
while(1){
ADC1->CR2|=ADC_CR2_SWSTART; //start adc conversion
//wait for the end of conversion 
while(!((ADC1->SR)&ADC_SR_EOC)){;}
adcValue=ADC1->DR;
delayMs(10);
}

}
void ADC_init(void){
RCC->AHB1ENR|=RCC_AHB1ENR_GPIOAEN; //enable gpio a clock
RCC->APB2ENR|=RCC_APB2ENR_ADC1EN; //enable adc clock
GPIOA->MODER|=GPIO_MODER_MODER1;  //set the PA1 to analog mode
ADC1->CR2=0; //disable the adc 	
ADC1->SQR3|=1; 
ADC1->CR2|=1; //enable the adc 	
}

void delayMs(int delay)
{
int i;
for(; delay>0 ;delay--)
{
for(i =0; i<3195;i++);
}

}

4.2 Code explanation

We are starting by including both the MCU header file and stdint.h

#include "stm32f4xx.h"                  // Device header
#include "stdint.h"

We declare the prototype of the used function.

void ADC_init(void);
void delayMs(int delay);

then declaring the variable which will hold the ADC results as unsigned int with length of 16-bit (uint16_t) since the ADC is only 12-bit

uint16_t adcValue;

int the main function we are initializing the ADC

int main(void){
ADC_init();

then in the while loop we are start the conversion by setting bit named SWSTART as seen in the register

SWSTART location in ADC_CR2
ADC1->CR2|=ADC_CR2_SWSTART; //start adc conversion

then waiting for the end of conversion which is in ADC_SR register, store the result in adcValue and delay about 10 milliseconds between each read

EOC position in ADC_SR

while(!((ADC1->SR)&ADC_SR_EOC)){;}
adcValue=ADC1->DR;
delayMs(10);
}

}

To initialize the ADC, the following is needed

  • enable clock access to GPIOA and ADC1
  • Set GPIO_A1 as Analog Input.
  • Disable the ADC before configure the ADC
  • Set the PA1 as first in the sequence in SQ3 register
  • Enable the ADC (bit0) (Refer to ADC_CR2 register from above)
Sequence of Conversion

5 Results

After compiling the code, uploading the code to mcu, opening a debug session in keil uVision

Rotate the potentiometer and observe the adcValue in watch window

Potentiometer rotate Counter clockwise
Potentiometer rotate clockwise

Add Comment

Your email address will not be published. Required fields are marked *