Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Accessing-Reading and Writing to whole gpio ports of stm32 microcontrollers

By EG Projects August 6, 2019

This tutorial is about reading and writing to whole gpio port of stm32 microcontrollers. While working with stm32f103 microcontroller using stm32cubemx codeconfigurator ide and ARM keil uvision-5 ide with HAL libraries I noticed that the examples provided in the HAL libraries did not contain any example which explains how to access individual ports of stm32 microcontrollers? I googled to find out any online tutorial on accessing the stm32 ports directly but ended up with no resourceful guide. So i decided to write this tutorial for the newbies and fill up the gap of writing and reading the individual ports of stm32 microcontrollers using HAL libraries.
I was interfacing 16×2 character lcd with stm32f103 microcontroller. 16×2 lcd can be interfaced with any microcontroller in 8 and 4 bit mode. The libraries that i was able to find for stm32 to interface 16×2 lcd with it can be found at https://stm32f4-discovery.net/2014/06/library-16-interfacing-hd44780-lcd-controller-with-stm32f4/ . The library works but it depends on many other custom libraries that are written by author himself. One thing which is disturbing in the library is, to write 4 bit data author is manipulating individual port pins of stm32 microcontroller one by one. 4-bit data or 8-bit can easily be written in just single iteration if using ports. 

How to access individual stm32 ports and write to them?

I could not find any official stm32 document that explains the registers associated with stm32 microcontrollers. Some tutorials are present on internet that explains some registers but not all the stm32 registers. These tutorials were made with SPL(Standard peripheral libraries) that are no more recommended by the stmicroelectronics. So i think that previously stm has released some information about the registers associated with the stm32 microcontrollers, but with the release of stm32cubemx code configurator  they hide the registers part.
ODR(Output Data Register)​
ODR is output data register of stm32 microcontrollers. It is a 16 bit register and it can be read and write to. Each stm32 microcontroller port has its own ODR register. So if your microcontroller has three ports A,B and C then it must has three ODR registers associated with each port. Each bit of ODR register represents the individual port bits.
​ODR regsiter can be accessed with the statement. In example port-c of stm32 microcntroller is accessed.
 

GPIOC->ODR

To write to the port the statement is simple. We can write data in hex form and in binary form. The examples are below.

GPIOC->ODR = 0xF0FE
GPIOC->ODR = ​0b1111000011111110

We can also write to the individual pins with ODR but that is not useful if we are using HAL libraries. Since HAL has predefined statements that can easily read and write to individual pins. Such as 

HAL_GPIO_TogglePin(Led_GPIO_Port,Led_Pin);  

Let us look at the definition of the upper function in the HAL libraries. The definition is present in the stm32f1xx_hal_gpio.c function. In the definition, to write to pin and toggle the pin ODR register is accessed.
Picture

A simple stm32 gpio port accessing(reading writing) project

Stm32 accessing ports directly

Stm32 accessing ports directly

Lets now do a practical. I am going to access the stm32f103 port-b and write to its pins 0 and 1 using ODR register. At the output i connected 2 leds so the output binary pattern can easily be recognized. Since 2 pins are used so the binary pattern will be 00, 01,10 and 11. 
If you don;t know how to initialize the stm32 gpio pins and ports here is a simple tutorial on initialing the stm32f103 microcontroller pins using stm32cubemx and generating code for keil arm ide.

Stm32Cubemx initializing code

After initializing the pins using stm32cubemx and generating code for keil arm. Its time to add the user code. The code will be like this. 

GPIOB->ODR = 0x0001;                              //Write to port-b
HAL_Delay(1000);                                       //Delay 1second
GPIOB->ODR = 0x0002;                              //Write to port-b
HAL_Delay(1000);                                       //Delay 1second
GPIOB->ODR = 0x0003;                              //Write to port-b
HAL_Delay(1000);                                       //Delay 1second
GPIOB->ODR = 0x0000;                              //Write to port-b
HAL_Delay(1000);                                        //Delay 1second

GPIOB->ODR = 0x0001; means (0b0000000000000001) pin#0 is high; 0x0002 means pin#1 is high; 0x0003 means pin#0 and 1 are high.
Download the project code and please give us your feed back on the tutorial. Project video is on the right hand side.

Watch the project video here

Project Code


Filed Under: Microcontroller Projects, STM32

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Reducing "shoot-through" in offline Full Bridge SMPS?
  • High Side current sensing
  • How to simulate power electronics converter in PSpice?
  • Voltage mode pushpull is a nonsense SMPS?
  • Layout IRN reduction in Comparator

RSS Electro-Tech-Online.com Discussions

  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors
  • Siemens large industrial PLC parts

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe