100% found this document useful (1 vote)
76 views12 pages

LED Blinking Lab with STM32F103C6

The document describes how to create a simple LED blinking circuit using an STM32 microcontroller in Proteus. Key steps include: 1. Configuring the microcontroller pins for an input switch and output LED in STM32CubeMX. 2. Writing code in STM32CubeIDE to read the switch pin and toggle the LED pin when the switch is pressed. 3. Generating a hex file and simulating the circuit in Proteus to test the LED blinking functionality.

Uploaded by

newstud 2022
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
76 views12 pages

LED Blinking Lab with STM32F103C6

The document describes how to create a simple LED blinking circuit using an STM32 microcontroller in Proteus. Key steps include: 1. Configuring the microcontroller pins for an input switch and output LED in STM32CubeMX. 2. Writing code in STM32CubeIDE to read the switch pin and toggle the LED pin when the switch is pressed. 3. Generating a hex file and simulating the circuit in Proteus to test the LED blinking functionality.

Uploaded by

newstud 2022
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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.

Common questions

Powered by AI

To generate a hex file for Proteus simulation in STM32CubeIDE, perform the following: configure the project within STM32CubeIDE as desired, complete the code, and build the project using the hammer icon to fix any errors. After ensuring a successful build, export the compiled code as a hex file by navigating through the export options and following specific instructions such as checking appropriate boxes and proceeding with 'Apply and Close'. This hex file can then be imported into Proteus for simulation .

The HAL (Hardware Abstraction Layer) library simplifies code development for STM32 microcontrollers. It abstracts the hardware specifics, providing initialization functions and API calls that facilitate peripheral control. In STM32CubeIDE, this library auto-generates the necessary functions to initialize peripherals, set GPIO outputs, and read/write pin states, making application development more efficient and portable across various STM32 microcontrollers .

To configure the STM32F103C6 microcontroller for LED control using STM32CubeIDE, first create a new workspace and project named LedBlink. Use STM32CubeMX to set pin PB0 as GPIO_Output for the LED and PB4 for the switch (SW) input. Set the microcontroller to use the internal 8MHz oscillator and maintain default GPIO settings. Use HAL libraries for code generation and manipulation, specifying the ReadPin and WritePin functions to interact with GPIOB for reading the switch status and controlling the LED state .

Errors during Proteus simulation can be resolved by revisiting and verifying the code and configuration settings within STM32CubeIDE. This includes double-checking the hex file's accuracy, ensuring that the microcontroller and peripheral configurations are consistent, and that there are no discrepancies between the hardware setup in Proteus and real-world specifications. Additionally, debugging steps such as correcting any wrong pin configurations or missing library functions within the project can address common simulation issues .

STM32CubeMX significantly enhances development efficiency by providing a graphical interface for configuring microcontroller settings, such as pin assignments and peripheral setups. It generates initialization code using the HAL library, ensuring consistent and error-free setup across various projects. This utility facilitates easier migration and configuration changes, aligning with different project requirements without manually coding low-level hardware details .

Using the STM32F103C6's 8MHz internal oscillator simplifies the circuit design by eliminating the need for external components like crystal oscillators, reducing circuit complexity and cost. Internal oscillators provide sufficient precision for basic applications like LED blinking without the additional power consumption or potential instability issues associated with external oscillators when high precision is unnecessary .

The infinite while loop plays a critical role in periodically checking the status of the switch and updating the LED state in the STM32F103C6 project. Within the loop, HAL_GPIO_ReadPin continuously monitors the switch input, and conditional statements determine if the LED should be turned on or off. This structure ensures that the microcontroller constantly appraises and reacts to GPIO changes, achieving the desired blinking operation in response to the switch .

The GPIO pin status controls the LED by reading the input from switch SW and adjusting the LED output accordingly. If the GPIO pin for the switch reads a LOW logic (0), indicating the switch is pressed, the code writes a LOW logic to the LED pin, turning the LED on. This decision-making process is enabled by HAL functions like HAL_GPIO_ReadPin and HAL_GPIO_WritePin, which check the switch status and update the LED state using GPIO_PIN_RESET to turn the LED on, following embedded conditional logic .

In the STM32 project, the main.c file includes auto-generated initialization functions and the while loop necessary for LED and switch operations. It uses HAL library functions to configure GPIO pins, read the switch status, and control LED output. The main.c file's infinite loop checks the switch state using HAL_GPIO_ReadPin and updates the LED state using HAL_GPIO_WritePin based on the switch input, ensuring continuous monitoring and control of the hardware .

For the LED to turn on using the STM32F103C6 microcontroller, the switch connected to pin PB4 must be pressed, indicating a LOW logic level (0) read by the microcontroller. The code, therefore, checks if HAL_GPIO_ReadPin returns GPIO_PIN_RESET. If true, it sets the LED pin to GPIO_PIN_RESET, writing a LOW logic to the LED pin, thus turning the LED on .

You might also like