0% found this document useful (0 votes)
6 views

Tutorial for LAB5

The document is a tutorial for LAB5 in the ELEC 3300 course at HKUST, focusing on the use of Analog to Digital Converters (ADC) and Digital to Analog Converters (DAC) in STM32 microcontrollers. It covers various topics including ADC modes, configuration, calibration, and practical tasks involving the use of ADC with external components like variable resistors and light-dependent resistors. Students are expected to understand the differences between single and continuous conversion modes, and to implement a light intensity system using an RGB LED.

Uploaded by

Pranjal Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Tutorial for LAB5

The document is a tutorial for LAB5 in the ELEC 3300 course at HKUST, focusing on the use of Analog to Digital Converters (ADC) and Digital to Analog Converters (DAC) in STM32 microcontrollers. It covers various topics including ADC modes, configuration, calibration, and practical tasks involving the use of ADC with external components like variable resistors and light-dependent resistors. Students are expected to understand the differences between single and continuous conversion modes, and to implement a light intensity system using an RGB LED.

Uploaded by

Pranjal Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

ELEC 3300 – Tutorial for LAB5

Department of Electronic and Computer Engineering


HKUST
by WU Chi Hang

1
Terminology
Analogue Digital

The whole analogue How many levels


range that you want depends on the
to chop into number of bits used.

2
An N-bit Linear DAC/ADC
Analogue Digital
e.g. 0 – Vmax 111…111 N bits
111…110
Vout  ?

How many levels


are there ?

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 1s (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

Where is the ADCCLK source ? APB1/APB2 ?


What is the Max value of ADCCLK ?

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

 Actually there are many ADC channels (not limited to the


one shown above) in STM32 that you can use.

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

Last digit of your 0 1 2 3 4 5 6 7 8 9


student ID
Channel number 12 13 2 3 4 5 6 7 14 15
Corresponding I/O
pin in STM32 ?

 Check the corresponding I/O pin from datasheet then check


the location of that pin on MINI V3 board

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

Choose HSE APB1 /2

PLLMul X9

23
Setting the clock of ADC in CubeIDE

 Is it the fastest setting of ADCCLK?


 How can you change it to fastest ?

24
Configuration of ADC

Channel number you use

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.

HAL_ADCEx_Calibration_Start(&hadc1); Depends on which ADC you use

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

 Please note that there is a time needed for the conversion,


you can use the following line to poll the end of conversion.
HAL_ADC_PollForConversion(&hadc1, 1000);
Timeout in milliseconds

 You can read the conversion result by calling the


procedure
uint32_t HAL_ADC_GetValue(&hadc1);

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

void LCD_DrawChar(uint16_t usC, uint16_t usP, const char cChar);


void LCD_DrawString(uint16_t usC, uint16_t usP, const char * pStr);

 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);

During the conversion process, what Conversion Process


you need to do in your program ?

After conversion is done, what you


need to do ? How do you know if the After conversion the
conversion is completed ? ADC will
STOP by itself

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

 You are required to use


Continuous Conversion Mode of
ADC2 to finish Task 2
 You need to write a program to
display an ADC2 conversion result
of the external VR in both decimal
and hex.
 You need to show to your TA your
main.c for verifying the mode used

32
Task 2 – Relation between SW and ADC

In Program In STM32

HAL_ADC_Start(&hadc2);

During the conversion process, what


Conversion Process
you need to do in your program ?

After conversion is done, what you After conversion the


need to do ? How do you know if the ADC will
conversion is completed ?
DO ANOTHER
CONVERSION

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.

The result you displayed should be


1. From ______ to ______ for DEC
2. From ______ to ______ for HEX
3. 1 and 2 should be SAME value but
DIFFERENT REPRESENTION ONLY
DEC HEX Correct ?
0 000
If there exists () a result violating any of above 3
10 0a0
conditions, it means your program have BUG…...
0004 000
1234 4d2
6013 177D
Please think out why .and FIX it. 7129 2c8

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…

1. Use CubeIDE to initialize different ADC modes.


2. Understand the difference between Single Conversion mode and
Continuous Conversion mode, and how to program them.
3. Calculate the sampling frequency for a conversion based on the
relation between input clock and the sample time.
4. Get the analogue input from the ADC for further processing.
5. Understand why and when we need ADC as an input to STM32.
6. Integrate the knowledge of LAB2 (RGB LED), LAB3 (LCD) add to
LAB5.

39
END

40

You might also like