Você está na página 1de 39

z

Analog to Digital Converters (ADC)


 What is ADC?

• An electronic circuit which transforms signal from analogue


(continuous) to digital (discrete form)
Example of analogue wave corrupted by noise
A received digital signal may be impaired by noise and
distortions without necessarily affecting the digits
Quantization and sampling rate

The blue curve is the input analog waveform, the


black dots are the samples, and the red curve is
the quantized output
Adc in stm32f429

• ADC1, ADC2, ADC3


• Regular group and injected group
• 16 channel
• Single and continuous conversion modes
• Scan mode for automatic conversion of channel 0 to channel ‘n’
• Discontinuous mode
• Dual/Triple mode
ADC on-off control

• Enable ADC clock from apb2en register in RCC


• Wakes up ADC from power down mode by setting ADON bit
• Start conversion by setting SWSTART or JSWSTART bit
• Stop conversion by clearing ADON bit
Regular group and Injected group

• A regular group performs normal conversion, while injected


group is simply having a higher priority than a regular group

• Regular group is composed of up to 16 conversions while an


injected group is composed only up to 4 conversions.
Regular group and Injected group (cont.)

The injected group has priority over the regular channel group. It interrupts the conversion of
the current channel in the regular channel group.
Single conversion mode
• Simplest of all mode
• ADC performs single conversion of a single channel and stops after
completion of the conversion
Single conversion mode (cont.)
Single conversion mode can be start by :
• setting the SWSTART bit in the ADC_CR2 register (for a regular channel only)
• setting the JSWSTART bit (for an injected channel)
• external trigger (for a regular or injected channel)
• Set the regular channel sequence length to 1 in ADC_SQR1, and specify the channel
in SQx in ADC_SQR1

Once the conversion of the selected channel is complete:


• If a regular channel was converted:
– The converted data are stored into the 16-bit ADC_DR register
– The EOC (end of conversion) flag is set
– An interrupt is generated if the EOCIE bit is set
• If an injected channel was converted:
– The converted data are stored into the 16-bit ADC_JDR1 register
– The JEOC (end of conversion injected) flag is set
– An interrupt is generated if the JEOCIE bit is set
Then the ADC stops.
Continuous conversion mode
• Converts a single channel continuously and indefinitely.

• DMA can be used in circular mode to reduce the CPU load

• Injected channel cannot be converted continuously, only


exception is when using auto injection

• This mode is started with the CONT bit at 1. EOC (end of


conversion) flag is set every successful conversion.
Discontinuous conversion mode
• Enable by setting DISCEN bit in ADC_CR1 register to enable in regular mode.
• To enable in injected mode, set JDISCEN bit instead.
• Used to convert a short sequence that is part of the sequence in SQR register.
• Value of short sequence is write inside DISCNUM bit.
• Example:

• Channels to be converted in SQR register : 0, 1, 2, 3, 6, 7, 9, 10


• 1st trigger : 0 , 1, 2, EOC is generated
• 2nd trigger : 3, 6, 7, EOC is generated
• 3rd trigger: 9, 10, EOC is generated
Injected channel management

Auto injection

• The channel in the injected group are automatically converted


after the regular group of channels.

• This can be used to convert a sequence of up to 20


conversions programmed in the ADC_SQRx and ADC_JSQR registers.
(16 of Regular + 4 of injected)
Injected channel management

Triggered injection

1. JAUTO bit must be cleared


