0% found this document useful (0 votes)
80 views27 pages

Embetronicx Com Tutorials Microcontrollers stm32 stm32 Gpio

Uploaded by

victor sanchez
This document provides a tutorial on using GPIO (General Purpose Input/Output) with the STM32 microcontroller. It discusses GPIO basics and features, then describes the key registers used to configure and control GPIO pins, including control registers like MODER and OTYPER to set pin direction and output type, and data registers like IDR and ODR to read input values and set output values. It also covers setting pin speed and enabling pull-ups/downs. The goal is to help readers understand GPIO programming without using HAL (Hardware Abstraction Layer) functions.

Copyright:

© All Rights Reserved

Available Formats

Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
80 views27 pages

Embetronicx Com Tutorials Microcontrollers stm32 stm32 Gpio

Uploaded by

victor sanchez
This document provides a tutorial on using GPIO (General Purpose Input/Output) with the STM32 microcontroller. It discusses GPIO basics and features, then describes the key registers used to configure and control GPIO pins, including control registers like MODER and OTYPER to set pin direction and output type, and data registers like IDR and ODR to read input values and set output values. It also covers setting pin speed and enabling pull-ups/downs. The goal is to help readers understand GPIO programming without using HAL (Hardware Abstraction Layer) functions.

Copyright:

© All Rights Reserved

Available Formats

Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 27

Home → Tutorials → Microcontrollers → STM32 → STM32 GPIO Tutorial

STM32 GPIO Tutorial


by SLR GPIO , LED Interfacing , stm32 , Switch interfacing , Tutorials STM32

Last Updated on: July 24th, 2022

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. Basically, you can write
GPIO codes in multiple ways (Using HAL, GPIO driver). Using that HAL you can finish
your job in one line of code. But I would suggest you, learn to program using the bare-
metal code (without any HAL or driver) initially. This is the STM32 GPIO Tutorial without
HAL.

No compatible source was found for this media.

X
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
You can also read, Getting started with STM32 RTOS, PIC16F877A GPIO tutorial, GPIO
Linux device driver, and STM32 GPIO RTOS tutorial.

Table of Contents 

Prerequisites

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

1. Create a New Project for STM32 in Keil


2. Understanding GPIO

STM32 GPIO Tutorial

Introduction

GPIO stands for “General Purpose Input/Output.” We are using STM32F401VE for our

examples. STM32F401VE has five ports mentioned below.

1. PORT A

2. PORT B
3. PORT C

4. PORT D
5. PORT E

Each port has 16 GPIO pins.

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
GPIO main features

Up to 16 I/Os under control

Output states: push-pull or open-drain + pull-up/down


Output data from output data register (GPIOx_ODR) or peripheral (alternate function

output)

Speed selection for each I/O


Input states: floating, pull-up/down, analog

Input data to input data register (GPIOx_IDR) or peripheral (alternate function


input)

Bit set and reset register (GPIOx_BSRR) for bitwise write access to GPIOx_ODR

Locking mechanism (GPIOx_LCKR) provided to freeze the I/O configuration


Analog function

Alternate function input/output selection registers (at most 16 AFs per I/O)
Fast toggle capable of changing every two clock cycles

Highly flexible pin multiplexing allows the use of I/O pins as GPIOs or as one of
several peripheral functions

STM32 GPIO Tutorial – Registers used in STM32


GPIO

There are a couple of registers used in GPIO. I have classified these register into 4 types

based on their operation.

1. Control Registers
2. Data Registers
3. Locking Registers

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
4. Alternate Function Registers

Control Registers

Before looking into the control register, we will see the Clock Register (RCC_AHB1ENR)
which will enable the AHB clock to the GPIO ports.

RCC_AHB1ENR

This is called as RCC AHB1 peripheral clock enable register. The register is given below.

Bit [0] – GPIOAEN: IO port A clock enable

0 – IO port A clock disabled

1 – IO port A clock enabled

Bit [1] – GPIOBEN: IO port B clock enable

0 – IO port B clock disabled


1 – IO port B clock enabled

Bit [2] – GPIOBEN: IO port C clock enable

0 – IO port C clock disabled


1 – IO port C clock enabled

