STM32F767 GPIO Tutorial using STM32CubeIDE

This is the Series of tutorials on the STM32 Microcontroller. The aim of this series is to provide easy and practical examples that anyone can understand.  In this tutorial, we are going to see the STM32F767 GPIO Tutorial using STM32CubeIDE.

This tutorial aims to help beginners to understand the basics of STM32 GPIO programming using the higher-level abstractions, making it suitable for those who want to learn the fundamentals of microcontroller interaction. If you want to learn GPIO programming without HAL, you can read this Baremetal STM32 GPIO Tutorial.

You can also read, Getting Started with STM32 RTOSSTM32 GPIO RTOS tutorial, and STM32 Bootloader Tutorial.

Prerequisites

Before starting this STM32F767 GPIO Tutorial, Please go through the below tutorials.

Hardware Requirements

STM32F767 GPIO Tutorial using STM32CubeIDE

Writing On a Port Pin

Writing data to a designated port refers to transmitting data from the controller to any connected peripheral device. Here, an LED visualizes the data leaving the parallel port.

Prototype

HAL_GPIO_WritePin (GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)

Reading from a Port

The reading operation involves retrieving data from any peripheral device. In this case, we are using a simple push-button (switch) to provide input to the port pins.

Prototype

HAL_GPIO_WritePin (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)

STM32F767ZIT6 Nucleo Board

The STM32F767ZIT6 is a high-performance microcontroller (MCU) from the STM32 family, based on the ARM Cortex-M7 core, and designed for demanding applications that require advanced processing power, high-speed I/O, and peripheral integration. It’s part of the STM32F7 series, which is known for its high performance and versatility, making it ideal for applications in fields such as industrial control, consumer electronics, automotive, and more.

This Board has Port from Port A to Port K (labeled from GPIOA to GPIOK) that provide flexible and configurable pins for a wide variety of tasks. These pins can be configured for multiple functions, such as digital input/output, analog input, or as alternate functions to interface with various communication peripherals like UART, SPI, I2C, etc.

LED Interfacing

The Nucleo board features three user LEDs connected to the PB0, PB7 and PB14 pins of PORTB, with each LED linked via a current-limiting resistor. 

Here we are using onboard LED from Port B and pin 0, Further we will see how to create a project using STM32cube IDE and to configure pin PB0 as an LED in STM32CubeIDE, follow these steps:

Project Creation

  • Open STM32CubeIDE and create a new STM32 project for your target microcontroller.
  • Once the project is created, open the .ioc file to access the configuration settings.
  • In the Pinout & Configuration tab, find the pin labelled PB0
  • Click on PB0 to configure it. In the dropdown menu, select “GPIO_Output“. This sets the pin as a digital output.
  • In the Configuration tab (on the left side), click on “GPIO” to adjust settings.
  • You can configure the output level (default is usually low), speed, pull-up/pull-down resistors, etc., as per your requirements.
  • In this tutorial I configured as No pull-up and No pull-down.  Once all the configurations are done  save with CTRL+S

STM32F767 GPIO Tutorial – Source Code

  • Open the main.c file in the Core/Src directory. In the main() function, you can toggle the LED with something like:
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);   // Turn LED on
    HAL_Delay(1000);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); // Turn LED off
    HAL_Delay(1000);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */

You can download the complete project from GitHub

Output

[To Be Added]

Push-button Interfacing

A pushbutton is a basic and widely used input device in embedded systems. It allows users to trigger various actions, such as turning an LED on or off, initiating specific processes, or interacting with the system in other ways.

On the STM32F767ZIT6 microcontroller (or any STM32-based development board), a pushbutton is typically connected to a GPIO pin. When the button is pressed, the state of the pin changes (either from high to low or vice versa, depending on the configuration).

This Nucleo board has a user button(B1) connected to Pin 13 of the PORTC(PC13). We will use this push button as a digital input to manage the onboard LEDs on the board.

Controlling LEDs with Push Button STM32F767 Discovery Board

To demonstrate, we will use a push button to control the onboard LED of the STM32 board.

  • The Pin C13 will read the input from the push button. If the state is active high, it will turn on the onboard LED of the STM32F767 Discovery board. Since the LED is connected to the PB0 pin in a No pull-up and No pull-down configuration, it will typically show an active high output.
  • When the push button is pressed, the LED will turn on; otherwise, it will stay off.
  • Now, we will see how to configure the pushbutton as input. We already know the LED configuration from the previous example.

Further, we will see how to create a project using STM32cube IDE. To configure pin PC13 as an push button in STM32CubeIDE, follow these steps:

Project Creation

  • Open STM32CubeIDE and create a new STM32 project for your target microcontroller.
  • Once the project is created, open the .ioc file to access the configuration settings.
  • In the Pinout & Configuration tab, find the pin labelled PC13
  • Click on PC13 to configure it. In the dropdown menu, select “GPIO_Input“. This sets the pin as a digital output. This sets the pin as a digital input and PC13 pin is configured with No Pull-Up and No Pull-down configuration.
  • Click on PB0 to configure it. In the dropdown menu, select “GPIO_Output“.

STM32F767 GPIO Tutorial – Source Code

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == 1)      // if Button pressed
    {
       HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);   // Turn on the LED
    }
    else
    {
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); // Turn off the LED
    }
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */

You can download the complete project from GitHub

Output

[To Be Added]

If you want to learn about other STM32 Microcontrollers GPIO Tutorials, You can read the below tutorials:

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials – STM32Zephyr RTOS Tutorials – ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL TutorialsArduino Tutorials


Discover more from EmbeTronicX

Subscribe to get the latest posts sent to your email.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Table of Contents

Discover more from EmbeTronicX

Subscribe now to keep reading and get access to the full archive.

Continue reading