0% found this document useful (0 votes)
2 views

Lab 2

This document provides an introduction to General Purpose Input/Output (GPIO) programming on the NUCLEO F401RE board, detailing the structure of GPIO, its operating modes, and the necessary registers for programming. It includes exercises for practical application, such as blinking a green LED using specific GPIO registers. Additionally, it references relevant documentation for further understanding and implementation.

Uploaded by

22022165
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 2

This document provides an introduction to General Purpose Input/Output (GPIO) programming on the NUCLEO F401RE board, detailing the structure of GPIO, its operating modes, and the necessary registers for programming. It includes exercises for practical application, such as blinking a green LED using specific GPIO registers. Additionally, it references relevant documentation for further understanding and implementation.

Uploaded by

22022165
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Lab 2: General

purpose input/output
(GPIO)
Introduction to embedded programming course
CONTENTS
1 Introduction to GPIO on NUCLEO F401RE board.

2 Registers used when programming GPIO.

3 Practice GPIO programming using registers.


INTRODUCTION TO GPIO
 What is GPIO?

- GPIO stands for General Purpose Input/Outputs, GPIO refers to the pins
of a microcontroller.
- Each pin has multiple purposes, can be configured as input to read signals,
genenal purpose output mode to control devices, alternate fuctions mode for
timer, I2C, UART,….and analog mode to use ADC.
INTRODUCTION TO GPIO
 PORT AND PIN

A pin refers to an individual pin on the microcontroller.


A port is a collection of several pins on a In STM32F401RE microcontroller, each port has 16 pins
microcontroller.
INTRODUCTION TO GPIO
 PORT AND PIN

STM32F401RE:

• 6 PORT: A, B, C, D, E, H

• Each PORT has 16 PIN (0-15


INTRODUCTION TO GPIO
 PORT AND PIN

Pin PA5 means:

• PORT A

• Pin 5
OPERATING MODES OF GPIO
OPERATING MODES OF GPIO
 INPUT floating

Input floating refers to a state where the input pin of a microcontroller is not connected to a specific
voltage level.
To ensure proper operation of STM32 microcontrollers, it is important to always connect all nput pins
to a known voltage level, either VCC or GND.=> use pull-up or pull down register.
OPERATING MODES OF GPIO
 INPUT pull-up
OPERATING MODES OF GPIO
 INPUT pull-down
OPERATING MODES OF GPIO
 Output open-drain
OPERATING MODES OF GPIO
 Output open-drain
OPERATING MODES OF GPIO
 Output push-pull
OPERATING MODES OF GPIO
 Output push-pull
OPERATING MODES OF GPIO
 Output push-pull
OPERATING MODES OF GPIO
 Output push-pull
OPERATING MODES OF GPIO
 Analog
OPERATING MODES OF GPIO
 Alternate function
PROGRAMING GPIO
 Registers
PROGRAMING GPIO
 GPIO port mode register
PROGRAMING GPIO
 GPIO port pull-up/down register
PROGRAMING GPIO
 Basic structure of a I/O port bit
PROGRAMING GPIO
 GPIO port input data register
PROGRAMING GPIO
 GPIO port input data register
PROGRAMING GPIO
 GPIO port output data register
PROGRAMING GPIO
 GPIO port bit set/reset register
PROGRAMING GPIO
 GPIO port bit set/reset register
EXERCISES
Exercise: Write a program to blink green led on NUCLEO-F401RE board (using register)

Step 1: Look up schematic of NUCLEO-F401RE to find which pin of microcontroller connect to green led.

=> Pin PA5 of microcontroller is connected with green led


EXERCISES
Exercise: Write a program to blink green led on NUCLEO-F401RE board (using register)

Step 2: Read reference manual to find information about appropriate registers used for programming.

1. Enable clock to IO Port A (Using RCC AHB1ENR register)

2. Configure I/O direction mode on PA5 to general output mode (Using GPIOx_MODER register)

3. Configure pull-up register on PA5 (Using GPIOx_PUPDR register)

4. Write alternating logic levels (1 and 0) to pin PA5 (Using GPIOx_ODR register or GPIOx_BSRR
register)
EXERCISES
Exercise: Write a program to blink green led on NUCLEO-F401RE board (using register)
Step 2: Read reference manual to find information about appropriate registers used for programming.
Example: Enable clock to IO Port A (Using RCC AHB1ENR register)

Þ To enable clock to IO Port A, need to write 1 to bit 0 of RCC_AHB1ENR register.


Þ RCC->AHB1ENR |= 1 << 0;
EXERCISES
Exercise: Write a program to blink green led on NUCLEO-F401RE board (using register)
Structure of program:
#include "stm32f4xx_hal.h"

int main(void)
{
RCC->AHB1ENR |= 1 << 0; // 1. Enable clock to IO Port A (Using RCC AHB1ENR register)
GPIOA->MODER |= ; // 2. Configure I/O direction mode on PA5 to general output mode (Using
// GPIOx_MODER register)
// 3. Configure pull-up register on PA5 (Using GPIOx_PUPDR register)
while(1)
{
//4. Write 1 to pin PA5 (Using GPIOx_ODR register or GPIOx_BSRR register to turn led on)
for (int i = 0; i < 3000000; i++);
// 5. Write 0 to pin PA5 (Using GPIOx_ODR register or GPIOx_BSRR register to turn led
off
for (int i = 0; i < 3000000; i++);
}
}
DOCUMENTATIONS
- NUCLEO-F401RE_Schematic
- UM1724: User manual for STM32 Nucleo – 64 boards
- STM32F401RE datasheet
- RM0368: Reference manual for STM32F401RE

You might also like