VXLFDGDGDG
VXLFDGDGDG
VXLFDGDGDG
Le Trong Nhan
Contents
Microcontroller Page 5
Page 6 HCMUT - Computer Engineering
CHAPTER 1
LED Animations
1 Introduction
In this manual, the STM32CubeIDE is used as an editor to program the ARM micro-
controller. STM32CubeIDE is an advanced C/C++ development platform with peripheral
configuration, code generation, code compilation, and debug features for STM32 micro-
controllers and microprocessors.
The most interest of STM32CubeIDE is that after the selection of an empty STM32 MCU or
MPU, or preconfigured microcontroller or microprocessor from the selection of a board,
the initialization code generated automatically. At any time during the development, the
user can return to the initialization and configuration of the peripherals or middleware
and regenerate the initialization code with no impact on the user code. This feature can
simplify the initialization process and speedup the development application running on
STM32 micro-controller. The software can be downloaded from the link bellow:
https://ubc.sgp1.digitaloceanspaces.com/BKU_Softwares/STM32/stm32cubeide_1.7.0.zip
Moreover, for a hangout class, the program is firstly simulated on Proteus. Students are
also supposed to download and install this software as well:
https://ubc.sgp1.digitaloceanspaces.com/BKU_Softwares/STM32/Proteus_8.10_SP0_Pro.exe
The IDE needs to download some packages, which normally takes time in this first time a
project is created.
Step 2: Select the STM32F103C6 in the following dialog, then click on Next
Step 3: Provide the Name and the Location for the project.
Microcontroller Page 9
Figure 1.4: Select the target device
It is important to notice that the Targeted Project Type should be STM32Cube. In the
case this option is disable, step 1 must be repeated. The location path should not contain
special characters (e.g. the space). Finally, click on the Next button.
Step 4: On the last dialog, just keep the default firmware version and click on Finish
button.
From the configuration windows, select Pin configuration, select the pin PA5 and set to
GPIO Output mode, since this pin is connected to an LED in the STM32 development kit.
Step 6: Right click on PA5 and select Enter user lable, and provide the name for this pin
(e.g. LED_RED). This step helps programming afterward more memorable.
Finally, save the configuration process by pressing Ctrl + S and confirm this step by click-
ing on OK button. The code generation is started.
Step 7: Implement the first blinky project in the main function as follow:
Microcontroller Page 11
1 int main ( void )
2 {
3 /* USER CODE BEGIN 1 */
4
7 /* MCU Configuration
--------------------------------------------------------
*/
8
29 /* Infinite loop */
30 /* USER CODE BEGIN WHILE */
31
32 while (1)
33 {
34 HAL_GPIO_TogglePin ( LED_RED_GPIO_Port , LED_RED_Pin ) ;
35 HAL_Delay (1000) ;
36 /* USER CODE END WHILE */
37
Navigate to C/C++ Build, select Settings, MCU Post build outputs, and check to the Intel
Hex file.
Step 8: Build the project by clicking on menu Project and select Build Project. Please
check on the output console of the IDE to be sure that the hex file is generated, as follow:
The hex file is located under the Debug folder of your project, which is used for the sim-
ulation in Proteus afterward. In the case a development kit is connected to your PC, from
menu Run, select Run to download the program to the hardware platform.
In the case there are multiple project in a work-space, double click on the project name to
activate this project. Whenever a project is built, check the output files to make sure that
you are working in a right project.
Microcontroller Page 13
3 Simulation on Proteus
For an online training, a simulation on Proteus can be used. The details to create an
STM32 project on Proteus are described bellow.
Step 1: Launch Proteus (with administration access) and from menu File, select New
Project.
Step 2: Provide the name and the location of the project, then click on Next button.
Step 3: For following dialog, just click on Next button as just a schematic is required for
the lab.
Step 5: On the main page of the project, right click to select Place, Components, From
Libraries, as follows:
If there is an error with no library found, please restart the Proteus software with Run
as administrator option.
Step 6: From the list of components in the library, select STM32F103C6, as follows:
Microcontroller Page 15
Figure 1.15: Select STM32F103C6
Repeat step 5 and 6 to select an LED, named LED-RED in Proteus. Finally, these compo-
nents are appeared on the DEVICES windows, which is on left hand side as follows:
Step 7: Place the components to the project: right click on the main page, select on Place,
Component, and select device added in Step 6. To add the Power and the Ground, right
click on the main page, select on Place, Terminal. The result in this step is expected as
follows:
Step 8: Start wiring the circuit. The negative pin of the LED is connected to PA5 while its
positive pin is connected to the power supply. For the power and the ground on the right,
just make a short wire, which will labeled in the next step.
In this step, also double click on the power supply in order to provide the String property
to +3.3V.
Microcontroller Page 17
Step 8: Right click on the wire of the power supply and the ground, and select Place wire
Label
This step is required as VDDA and VSSA of the STM32 must be connected to provide the
reference voltage. Therefore, VDDA is connected to 3.3V, while the VSSA is connected to
the Ground. Finally, the image of our schematic is shown bellow:
Figure 1.21: Set the program of the STM32 to the hex file from Cube IDE
From now, the simulation is ready to start by clicking on the menu Debug, and select on
Run simulation. To stop the simulation, click on Debug and select Stop VMS Debugging.
Moreover, there are some quick access bottom on the left corner of the Proteus to start or
stop the simulation, as shown following:
Figure 1.22: Quick access buttons to start and stop the simulation
If everything is success, students can see the LED is blinking every second. Please stop the
simulation before updating the project, either in Proteus or STM32Cube IDE. However,
the step 9 (set the program file for STM32 in Proteus) is required to do once. Beside the
toggle instruction, student can set or reset a pin as following:
1 while (1) {
2 HAL_GPIO_WritePin ( LED_RED_GPIO_Port , LED_RED_Pin ,
GPIO_PIN_SET ) ;
3 HAL_Delay (1000) ;
4 HAL_GPIO_WritePin ( LED_RED_GPIO_Port , LED_RED_Pin ,
GPIO_PIN_RESET ) ;
5 HAL_Delay (1000) ;
6 }
Program 1.2: An example for LED blinky
Microcontroller Page 19
4 Exercise and Report
4.1 Exercise 1
From the simulation on Proteus, one more LED is connected to pin PA6 of the STM32
(negative pin of the LED is connected to PA6). The component suggested in this exercise
is LED-YELLOW, which can be found from the device list.
In this exercise, the status of two LEDs are switched every 2 seconds, as demonstrated in
the figure bellow.
Report 1: Depict the schematic from Proteus simulation in this report. The caption of the
figure is a downloadable link to the Proteus project file (e.g. a github link).
Report 2: Present the source code in the infinite loop while of your project. If a user-
defined functions is used, it is required to present in this part. A brief description can be
added for this function (e.g. using comments). A template to present your source code is
presented bellow.
1 while (1) {
2 HAL_GPIO_TogglePin ( GPIOA , GPIO_PIN_5 ) ;
3 HAL_Delay (1000) ;
4 }
Program 1.3: An example for your source code
4.2 Exercise 2
Extend the first exercise to simulate the behavior of a traffic light. A third LED, named
LED-GREEN is added to the system, which is connected to PA7. A cycle in this traffic light
is 5 seconds for the RED, 2 seconds for the YELLOW and 3 seconds for the GREEN. The
LED-GREEN is also controlled by its negative pin.
Similarly, the report in this exercise includes the schematic of your circuit and a your
source code in the while loop.
Report 1: Present the schematic.
4.4 Exercise 4
Add only one 7 led segment to the schematic in Exercise 3. This component can be found
in Proteus by the keyword 7SEG-COM-ANODE. For this device, the common pin should
be connected to the power supply and other pins are supposed to connected to PB0 to
PB6. Therefore, to turn-on a segment in this 7SEG, the STM32 pin should be in logic 0
(0V).
Implement a function named display7SEG(int num). The input for this function is from
0 to 9 and the outputs are listed as following:
Microcontroller Page 21
This function is invoked in the while loop for testing as following:
1 int counter = 0;
2 while (1) {
3 if ( counter >= 10) counter = 0;
4 display7SEG ( counter ++) ;
5 HAL_Delay (1000) ;
6
7 }
Program 1.4: An example for your source code
4.5 Exercise 5
Integrate the 7SEG-LED to the 4 way traffic light. In this case, the 7SEG-LED is used to
display countdown value.
In this exercise, only source code is required to present. The function display7SEG in
previous exercise can be re-used.
4.6 Exercise 6
In this exercise, a new Proteus schematic is designed to simulate an analog clock, with
12 different number. The connections for 12 LEDs are supposed from PA4 to PA15 of the
STM32. The arrangement of 12 LEDs is depicted as follows.
Report 1: Present the schematic. Report 2: Implement a simple program to test the con-
nection of every single LED. This testing program should turn every LED in a sequence.
4.8 Exercise 8
Implement a function named setNumberOnClock(int num). The input for this function
is from 0 to 11 and an appropriate LED is turn on. Present the source code of this function.
4.9 Exercise 9
Implement a function named clearNumberOnClock(int num). The input for this func-
tion is from 0 to 11 and an appropriate LED is turn off.
4.10 Exercise 10
Integrate the whole system and use 12 LEDs to display a clock. At a given time, there are
only 3 LEDs are turn on for hour, minute and second information.
Microcontroller Page 23