Tutorial for LAB5
Tutorial for LAB5
1
Terminology
Analogue Digital
2
An N-bit Linear DAC/ADC
Analogue Digital
e.g. 0 – Vmax 111…111 N bits
111…110
Vout ?
Vmax
Vout 2 000…010
2N
000…001
Vmax
Vout 1 0V 000…000
2N
3
Digital to Analogue Converter (DAC)
4
Terminology
Resolution
Number of bits in the binary word used in the sample.
Full Scale Output Voltage (for DAC)
Maximum output voltage of the D/A convertor. (Always 1 LSB below
the stated value)
Setting Time (for DAC)
When you change a binary word applied to the input of a converter,
the output will change to appropriate new value. (Time the output to
takes to get within ± ½ LSB
Conversion Time (for ADC)
Time that convertor takes to produce a valid output binary code for an
applied input voltage.
5
Analogue to Digital Converter (ADC)
6
Analogue to Digital Convertor (ADC)
In STM32 there are three ADCs inside. Each ADC is 12-bit.
It has up to 18 multiplexed channels allowing it measure
signals from 16 external and two internal sources.
A/D conversion of the various channels can be performed in
single, continuous, scan or discontinuous mode.
The result of the ADC is stored in a left-aligned or right-
aligned 16-bit data register.
The conversion time is around 1s (refer to page 207 of the
reference manual)
7
About Conversion Mode
A/D conversion of the various channels can be performed in
single, continuous, scan or discontinuous mode.
Single conversion mode
ADC does one conversion, after finished conversion, ADC stopped.
Continuous conversion mode
ADC starts another conversion as soon as it finishes one.
Scan mode
This mode is used to scan a group of analog channels. A single
conversion is performed for each channel of the group. After each end of
conversion the next channel of the group is converted automatically.
When using scan mode, DMA bit must be set and the direct memory
access controller is used to transfer the converted data of regular group
channels to SRAM after each update of the ADC_DR register.
Please refer to Reference Manual Section 11.3 for details.
8
About Conversion Mode
Single Channel
Single Conversion Mode Scan mode
Multi-channel Multi-channel
Single Conversion Mode Continuous Conversion Mode
Single Channel
Continuous Conversion Mode
9
Block Diagram (Ref. Man. P. 208)
10
Clock Tree
11
Note on ADCCLK
Refer to Page 207 of the Reference Manual, it said
ADC conversion time:
STM32F103xx performance line devices: 1 μs at 56 MHz (1.17 μs at
72 MHz)
Question
Why the SYSCLK faster, but conversion time is longer ?
Refer to last page.
ADCCLK originates from APB2
The Prescaler of ADCCLK can only be /2, 4, 6, 8
If APB2 = 72MHz, what the max ADCCLK can be ?
If APB2 = 56MHz, what the max ADCCLK can be ?
12
Total Conversion Time
Refer to Section 11.6 of the Reference Manual.
Each channel can be sampled with a different sample time.
The total conversion time is calculated as follows:
Tconv = Sampling time + 12.5 cycles
The sampling time can be set via the SMP[2:0] bits in the ADC_SMPR1
and ADC_SMPR2 registers.
Example:
With an ADCCLK = 14 MHz and a sampling time of 1.5 cycles:
Tconv = 1.5 + 12.5 = 14 cycles = 1 μs
13
Sampling Time
14
About Data Alignment
The data alignment means how the ADC data is being put
into the register.
15
ADC in MINI-V3
In MINI-V3, there are different ADC channels located at
position 1 of the figure below
16
ADC in MINI-V3
The corresponding schematic is also shown here, note that
it is not pin to pin correspondence to the board.
Note: PC0 has a
4.7kΩ to 3V3
ON BOARD VR
As you see
there is a
jumper at J10
connected to
PC1 and VR
17
ADC in MINI-V3
The on board VR is not good to use and easy to be
damaged. As a result, we will use an outside circuit to
connect the analogue input to one of the channels
ON BOARD VR
18
ADC in STM32
Refer to STM32 Datasheet,
ADC1 and ADC2 have 16 channels (from IN0 to IN15) to use
ADC3 have 13 channels (from IN0 to IN8, IN10 to IN13)
IN0 IN1 IN2 IN3 IN4 IN5 IN6 IN7 IN8 IN9 IN10 IN11 IN12 IN13 IN14 IN15
ADC1
ADC2
ADC3 N/A N/A N/A
In Datasheet
ADC123_IN10 means Channel 10 shared by ADC1 ADC2 and ADC3
ADC12_IN5 means Channel 5 shared by ADC1 and ADC2 only
ADC3_IN8 means Channel 8 for ADC3 only
Question : If one shared channel means one I/O pin, if we use all the
channels available, how many I/O pins will be used by ADC ?
19
ADC in STM32
In this LAB, we will use ADC1 and ADC2, you need to use
the channel according to the last digit of your student ID
20
Using VR as input
Using your breadboard, assemble the following circuit, this
would be used for your Task 1 and Task 2.
Channel input
depends on your
Student ID
21
Configuration of LCD
In this LAB, we need to use the LCD to display the value.
Please refer to the Tutorial for CubeIDE and Tutorial for
LAB3 to create a project that allows you to use the LCD
Display.
Or you may start your LAB5 by using the LAB3 as a starting
point.
22
Clock Configuration
Choose PLLCLK
PLLMul X9
23
Setting the clock of ADC in CubeIDE
24
Configuration of ADC
ADC1
Mode and
Alignment
Selection
Conversion
Selection
25
About Calibration
The ADC has an built-in self calibration mode. Calibration
significantly reduces accuracy errors due to internal
capacitor bank variations.
During calibration, an error-correction code (digital word) is
calculated for each capacitor, and during all subsequent
conversions, the error contribution of each capacitor is
removed using this code.
Once calibration is over, the CAL bit is reset by hardware
and normal conversion can be performed. It is
recommended to calibrate the ADC once at power-on. The
calibration codes are stored in the ADC_DR as soon as
the calibration phase ends.
26
About Calibration
The datasheet suggested to perform a self calibration at
the startup, so, you may want to add the following line
before the while(1) loop of your code.
27
How to start a conversion and get the
result ?
The ADC conversion can be initiated by
HAL_ADC_Start(&hadc1); Depends on which ADC you use
28
Task 1 – Single Conversion on ADC1
You are required to use Single Conversion
Mode of ADC1 to finish Task 1.
You need to write a program to display the
ADC conversion result of the external VR.
When K2 is pressed, ADC1 will start the
conversion of taking ONE DATA and result will
be displayed on LCD in both decimal and hex.
ADC1 should NOT be working if K2 is NOT
pressed.
You need to show to your TA your main.c for
verifying the mode used.
29
Task 1 – Display the Result
You need to think about how to decompose the value to
display. The following functions and table might help you.
Details refer back to lcd.c in the lcd.zip
You can also use stdlib function like sprintf() to help you.
30
Task 1 – Relation between SW and ADC
In Program In STM32
HAL_ADC_Start(&hadc1);
NOTE : For Task 1, the whole PROCESS should be done when K2 is PRESSED
Question : How can I make the ADC work again ?
31
Task 2 – Continuous Conversion on ADC2
32
Task 2 – Relation between SW and ADC
In Program In STM32
HAL_ADC_Start(&hadc2);
Question: Do you think you need to ask the ADC to start again ?
IMPORTANT: ADCs are working in DIFFERENT MODES for Task 1 and Task 2
33
Task 1 and 2 – Note
You are **REQUIRED** to display the ADC1 and ADC2 result on the LCD
Display at the same time.
34
Connecting to LDR
Light Dependent Resistor is a resistor that the resistance
will depends on the light intensity. In this part, you need to
use the LDR as an analogue input to the system.
35
Using LDR as input
Replace your VR with a fixed resistor and a LDR
Channel input
depends on your
Student ID
36
Task 3: Using LDR as input
Following the steps listed on the LAB Sheet, fill the
corresponding data.
You might need to try to swap the LDR and the resistor.
37
Task 4: Light Intensity System
Using your knowledge from Task 3 and LAB2, together with the RGB
LED on MINI V3 Development Board, implement a five-level Light
Intensity System such that..
Light Intensity
Very Dark Dark Medium Bright Very Bright
RGB LED
WHITE RED GREEN BLUE OFF
COLOR
You can use either Figure 1 or Figure 2 on Page 2 of the LAB sheet to
implement the system, as long as it follows above requirement.
You are free to choose the boundary for the system, but you need to
clearly show the TA the five different levels according to the above
requirement during the demo.
38
After finishing LAB5 you are expected to…
39
END
40