Bit [3] – GPIOBEN: IO port D clock enable

0 – IO port D clock disabled

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
1 – IO port D clock enabled

Bit [4] – GPIOBEN: IO port E clock enable

0 – IO port E clock disabled


1 – IO port E clock enabled

We don’t need the rest of the bits as we are only working on GPIO.

Example

1. //These are a couple of ways of enabling AHB clock for Port A


2. SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOAEN);
3. RCC->AHB1ENR |= (1UL << 0U);

The below control registers are used to configure the GPIOs.

GPIO Port mode register (GPIOx_MODER)

GPIO Port output type register (GPIOx_OTYPER)


GPIO Port output speed register (GPIOx_OSPEEDR)
GPIO Port Pullup/Pulldown register (GPIOx_PUPDR)

GPIOx_MODER

This GPIO port mode register is used to select the I/O direction. Please find the below
image of the GPIOx_MODER register.

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Here 2-bits are combined for one particular GPIO pin.

Bits [31:0] – MODERy : Direction selection for port X and bit Y, (y = 0 … 15)

MODERy Direction Selection:

00: Input (reset state)


01: General purpose output mode
10: Alternate Function mode
11: Analog mode

In this tutorial, we are using only the I/O operation. So, we will use either Input mode or

output mode.

Example

1. //makes Port A0 as output


2. GPIOA->MODER = 0x00000001;
3.
4. //makes Port A5 as output
5. GPIOA->MODER = 0x00000400;
6.
7. //makes Port A as output
8. GPIOA->MODER = 0x55555555;
9.
10. //makes Port A as input
11. GPIOA->MODER = 0x00000000;

GPIOx_OTYPER

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
This is the GPIO output type register which is used to select the output type (Push-Pull or
Open Drain). First, we need to know what is push-pull and open drain.

Open-drain output type

I think most of them are aware of this. If you have worked on I2C you must have heard
this. But still, I will put my words. In open-drain mode, inside the microcontroller one

switch (transistor/MOSFET) is connected to the GPIO pin and the ground. So If you write
high to the GPIO pin using software, it will be connected to the ground through the switch.
Which means the original output is low. If you write low to the GPIO pin, it will be left
floating since the switch will be turned off. That’s why we are using a pullup resistor for
the open-drain pins.

Push-Pull output type

Whereas in push-pull mode, two switches (transistor/MOSFET) will be there inside of the
microcontroller. One switch is connected to Vcc/Vdd and another switch is connected to
the ground. So when you write High to the GPIO pin, the switch will be connected to the
Vcc/Vdd. The resulting output will be high (1). And if you write low to the GPIO, then the

switch will be connected to the ground. The resulting output will be low (0).

Got some idea about both output modes? Okay, let’s go to the register now. Please find
the below image of the GPIOx_OTYPER register.

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Here,

Bits [15:0] – OTy : Port output type, (y = 0 … 15)

0 – Output Push-Pull (reset state)


1 – Output open-drain

Bits [31:16] – Reserved (Must be kept at reset value).

GPIOx_OSPEEDR

This GPIO Output speed register is used to set the speed of the GPIO pin. Please find the
below image of the GPIOx_OSPEEDR register.

Here 2-bits are combined for one particular GPIO pin.

Bits [31:0] – OSPEEDRy : Speed selection for port X and bit Y, (y = 0 … 15)

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
OSPEEDRy Selection:

00: Low Speed


01: Medium speed

10: High speed


11: Very high speed

GPIOx_PUPDR

This is the GPIO port pullup/pulldown register which is used to configure the GPIO pin
into Pullup or pulldown mode. Please find the below image of the GPIOx_PUPDR register.

Here 2-bits are combined for one particular GPIO pin.

Bits [31:0] – PUPDRy : pullup/pulldown selection for port X and bit Y, (y = 0 … 15)

PUPDRy Selection:

00: No pullup or pulldown


01: Pullup
10: Pulldown
11: Reserved

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Example

1. //Enable Pullup on PA0


2. GPIOA->PUPDR = 0x00000001;
3.
4. //Enable Pullup on PA
5. GPIOA->PUPDR = 0x55555555;

Data Registers

These data registers are used to make the store the data to be output/input. The below
registers are used for output/input.