2. Start a regular conversions.
3. If an external injected trigger (timer expired, exit), or software trigger (set
JSWSTART bit, the current regular conversions is reset and the injected channel
seq switches to scan-once mode.
4. Then the regular conversion of the regular group is resumed.
5. If a regular event occurs during injected conversion, the injected converision is
not interrupted but the regular sequence is executed at the end of
injected sequence.
Injected channel management (cont.)
Playing with built in temperature sensor

• The temperature sensor is internally connected to channel 18 on ADC1


which is shared alongside with VBAT
• The temperature sensor can be used to measure the ambient temperature
of the device.
• To use the sensor :
1. Select ADC1_in18 (channel 18) input channel
2. Select sampling time greater than minimum sampling time specified in data sheet
3. Set TSVREFE bit to wake temperature sensor up
4. Start ADC conversion by setting SWSTART bit.
5. Read the resulting Vsense data in the ADC DR.
6. Calculate the temperature using the following formula:
Temperature (in °C) = {(VSENSE – V25) / Avg_Slope} + 25
Where:
– V25 = VSENSE value for 25° C
– Avg_Slope = average slope of the temperature vs. VSENSE curve (given in mV/°C
or μV/¡ãC)
The actual value of V25 and Avg_slope can be found in datasheet.
• We will use single conversion and continuous conversion and observe the differences
Playing with built in temperature sensor (cont.)
Important parameters to calculate temperature as well as the sampling time (pg.163)
Playing with built in temperature sensor (cont.)
Configure using CUBEMX

1. Enable the temperature sensor channel


Playing with built in temperature sensor (cont.)
Single Conversion Mode

1. Go to ADC configuration,
ensure that the number of conversion
is 1 as we only converting 1 channel
2. Select regular conversion launched by
software. We will initiate conversion by
setting SWSTART bit to 1.
3. Select channel Temperature sensor.
4. Sampling time shouldn’t greater than min
sampling time in the datasheets, which is
10us
5. To enable single conversion mode, disable
the continuous conversion mode.
Calculation for sampling time

Note: the clock that we are using is apb2


clock, which is 16Mhz, and prescale into
8Mhz
16/2𝑀
( 𝑐𝑦𝑐𝑙𝑒𝑠 ) −1 > 10us

Solving for equation above, the cycles


must be greater than 80.
Playing with built in temperature sensor (cont.)
Single Conversion Mode

1. Add the code as shown above, put a breakpoint at


HAL_ADC_GetValue().
2. If single conversion is enable, the adcRawVal should stay the same

Note: to calculate the exact temperature, use the equation in the first page of playing
with built in temperature sensor.
Playing with built in temperature sensor (cont.)
Continuous Conversion Mode

1. Leave the settings as it is previously,


enable the continuous conversion mode.
2. Generate the source file.
3. Leave also the source code as it is
previously, and the data obtained should
be changing.
Playing with built in temperature sensor (cont.)
Programming without cubemx
Conversion on external trigger and trigger polarity

• Conversion can be triggered by an external event (timer capture,


EXTI line). Table below provides the correspondence between EXTEN
and JEXTEN values and trigger polarity.
Conversion on external trigger and trigger polarity (cont.)

• EXTSEL and JEXTSEL control bits are used to select 16 possible events that
trigger
Regular channel
Conversion on external trigger and trigger polarity (cont.)

Injected channel
Conversion on external trigger and trigger polarity (cont.)
Programming with cubemx

1. enable the external trigger for regular


conversion
2. EXTI11 line selected is PF11.
Note that it doesn’t necessary to be
PF11, it could be PA11, PA12 etc..
Conversion on external trigger and trigger polarity (cont.)
Programming with cubemx

1. Select trigger conversion source as exti line11, and trigger detection as rising or falling
edge.
2. Don’t forget also to enable EXTI interrupts on NVIC configuration.
3. Use the same code as before, but now only exti interrupt could initiate ADC conversion.
Conversion on external trigger and trigger polarity (cont.)
External trigger on EXTI line11 for regular channel
Programming without cubemx
• The exti channel select is PF11
Interrupts on ADC

• Be able to generate interrupts after EOC or EOS bit is set by either setting
EOCIE bit (for regular channel) or JEOCIE bit (for injected channel).

• Adc interrupt on NVIC needs to be enable. ADC interrupt is on position 18

• After interrupt generated, EOC or EOS bit has to be cleared by user.


Multi ADC mode

The four possible modes below are implemented:


• Injected simultaneous mode
• Regular simultaneous mode
• Interleaved mode
• Alternate trigger mode

There can also be combined in the following ways


• Injected simultaneous mode + Regular simultaneous mode
• Regular simultaneous mode + Alternate trigger mode

Multi Adc mode selection is done in setting MULTI[4:0] bits in ADC_CCR


Multi ADC mode cont.

Injected simultaneous mode

• Converts an injected group of channels. Trigger sources comes from JEXTSEL on ADC1.
A simultaneous trigger source is provided to the other ADC.
Dual ADC
Multi ADC mode cont.

Injected simultaneous mode

• Behave similar like injected simultaneous, it converts an regular group of channels.


Trigger sources also comes from JEXTSEL on ADC1. A simultaneous trigger source
is provided to the other ADC.
Multi ADC mode cont.

Regular simultaneous mode


• Converts an regular group of channels. Trigger sources comes from JEXTSEL on ADC1.
A simultaneous trigger source is provided to the other ADC.

Triple ADC
Multi ADC mode cont.
Interleaved mode

• This mode can be started only on a regular group (usually one channel). The external
trigger source also comes from the regular channel multiplexer of ADC1.
• After an external trigger occurs:
- ADC1 starts immediately
- ADC2 starts after a delay of several ADC clock cycles
• The minimum delay can be configured in DELAY bits in ADC_CCR register.
• The conversion will repeat itself if CONT bit is set.

Dual ADC mode


Multi ADC mode cont.
Interleaved mode (cont.)

Triple ADC
Multi ADC mode cont.
Alternate trigger mode

• This mode can be started only on an injected group. The source of


external trigger comes from the injected group multiplexer of ADC1.
• When the 1st trigger occurs, all injected ADC1 channels in the group
are converted
• When the 2nd trigger occurs, all injected ADC2 channels in the group are
converted and so on
• Interrupt can be generated (JEOC) after all injected group of ADC1
channels have been converted.
Multi ADC mode cont.

Alternate trigger mode in discontinuous mode


References

• Stm32 Adc mode’s and their applications AN3116


• Stm32f4 reference manual
• Description of stm32F4 HAL library and their applications

Você também pode gostar