Stm32f3 Adc
Stm32f3 Adc
Stm32f3 Adc
CUAUHTÉMOC CARBAJAL 1
18/10/2013
References
• http://www.embedds.com/introducing-to-stm32-adc-programming-part1/
• http://controlsoft.nmmu.ac.za/STM32F0-Discovery-Board/Example-
programs/Analog
• http://mipsandchips.blogspot.mx/
• STM32F3 Microcontroller Reference Manual
2
ADC PRINCIPLES
3
Basics of A/D Conversion (1 of 2)
• Many embedded systems need to deal with
nonelectric quantities: weight, humidity, pressure,
weight, mass or airflow, temperature, light intensity,
and speed.
• These nonelectric quantities are analog in nature.
• Analog quantities must be converted into digital
format so that they can be processed by the
computer.
• An A/D converter can only deal with electric
voltage.
4
Basics of A/D Conversion (2 of 2)
• Any nonelectric quantity must be converted into an electric
quantity using a certain type of transducer.
• A transducer converts a nonelectric quantity into an electric
quantity.
• The output of a transducer may not be in a suitable range for A/D
conversion.
• A signal conditioning circuit is needed to shift and scale the
transducer output to a range suitable for A/D conversion.
• A lowpass/bandpass filter is required to remove unwanted signals
outside the bandwidth of interest and prevent aliasing.
temperature
pressure
signal Digital
voltage voltage A/D value
light Transducer conditioning Computer
weight circuit converter
airflow
Such as a (optional)
humidity sensor,
. load cell,
. photocall, or
. thermocouple
. The
Figure A/DThe
12.1 conversion process
A/D conversion process
. 5
http://www.analog.com/static/imported-files/tutorials/MT-002.pdf
Analog Voltage and
Digital Code Characteristic (1 of 2)
Digital Code
characteristic as shown.
• An A/D converter with
characteristic as shown
would need infinite
number of bits to
represent the A/D Voltage
6
Analog Voltage and
Digital Code Characteristic (2 of 2)
outputcode
• The area above and below the
dotted line is called quantization
error.
• Using n-bit to represent A/D
conversion has an average error
of VDD/2n+1.
• A real A/D converter output V DD/2n V DD
voltage
• Dominant:
• Delta-Sigma
Industrial Measurement, voice-band, audio
• Successive Approximation Data acquisition
• Pipeline High speed: instrumentation
video, IF sampling, software
radio, etc.
• Flash
• Other:
• Tracking
• Stair Step Ramp
• Single and Dual Slope
http://www.analog.com/library/analogdialogue/archives/39-06/architecture.html
http://www.maximintegrated.com/app-notes/index.mvp/id/1041
8
A/D Successive Approximation (1 of 3)
9
Successive Approximation Method (2 of 3)
• Approximates the analog
signal in n steps.
• The first step initializes the
SAR register to 0.
• Perform a series of guessing
steps that starts from the
most significant bit and
proceeding toward the
least significant bit.
• For every bit in SAR register
guess it to be 1.
• Converts the value of the
SAR register to analog
voltage.
• Compares the D/A output
with the analog input and
clears the bit to 0 if the D/A
output is larger.
10
ILLUSTRATION OF FOUR-BIT SAC OPERATION USING A DAC STEP SIZE OF 1 V AND VA = 10.4 V.
11
A/D Successive Approximations
12
Optimal Voltage Range for A/D Conversion
• Needs a low reference voltage (VRL) and a high reference
voltage (VRH) in performing A/D conversion.
• VRL is often set to ground level.
• VRH is often set to VDD.
• Most A/D converter are ratiometric
• A 0 V (or VRL) analog input is converted to the digital code of 0.
• A VDD (or VRH) analog input is converted to the digital code of 2n – 1.
• A VK input will be converted to the digital code k = VK 2n VDD.
• The A/D conversion result will be most accurate if the value of
analog signal covers the whole voltage range from VRL to VRH.
• The A/D conversion result k can be translated back to an
analog voltage VK by the following equation:
13
Example
Solution
R2
R1 = 1 + R2/R1
A voltage
Figure scalerscaler
12.6 A voltage
Example
Choose appropriate values of R1 and R2 in to scale a voltage in the range of
0~200mV to 0~5V.
Solution
AV = 1 + R2/R1 = 5V / 200mV = 25
R2/R1 = 24
R0 +12 V
VIN - R1 +12 V
741 -
+ VM 741 VOUT
+
- 12 V R2
- 12 V
VM = - VIN V1 Rf
VOUT = VIN - Rf V1 Equation
(12-5) 3
R1 R2
FigureLevel shifting
12.7 Level and and
shifting scaling circuit
scaling circuit
16
Example
17
STM32F3 ADC
18
Introduction (1)
19
Introduction (2)
21
Conversion Modes (2)
22
ADC sequencer converting 7 channels with different configured sampling times
Conversion Modes (3)
23
Conversion Modes (4)
28
STM32F3 Microcontroller Reference Manual, page 205
Conversion Modes (7)
This figure shows how to measure a power using the two ADCs in dual
regular simultaneous mode. To measure a single-phase power, ADC1 and
ADC2 are used with two channels (1 channel for the voltage and 1
channel for the current). To measure a three-phase power, ADC1 and
ADC2 are used with 6 channels (3 channels for the voltage and 3 channels
30
for the current).
STM32F3 Microcontroller Reference Manual, page 203 31
Cortex-M4
ADC Interrupt
DMA Request
32
EXTERNAL
ADC Channels INTERNAL
PIN CHANNEL PIN CHANNEL PIN CHANNEL PIN CHANNEL PIN CHANNEL PIN CHANNEL
FAST
PA2 ADC1_IN3 PA6 ADC2_IN3 PE13 ADC3_IN3 PB12 ADC4_IN3
PA3 ADC1_IN4 PA7 ADC2_IN4 PB14 ADC4_IN4
PF4 ADC1_IN5 PC4 ADC2_IN5 PB13 ADC3_IN5 PB15 ADC4_IN5
PC0 ADC12_IN6 PE8 ADC34_IN6
PC1 ADC12_IN7 PD10 ADC34_IN7
PC2 ADC12_IN8 PD11 ADC34_IN8
PC3 ADC12_IN9 PD12 ADC34_IN9
PF2 ADC12_IN10 PD13 ADC34_IN10
PC5 ADC2_IN11 PD14 ADC34_IN11
SLOW
PB2 ADC2_IN12 PB0 ADC3_IN12 PD8 ADC4_IN12
PE7 ADC3_IN13 PD9 ADC4_IN13
PE10 ADC3_IN14
OA1 ADC1_IN15 PE11 ADC3_IN15
TS ADC1_IN16 PE12 ADC3_IN16
BT/2 ADC1_IN17 OA2 ADC2_IN17 OA3 ADC3_IN17 OA4 ADC4_IN17
VRI ADC1_IN18 VRI ADC2_IN18 VRI ADC3_IN18 VRI ADC4_IN18
33
ADC main registers (x=1..4)
36
ADC Registers
37
ADC Registers
38
Bus Matrix and Busses
FLTIF FLASH
IBus
RAM
CORTEX-M4
DBus
CORE
Bus Matrix
AHB2 GPIO[A:F] TIM[1,8,15,16,17]
SPI1
SBus USART1
AHB3 SPI1
ADC[1:2]
EXTI
DMA1 COMP
fCLK ≤ 72MHz OPAMP
fCLK ≤ 72MHz Bridge2 APB2 SYSCFG
DMA2
AHB1 fCLK ≤ 36MHz
Bridge1 APB1 TIM[2,3,4,6,7]
SPI[2,3]
USART[2,:3]
UART[4:5]
fTIM[2:7] CLK = 2 * fAPB1CLK I2C[1,2]
TSC
(STM32F3 Microcontroller CAN
CRC
Datasheet, page 17) USB
RCC DAC
IWDG
WWDG
AHB[1;3]: Advanced High-performance Bus RTC
APB: Advanced Peripheral Bus
RCC: Reset and Clock Control 39
STM32F3 Microcontroller Reference Manual, pages 41-44
ADC clock
• The input clock of the two ADCs (master and slave) can
be selected between two different clock sources:
• The ADC clock can be a specific clock source, named
ADCxy_CK (xy=12 or 34) which is independent and
asynchronous with the AHB clock. It can be configured in the
RCC_CFGR2 to deliver up to 72 MHz (PLL output).
• To select this scheme, bits CKMODE[1:0] of the ADC_CCR register
must be reset.
• The ADC clock can be derived from the AHB clock of the ADC
bus interface, divided by a programmable factor (1, 2 or 4). In
this mode, a programmable divider factor can be selected (/1,
2 or 4 according to bits CKMODE[1:0]).
• To select this scheme, bits CKMODE[1:0] of the ADC_CCR register
must be different from “00”.
Note: CKMODE[1:0] is valid only if the AHB prescaler is set to 1 (to
achieve a clock duty cycle of 50%). 40
Clock tree (detail)
HCLK
ADCx->CCR.CKMODE[1:0] > 0
ADCxy_CK
ADCx->CCR.CKMODE[1:0] = 0
RCC->CFGR2.ADCxyPRES[4:0]
41
Clock configuration register 2 (RCC_CFGR2)
Bits
13:9 ADC34PRES Set and reset by software to 0xxxx: ADC34 clock disabled, ADC34
control PLL clock to ADC34 can use AHB clock
division factor. 10000: PLL clock ÷ 1
10001: PLL clock ÷ 2
10010: PLL clock ÷ 4
…
8:4 ADC12PRES Set and reset by software to 0xxxx: ADC12 clock disabled, ADC12
control PLL clock to ADC12 can use AHB clock
division factor. 10000: PLL clock ÷ 1
10001: PLL clock ÷ 2
10010: PLL clock ÷ 4
…
3:0 PREDIV These bits are set and cleared by 0000: HSE input to PLL not divided
software to select PREDIV1 division 0001: HSE input to PLL ÷ 2
factor. They can be 0010: HSE input to PLL ÷ 3
written only when the PLL is 0011: HSE input to PLL ÷ 4
disabled. … 42
ADC voltage regulator (ADVREGEN)
44
Single-ended and differential input channels
45
Calibration (ADCAL, ADCALDIF,
ADC_CALFACT)
• Each ADC provides an automatic calibration procedure
which drives all the calibration sequence including the power-
on/off sequence of the ADC.
• During the procedure, the ADC calculates a calibration factor
which is 7-bits wide and which is applied internally to the ADC
until the next ADC power-off.
• During the calibration procedure, the application must not use
the ADC and must wait until calibration is complete.
• Calibration is preliminary to any ADC operation. It removes the
offset error which may vary from chip to chip due to process
or band-gap variation.
• The calibration factor to be applied for single-ended input
conversions is different from the factor to be applied for
differential input conversions:
• Write ADCALDIF=0 before launching a calibration which will be
applied for single-ended input conversions.
• Write ADCALDIF=1 before launching a calibration which will be
applied for differential input conversions.
46
Calibration (ADCAL, ADCALDIF,
ADC_CALFACT)
• The calibration is then initiated by software by setting bit
ADCAL=1.
• Calibration can only be initiated when the ADC is
disabled (when ADEN=0).
• ADCAL bit stays at 1 during all the calibration sequence.
• It is then cleared by hardware as soon the calibration
completes.
• At this time, the associated calibration factor is stored
internally in the analog ADC and also in the bits
CALFACT_S[6:0] or CALFACT_D[6:0] of ADC_CALFACT
register (depending on single-ended or differential input
calibration)
• The internal analog calibration is kept if the ADC is
disabled (ADEN=0). However, if the ADC is disabled for
extended periods, then it is recommended that a new
calibration cycle is run before re-enabling the ADC.
47
Software procedure to calibrate the ADC
48
ADC on-off control (ADEN, ADDIS,
ADRDY)
• Once ADVREGEN[1:0] = ’01’, the ADC must be
enabled and the ADC needs a stabilization time
tSTAB before it starts converting accurately (10µs).
• Two control bits enable or disable the ADC:
• ADEN=1 enables the ADC. The flag ADRDY will be set once
the ADC is ready for operation.
• ADDIS=1 disables the ADC.
• ADEN and ADDIS are then automatically cleared by
hardware as soon as the analog ADC is effectively
enabled/disabled.
• Regular conversion can then start by setting
ADSTART=1.
49
ADC on-off control (ADEN, ADDIS,
ADRDY)
• The internal analog calibration is lost each time the
power of the ADC is removed (example, when the
product enters in STANDBY or VBAT mode.
• In this case, to avoid spending time recalibrating the
ADC, it is possible to re-write the calibration factor into
the ADC_CALFACT register without recalibrating,
supposing that the software has previously saved the
calibration factor delivered during the previous
calibration.
• The calibration factor can be written if the ADC is
enabled but not converting (ADEN=1 and ADSTART=0).
Then, at the next start of conversion, the calibration
factor will automatically be injected into the analog
ADC.
• This loading is transparent and does not add any cycle latency
to the start of the conversion.
50
ADC on-off control (ADEN, ADDIS,
ADRDY)
• Software procedure to enable the ADC
• 1. Set ADEN=1.
• 2. Wait until ADRDY=1 (ADRDY is set after the ADC startup
time). This can be done using the associated interrupt
(setting ADRDYIE=1).
• Software procedure to disable the ADC
• 1. Check that ADSTART=0 to ensure that no conversion is
ongoing. If required, stop any regular conversion ongoing
by setting ADSTP=1 and then wait until ADSTP=0.
• 2. Set ADDIS=1.
• 3. If required by the application, wait until ADEN=0, until the
analog ADC is effectively disabled (ADDIS will automatically
be reset once ADEN=0).
51
Channel selection (SQRx)
53
Channel selection (SQRx)
54
Channel-wise programmable sampling time
(SMPR1, SMPR2)
• Before starting a conversion, the ADC must establish a direct connection
between the voltage source under measurement and the embedded
sampling capacitor of the ADC. This sampling time must be enough for
the input voltage source to charge the embedded capacitor to the
input voltage level.
• Each channel can be sampled with a different sampling time which is
programmable using the SMP[2:0] bits in the ADC_SMPR1 and
ADC_SMPR2 registers. It is therefore possible to select among the
following sampling time values:
SMP ADC clock cycles • The total conversion time is calculated as
000 1.5 follows (resolution = 12 bits):
001 2.5
• Tconv = Sampling time + 12.5 ADC clock
cycles
010 4.5 • Example:
011 7.5 • With FADC_CLK = 72 MHz and a sampling
100 19.5 time of 1.5 ADC clock cycles:
101 61.5
• Tconv = (1.5 + 12.5) ADC clock
cycles = 14 ADC clock cycles =
110 181.5 0.194 μs (for fast channels)
111 601.5
55
• Cext represents the capacitance of the PCB (dependent on
soldering and PCB layout quality) plus the pad capacitance
(roughly 7 pF). A high Cext value will downgrade conversion
accuracy. To remedy this, fADC should be reduced.
Rin
5pF
56
Constraints on the sampling time for fast
and slow channels
• For each channel, bits
SMP[2:0] must be
programmed to respect a
minimum sampling time
which depends on:
• the type of channel (fast or
slow)
• the resolution
• the output impedance of the
external signal source to be
converted (Rin)
57
Single conversion mode (CONT=0)
58
Single conversions of a sequence,
software trigger (Timing Diagram)
59
Continuous conversion mode (CONT=1)
61
ADC Example (1)
#include "stm32f30x.h“
uint16_t ADC1ConvertedValue = 0;
uint16_t ADC1ConvertedVoltage = 0;
uint16_t calibration_value = 0;
Volatile uint32_t TimingDelay = 0;
int main(void)
{
// At this stage the microcontroller clock tree is already configured
RCC->CFGR2 |= RCC_CFGR2_ADCPRE12_DIV2; // Configure the ADC clock
RCC->AHBENR |= RCC_AHBENR_ADC12EN; // Enable ADC1 clock
// Setup SysTick Timer for 1 µsec interrupts
if (SysTick_Config(SystemCoreClock / 1000000))
{
// Capture error
while (1)
{}
}
62
ADC Example (2)
/* Calibration procedure */
ADC1->CR &= ~ADC_CR_ADVREGEN;
ADC1->CR |= ADC_CR_ADVREGEN_0; // 01: ADC Voltage regulator enabled
Delay(10); // Insert delay equal to 10 µs
ADC1->CR &= ~ADC_CR_ADCALDIF; // calibration in Single-ended inputs Mode.
ADC1->CR |= ADC_CR_ADCAL; // Start ADC calibration
// Read at 1 means that a calibration in progress.
while (ADC1->CR & ADC_CR_ADCAL); // wait until calibration done
calibration_value = ADC1->CALFACT; // Get Calibration Value ADC1
63
ADC Example (3)
// ADC configuration
ADC1->CFGR |= ADC_CFGR_CONT; // ADC_ContinuousConvMode_Enable
ADC1->CFGR &= ~ADC_CFGR_RES; // 12-bit data resolution
ADC1->CFGR &= ~ADC_CFGR_ALIGN; // Right data alignment
while (1)
{
while(!(ADC1->ISR & ADC_ISR_EOC)); // Test EOC flag
ADC1ConvertedValue = ADC1->DR; // Get ADC1 converted data
ADC1ConvertedVoltage = (ADC1ConvertedValue *3300)/4096; // Compute the voltage
}
}
64
ADC Example (4)
void SysTick_Handler(void)
{
TimingDelay--;
}
65