1. Input data register (GPIOx_IDR)


2. Output data register (GPIOx_ODR)
3. Bit Set/Reset register (GPIOx_BSRR)

where, x = A, B, C, D, and E.

GPIOx_IDR

This is the Input Data Register. When you configure the GPIO ports as input using
GPIOx_MODER register, this register is used to get the value from the GPIO pin. This

register is a read-only register. So you cannot write into it. Please find the below image of
the GPIOx_IDR register.

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Here,

Bits [15:0] – IDRy : Port Input Data, (y = 0 … 15)

This will be containing the corresponding value of the corresponding I/O port.
And It can be accessed in 32-bit word mode only. Which means you cannot
read a single bit. You have to read the whole register.

Bits [31:16] – Reserved (Must be kept at reset value).

Example

Let’s assume that I have configured PORT B as input, using the GPIOB_MODER register
and other control registers. Now we can read the GPIO pins like below.

1. //Reading PB0 bit


2. if( ( GPIOB->IDR & 0x01) == 0 )
3. {
4. //PORT B's 0th bit is 0
5. }
6. else
7. {
8. //PORT B's 0th bit is 1
9. }
10.
11. //Reading full PB
12. uint32_t value = GPIOB->IDR;
13.
14. //Reading PB15
15. bool value = (( GPIOB->IDR >> 15 ) & 0x1);

GPIOx_ODR

This is the Output Data Register. When you have configured GPIO Port as output using

GPIOx_MODER register, this register is used to set the value to the GPIO pin. We can read

and write to the register. Please find the below image of the GPIOx_ODR register.

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Bits [15:0] – ODRy : Port Output Data, (y = 0 … 15)

We can write to the corresponding value of the corresponding I/O port.

Bits [31:16] – Reserved (Must be kept at reset value).

Note: When you read the output data register, it will give you the last written value.

Example

Let’s assume that I have configured PORT B as output, using the GPIOB_MODER register

and other control registers. Now we can write the GPIO pins like below.

1. //Write 1 to the full Port B


2. GPIOB->ODR = 0x0000FFFF;
3.
4. //Write 0 to the full Port B
5. GPIOB->ODR = 0x00000000;

You have to be careful when you are writing the GPIO port using this GPIOx_ODR.

Because you may disturb the other Pins (bits) of the register which you don’t want to.

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Then what if I want to write a single bit without disturbing others? There is a way to do
that. Just keep reading.

GPIOx_BSRR

This is GPIO Bit Set/Reset Register. When you want to set or reset the particular bit or

pin, you can use this register. This is a write-only register. This register will do the atomic
set/reset. So we don’t worry about the interrupts that cause problems during set/reset.

And in this register, the lower 16-bits are used to set any of the 16 pins and the higher 16-

bits to clear/reset any of the 16 pins of a particular IO port. Please find the below image of
the GPIOx_BSRR register.

Bits [15:0] – BSy : Port Set Bit, (y = 0 … 15)

These bits are write-only and accessed in word, half-word, or byte mode. If

you read this register you will get 0x00000000. If you write 1 to any bit [0 to
15], it will set the corresponding bit in GPIOx_ODR register [0 to 15]. If you write

0 to any bit [0 to 15], no action will be performed to the corresponding bit in


GPIOx_ODR register [0 to 15].

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Bits [31:16] – BRy : Port Reset Bit, (y = 0 … 15)

These bits are write-only and accessed in word, half-word, or byte mode. If

you read this register you will get 0x00000000. If you write 1 to any bit [16 to

31], it will set the corresponding bit in GPIOx_ODR register [0 to 15]. If you write
0 to any bit [0 to 15], no action will be performed to the corresponding bit in

GPIOx_ODR register [0 to 15].

If you set both BSx and BRx, BSx has the priority. So BRx will be ignored. Where, x = 0…
15.

Example

Let’s assume that I have configured PORT B as output, using the GPIOB_MODER register

and other control registers. Now we can write the GPIO pins like below.

1. //set all the bits of Port B


