Engineers Garage

  • Projects and Tutorials
    • Circuit Design
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • EE Design News
    • DIY Reviews
    • Guest Post
    • Sponsored Content
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key 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
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • Video
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

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

August 6, 2019 By EG Projects

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

Related Articles Read More >

TV remote hack using Arduino and IR sensor
Gesture sensor using Arduino
How to build a metal detector using an inductive proximity sensor with Arduino
Object follower robot using Arduino

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!


Featured Tutorials

  • Getting Started with the ESPlorer IDE
  • SENDING TEXT MESSAGE USING ESP8266
  • CONNECTION BETWEEN TWO ESP8266
  • ESP8266 WIFI HOTSPOT
  • HOME AUTOMATION USING ESP8266
  • Open WiFi Scanner using Esp8266

Stay Up To Date

Newsletter Signup

EE Training Center Classrooms

“ee

“ee

“ee

“ee

“ee

Recent Articles

  • TV remote hack using Arduino and IR sensor
  • Gesture sensor using Arduino
  • Diodes adds to its family of voltage-level shifters
  • Xilinx expands UltraScale+ portfolio to include compact, intelligent edge solutions
  • New Armv9 architecture designed to increase security and AI capabilities

RSS EDABOARD.com Discussions

  • Help with damaged board
  • Sine Wave Generation with SPI and TIM interrupt in STM32
  • PIC BASIC
  • 4 switch buck boost converter
  • Question about hexagonal patch antenna frequency and design

RSS Electro-Tech-Online.com Discussions

  • Can MT8870 DTMF decoder module handles none audible signals?
  • What has happened to EEVBLOG videos?
  • Funny Images Thread!
  • Cell phone detector
  • Adjustable 0-5v ground switch
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2021 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 | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Circuit Design
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • EE Design News
    • DIY Reviews
    • Guest Post
    • Sponsored Content
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key 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
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • Video
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering