You are on page 1of 12

LED BLINKING LAB USING THE STM32F103C6 Microcontroller

Software Requirements

 STM32Cube IDE

 Proteus 8.9 or above

STM32CubeIDE

 The STM32CubeIDE is a complete development system to develop code for almost

all STM32-based microcontrollers from ST Microelectronics.

 As the name suggests, it is an Integrated Development Environment (IDE) that

essentially includes the STMCubeMx GUI HW configuration tool, and a full

compiler.

 After the selection of an empty STM32 MCU or MPU, the project is created and

initialization code generated.

STM32CubeMX

 STM32Cube-MX is a graphical tool for selecting, configuring and generating code

for STM32.

 It uses the STM32 hardware abstraction layer (HAL) library to create the

initialization code, which makes it a lot easier to migrate between STM32

microcontrollers if needed.
 After the code is generated, everything should be ready to use the HAL library to

control the peripherals.

Lab Practice 1

In this example, we will create a simple circuit consisting of a switch, SW and an led, LED.

The led will be ON when the switch is pressed down. Otherwise it will remain OFF.

Step 1

First create a directory called LedBlink (1). Next open the STM32CubeIDE by double

clicking on its icon (2).

Step 2

Browse and select the directory created in Step 1 as the workspace (1). Next click on the

Launch button (2).


Step 3

Click on Start new STM32 project.

Step 4

In the Part Number box, type stm32f103 and click on STM32F103C6 from the drop down

list.

Next click on the first row (package LQFP48) and click on the Next button.

Step 5

Name the project as LedBlink and click on the Finish button.


Step 6

When the screen shown below pops up, click on the Yes button.

Step 7

An led, LED, will be connected to pin PB0 (1). Click on this pin and from the drop down

list , select GPIO_Output (2). Notice now pin PB0 is labelled as GPIO_Output. (3)
Step 8

Click on System Core and from the drop down menu, select GPIO.

Modify the output pin name as LED (1). The other configurations should be maintained. By

default the GPIO output level is Low (2). Do not change this default level.
NOTE : There is an alternative way to label the pin. Simply left click on the pin (1). Click on

Enter User Label (2) and change the pin accordingly (3).

Repeat the same proceduce for switch SW, which will be connected to pin PB4.

Step 9

We will use the stm32f103c6 8MHz internal oscillator (HSI). The external oscillators (HSE

and LSE) may be left at their default disable status.


Step 10

Next, click on the Device Configuration Tool Code Generation.

Once done, double click on the main.c code.


Notice the various initialization functions auto-generated by the HAL libraries. The code that

need to be included for blinking the led is inside the infinite while loop.

Now we have the input pin, SW and the output pin, LED. These input and output pin

statements may be determined by typing the keyword hal_gpio followed by pressing the Ctrl

and spacebar keys.


From the list that pops up, double click on the ReadPin. Repeat for the WritePin. This will

give :

GPIOx refers to the port we are using – in this case port B (PB0 and PB4). So, we change

GPIOx to GPIOB. For the switch input, the GPIO_Pin is SW_Pin. For the LED output, this

is LED_Pin. So, we modify the above code as follow :

HAL_GPIO_ReadPin(GPIOB, SW_Pin)

HAL_GPIO_WritePin(GPIOB, LED_Pin, GPIO_PIN_RESET)

Finally, we write the intended code - when SW is pressed down, LED will be ON. We know

that SW is pressed down when it reads 0 or LOW logic. To check this, we use the if

condition :

if (HAL_GPIO_ReadPin(GPIOB, SW_Pin) == GPIO_PIN_RESET)

Also, LED will be ON when we write 0 or LOW logic to it. So, to ON LED, we write :

HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);

Note that GPIO_Pin_SET and GPIO_Pin_RESET means the pin has 1 and 0 logic

respectively.

The complete coding is shown below :


Step 11

Next, to generate the hex file to be used with Proteus, perform the following steps (click on

each highlighted part) :

NOTE : For step 5, check on the second box and click on the Apply and Close

button at the bottom left of the window.

Step 12

To build the project, click on the hammer icon as shown below :

There should be no error.


Step 13

Simulate the circuit using Proteus.

NOTE:

The following error message may pop up when we run simulation :

If that is the case, do the following :


Lab Practice Exercise

Remodify the program such that LED will blink when switch SW is pressed down.

You might also like