2. GPIOB->BSRR = 0x0000FFFF;
3.
4. //Clear all the bits of Port B
5. GPIOB->BSRR = 0xFFFF0000;
6.
7. //Set the 5th bit of Port B
8. GPIOB->BSRR = (1U << 5);
9.
10. //Clear the 5th bit of Port B
11. GPIOB->BSRR = (1U << 21);

Locking Registers

This register is used to lock the configuration of the port bits. The below register is used to
do that.

GPIO Lock register (GPIOx_LCKR)

GPIOx_LCKR

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Using this register, you can freeze the GPIO configurations. Once you do the proper lock

key write sequence, it will lock the GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,


GPIOx_PUPDR, GPIOx_AFRL, and GPIOx_AFRH registers. Before we see how to do that,

let’s see that register. Please find the below image of the GPIOx_LCKR register.

This can be accessed in 32-bit word only and you can perform both read and write.

Bits [15:0] – LCKy : Port Set Bit, (y = 0 … 15)

0 – Port configuration is not locked


1 – Port configuration is locked

These bits can be written when the LCKK (16th bit) is 0.

Bits [16] – LCKK : Lock Key

0 – Port configuration lock key is not active

1 – Port configuration lock key is not active

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
This bit can be read at any time. But if we want to modify the bit, we have to

follow the Lock key write sequence. Once you have locked the GPIO, then it
will be locked until an MCU reset or a peripheral reset occurs.

Bits [31:17] – Reserved (Must be kept at reset value).

Lock key write sequence

As per the datasheet, below is the lock key write sequence.

WR LCKR[16] = ‘1’ + LCKR[15:0]

WR LCKR[16] = ‘0’ + LCKR[15:0]

WR LCKR[16] = ‘1’ + LCKR[15:0]

RD LCKR

RD LCKR[16] = ‘1’ (this read operation is optional but it confirms

that the lock is active)

Note: During the Lock key write sequence, the value of LCK[15:0] should not change.
And in some other STM32, this GPIOx_LCKR is not available for all the GPIO ports. So

you should check with the datasheet before doing this. But in the STM32F401VE,
GPIOx_LCKR register is available for all the GPIO ports.

Any error in the lock key write sequence aborts the lock. Once you have done the lock

key write sequence properly on any bit of the port, any read access on the LCKK bit will
return ‘1’ until the next CPU reset.

If you get confused, please go through the below example.

1. /*
2. ** The below full code snippet, locks the GPIO configuration of Port B
3. */
4. #define GPIO_PIN_POS (5U) //I want to lock PB5. So setting 5th bit
5. #define LCKK_BIT_POS (16U) //Position of LCKK bit
6.
7. volatile uint32_t lock_gpio = 0;
8.
9. /* Lock key write sequence */
10. /* WR LCKR[16] = ‘1’ + LCKR[5] = ‘1’ */
11. lock_gpio = (( 1UL << LCKK_BIT_POS) | ( 1UL << GPIO_PIN_POS ));

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
12. GPIOB->LCKR = lock_gpio;
13.
14. /* WR LCKR[16] = ‘0’ + LCKR[5] should not change*/
15. GPIOB->LCKR = ( 1UL << GPIO_PIN_POS );
16.
17. /* WR LCKR[16] = ‘1’ + LCKR[5] should not change*/
18. GPIOB->LCKR = lock_gpio;
19.
20. /* RD LCKR */
21. lock_gpio = GPIOB->LCKR;
22. if((GPIOB->LCKR & ( 1UL << LCKK_BIT_POS)) != 0)
23. {
24. //PB5 configuration has been locked
25. }
26. else
27. {
28. //PB5 configuration has not been locked
29. }

Alternate Function Registers

Each GPIO pin has around sixteen alternative functions like SPI, I2C, UART, etc. So we

can tell the STM32 to use our required functions.

The below-mentioned two registers are used to select the one function out of sixteen

alternative function inputs/outputs available for each I/O.

Alternate Function Low register (GPIOx_AFRL)


Alternate Function High register (GPIOx_AFRH)

GPIOx_AFRL

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
This 32-bit register is grouped by 4bits. So This GPIOx_AFRL register is used to select the
alternate functions of Pin 0 to Pin 7. Please find the below image of the GPIOx_AFRL

register.

Bits [31:0] – AFRLy : Alternate function selection for port X and bit Y, (y = 0 … 7)

