Tutorials for STM32F103RB
Nicolas Barbot
2016
Tutorials for STM32F103RB 2
• Create an OpenSTM32 Project
• Create a STM32CubeMX Project
• GPIO
• UART
• Timer
• Analog to Digital Conversion
• Digital to Analog Conversion
• FLASH
Tutorials for STM32F103RB 3
• Ressources
• Nucleo F103RB
• http://www.st.com/content/st_com/en/products/evaluation-tools/product-evaluation-
tools/mcu-eval-tools/stm32-mcu-eval-tools/stm32-mcu-nucleo/nucleo-f103rb.html
• This page contains user manual UM1724 describing the hardware of Nucleo boards
• STM32F103RB
• http://www.st.com/content/st_com/en/products/microcontrollers/stm32-32-bit-arm-cortex-
mcus/stm32f1-series/stm32f103/stm32f103rb.html
• This page contains reference manual RM0008 describing STM32F103RB
• STM32 Cube F1
• http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-
software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef1.html
• This page contains UM1850 describing the HAL library
• Open STM32
• http://www.openstm32.org/HomePage
• Open STM32 is a free Eclipse plugin for programming STM32 MCU (registration needed).
• Eclipse
• https://www.eclipse.org
• Eclipse is the IDE software
Using GPIOs
Using GPIOs 5
• Goals:
• To identify a GPIO pin from the datasheet
• To configure and use GPIO with HAL drivers (input, output, interruption)
• To evaluate the performance of GPIO pin (frequency)
• Requirement
• Create an OpenSTM32 Project
Identify a GPIO pin 6
• Open User Manuel 1724 « STM32 Nucleo-64 boards » [1]
• This document describes the hardware layout of each Nucleo boards.
• Table 11 (page 37) presents the correspondance between Nucleo pins (on the
extension connectors) and STM32 pins.
• Nucleo boards provide a user led called LD2
• LD2 is connected to D13 on the Arduino connector (see LED p.22)
• Table 11 indicates that D13 corresonds to PA5 for STM32F103RB
• PA5 stands for Port A Pin 5
• In this tutorial, we will configure the STM32 to blink LD2 with a period
of 1 s
Create the Project 7
• Create a new project for STM32F103RB using HAL library
• See “CreateOpenSTM32Project” presentation
• Add the SystemClock_Config function in main.c
• Set HCLK to 64 MHz
• Call SystemClock_Config and HAL_Init function in main().
USING GPIO 8
• Open User Manuel 1850 « Description of STM32F1xx HAL drivers » [3]
• Chapter 18 (p. 247) describes the use of GPIO with HAL drivers
• Declare a GPIO_InitTypeDef structure in main() function
• Fill up the structure fields:
Mode Pull Speed Pin
Input No pull Low 0
Analog Pull up Medium 1
Output Pull down High 2
Alternate F. …
Interrupt 15
• Set the fields to GPIO_MODE_OUTPUT_PP, GPIO_NOPULL,
GPIO_SPEED_FREQ_LOW, GPIO_PIN_5
• Call the macro __HAL_GPIOA_CLK_ENABLE() to activate port A
• Configure the GPIO pin(s) using HAL_GPIO_Init()
Using GPIO 9
• You can now drive your GPIO pin with the function HAL_GPIO_WritePin() or
HAL_GPIO_TogglePin().
• Write a infinite loop to blink LD2 every 1 seconds.
• Your main() function should look like this:
• Build your project and run your program on the board
Evaluate the performance of GPIO 10
• In this exercise, we want to toggle D13 (on the Arduino connector) at the
maximum frequency
• Identify port and pin number of D13
• Create a new project (or modify the previous one)
• Configure the pin corresponding to D13 as an output
• In an infinite loop, toggle D13 (without delay)
• Build the project
• Observe D13 with an oscilloscope and determine the maximum frequency
Activate LED when button is pushed 11
• In this exercise, we want to activate LD2 when B2 (user button) is pressed
• Identify port and pin number of B2 (and LD2)
• Create a new project (or modify the previous one)
• Configure the pin corresponding to LD2 as an output
• Configure the pin corresponding to B2 as an input
• In an infinite loop, read the state of the pin corresponding to B2 and write this state
to the pin corresponding to LD2
• Build the project and debug your program
Generate INT when button is pressed 12
• In this exercise, we want to generate an interruption when B2 (user button) is
pressed to switch on LD2
• Identify port and pin number of B2 (and LD2)
• Create a new project (or modify the previous one)
• Configure the pin corresponding to LD2 as an output
• Configure the mode of B2 pin to GPIO_MODE_IT_FALLING
• Configure the priority interruption with HAL_NVIC_SetPriority() function
• Enable the interruption with HAL_NVIC_EnableIRQ() function
• Each interruption calls EXTI15_10_IRQHandler() (defined in
startup_stm32f103xb.s) which can be overloaded
• Overload the function EXTI15_10_IRQHandler() to call
HAL_GPIO_EXTI_IRQHandler() function
• Write the callback function HAL_GPIO_EXTI_Callback() to switch on the LED
• Build the project and debug your program
Conclusion 13
• Thanks to this tutorial, you are now able to:
• Use any GPIO as output
• Use any GPIO as input
• Use any GPIO for generating an interruption