Library 06- AD Converter on STM32F4

In this tutorial I will talk about ADC (Analog to Digital Converter). AD converts analog voltage to digital number, that can be used in microcontrollers.

STM32F4xx MCUs have up to 3 ADCs of which every has 19 channels.

  • 16 external channels, connected to IO pins
  • 3 internal channels
    • Vbat
      • Voltage on battery pin for RTC
    • Temp sensor
      • unusable for measure temp,only for measure difference in temperature because it can fail up to 45°C
    • Vref
      • Voltage reference for ADC

Below is table with and channels connected to physical pins.

Library

Features

  • Measure analog value with pooling mode
  • Use all possible ADC’s with all possible pins
  • Measure Vbat pin for RTC battery

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx ADC
  • TM
    • TM GPIO
    • defines.h
Channel ADC1 ADC2 ADC3
APB 2 2 2
ADC Channel 0 PA0 PA0 PA0
ADC Channel 1 PA1 PA1 PA1
ADC Channel 2 PA2 PA2 PA2
ADC Channel 3 PA3 PA3 PA3
ADC Channel 4 PA4 PA4 PF6
ADC Channel 5 PA5 PA5 PF7
ADC Channel 6 PA6 PA6 PF8
ADC Channel 7 PA7 PA7 PF9
ADC Channel 8 PB0 PB0 PF10
ADC Channel 9 PB1 PB1 PF3
ADC Channel 10 PC0 PC0 PC0
ADC Channel 11 PC1 PC1 PC1
ADC Channel 12 PC2 PC2 PC2
ADC Channel 13 PC3 PC3 PC3
ADC Channel 14 PC4 PC4 PF4
ADC Channel 15 PC5 PC5 PF5

My library ADCx:

  • Resolution 12bits
  • Prescaler set to 4: APB2 clock / 4
  • Single mode,
  • Independent mode,
  • 8 clock cycles between 2 reads

Functions and enumerations

Example 1

Example 2

  • Measure voltage on Vbat pin
  • Result is in mV

Projects are available on Github, download library below.

tilz0R

Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...