AFRLy Selection:

0000: AF0 (Alternate Function 0)

0001: AF1 (Alternate Function 1)


0010: AF2 (Alternate Function 2)

0011: AF3 (Alternate Function 3)


0100: AF4 (Alternate Function 4)

0101: AF5 (Alternate Function 5)

0110: AF6 (Alternate Function 6)


0111: AF7 (Alternate Function 7)

1000: AF8 (Alternate Function 8)


1001: AF9 (Alternate Function 9)

1010: AF10 (Alternate Function 10)


1011: AF11 (Alternate Function 11)

1100: AF12 (Alternate Function 12)

1101: AF13 (Alternate Function 13)


1110: AF14 (Alternate Function 14)

1111: AF15 (Alternate Function 15)

GPIOx_AFRH

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
This 32-bit register is grouped by 4bits. So This GPIOx_AFRH register is used to select the

alternate functions of Pin 8 to Pin 15. Please find the below image of the GPIOx_AFRH
register.

Bits [31:0] – AFRHy : Alternate function selection for port X and bit Y, (y = 0 … 7)

AFRHy Selection:

0000: AF0 (Alternate Function 0)

0001: AF1 (Alternate Function 1)


0010: AF2 (Alternate Function 2)

0011: AF3 (Alternate Function 3)


0100: AF4 (Alternate Function 4)

0101: AF5 (Alternate Function 5)


0110: AF6 (Alternate Function 6)

0111: AF7 (Alternate Function 7)

1000: AF8 (Alternate Function 8)


1001: AF9 (Alternate Function 9)

1010: AF10 (Alternate Function 10)


1011: AF11 (Alternate Function 11)

1100: AF12 (Alternate Function 12)


1101: AF13 (Alternate Function 13)

1110: AF14 (Alternate Function 14)

1111: AF15 (Alternate Function 15)

As of now, we are using these pins as a GPIO and we are not selecting other functions
than Input/Output. So in our future post, we will discuss these GPIOx_AFRL and

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
GPIOx_AFRH.

I think we have covered almost all the registers. Now we will just put them all together and

make our hands dirty by playing with the LEDs. Let’s dive into the programming part.

STM32 GPIO Tutorial – LED Interfacing with STM32

In the below example, I have enabled all the Ports (A, B, C, D, and E) as an output. and
toggling them with some delay. You can also find the complete project on GitHub.

Code

1. /*********************************************************************
2. * \file main.c
3. *
4. * \details Setting all the Ports (A, B, C, D, and E) as output
5. * and toggling them with some random delay - STM32 GPIO
6. *
7. * \author EmbeTronicX
8. *
9. * \This code is verified with proteus simulation
10. *
11. **********************************************************************
12.
13. #include "stm32f4xx.h"
14.
15. #define DELAY_COUNT ( 30000 ) //delay count
16.
17. /*********************************************************************
18.
19. \details Providing Delay by running empty for loop
20.
21. \return void
22.
23. \retval none

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
24.
25. **********************************************************************
26. static void delay( void )
27. {
28. uint32_t i = 0;
29. for( i=0; i<=DELAY_COUNT; i++ );
30. }
31.
32. /*********************************************************************
33.
34. \details The main function. It should not return.
35.
36. \return void
37.
38. \retval none
39.
40. **********************************************************************
41. int main(void){
42.
43. //Enable the AHB clock all GPIO ports
44. SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOAEN);
45. SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOBEN);
46. SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
47. SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIODEN);
48. SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOEEN);
49.
50. //set all Port A to Port E as output
51. GPIOA->MODER = 0x55555555;
52. GPIOB->MODER = 0x55555555;
53. GPIOC->MODER = 0x55555555;
54. GPIOD->MODER = 0x55555555;
55. GPIOE->MODER = 0x55555555;
56.
57. //Endless loop
58. while(1){
59.
60. //Turn ON the LED of all the ports
61. GPIOA->BSRR = 0x0000FFFF;
62. GPIOB->BSRR = 0x0000FFFF;
63. GPIOC->BSRR = 0x0000FFFF;
64. GPIOD->BSRR = 0x0000FFFF;
65. GPIOE->BSRR = 0x0000FFFF;
66.
67. delay();
68.
69. //Turn OFF the LED of all the ports
70. GPIOA->BSRR = 0xFFFF0000;
71. GPIOB->BSRR = 0xFFFF0000;
72. GPIOC->BSRR = 0xFFFF0000;
73. GPIOD->BSRR = 0xFFFF0000;
74. GPIOE->BSRR = 0xFFFF0000;
75.
76. delay();
77. }
78. }

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Output

Please find the output of the example below.

STM32 GPIO Tutorial – Switch/Button interfacing with


STM32

I have connected the button to the PA0 (Port A.0) and LEDs to the PD0 to PD3. You can
also find the project on GitHub.

Code

1. /*********************************************************************
2. * \file main.c
3. *
4. * \details Setting PORT D0 to D3 as output and PORT A0 as input.
5. * When we press the Port A0, we will turn on the PD0-PD3
6. *
7. * \author EmbeTronicX
8. *
9. * \This code is verified with proteus simulation
10. *

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
11. **********************************************************************
12.
13. #include "stm32f4xx.h"
14.
15.
16. /*********************************************************************
17.
18. \details The main function. It should not return.
19.
20. \return void
21.
22. \retval none
23.
24. **********************************************************************
25. int main(void){
26.
27. //Enable the AHB clock all GPIO PORT A and PORT D
28. SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOAEN);
29. SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIODEN);
30.
31. //set Port A as input
32. GPIOA->MODER = 0x00000000;
33. //Enable Pullup on PA0
34. GPIOA->PUPDR = 0x00000001;
35.
36. //set PORTD0 to PORTD3 as output
37. GPIOD->MODER = 0x00000055;
38.
39. //Turn OFF the LEDs of PORTD
40. GPIOD->BSRR = 0xFFFF0000;
41.
42. //Endless loop
43. while(1){
44.
45. //Button is connected to PA0. So we need to check bit 0 of IDR reg
46. if( ( GPIOA->IDR & 0x01) == 0 )
47. {
48. //Turn ON the LEDs
49. GPIOD->BSRR = 0x0000000F;
50. }
51. else
52. {
53. //Turn OFF the LEDs
54. GPIOD->BSRR = 0x000F0000;
55. }
56. }
57. }

Output

Please find the output of the example below.

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Note: The code is tested with the Proteus simulation. Not with the
real hardware. If you have figured out any problems and issues with
code while testing with the hardware, Please inform us and provide
us as the workaround. So that, We will update here also. This will
help others to learn.

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
If you want to use RTOS in STM32, you can refer to the STM32 GPIO with the RTOS
tutorial.

You can also read the below tutorials.

Linux Device Driver Tutorials C Programming Tutorials

FreeRTOS Tutorials NuttX RTOS Tutorials

RTX RTOS Tutorials Interrupts Basics

I2C Protocol – Part 1 (Basics) I2C Protocol – Part 2 (Advanced Topics)

STM32 Tutorials LPC2148 (ARM7) Tutorials

PIC16F877A Tutorials 8051 Tutorials

Unit Testing in C Tutorials ESP32-IDF Tutorials

Raspberry Pi Tutorials Embedded Interview Topics

Reset Sequence in ARM Cortex-M4 BLE Basics

VIC and NVIC in ARM SPI – Serial Peripheral Interface Protocol

Bootloader Tutorials Raspberry PI Pico Tutorials

Zephyr RTOS Tutorials - STM32 Zephyr RTOS Tutorials - ESP32

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
VHDL Tutorials UDS Protocol Tutorials

Product Reviews

SLR
Embedded Software | Firmware | Linux Devic Deriver | RTOS

Hits (since 1 July 2022) - 7,511

 STM32

 GPIO , LED Interfacing , stm32 , Switch interfacing , Tutorials

Subscribe Login

Join the discussion

{} [+]

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

1 COMMENT

Oldest

Mohamed samseer Guest July 3, 2021 4:16 PM

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
hi sir my name is samseer from india. wright now i’m learning the stm32f401 baremetal
programing on the keil ide .but i have a doubt about intialization process.my doubt is why did
you mention the clock initialization and pll configuration when create a simple led program.

0 Reply

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

You might also like