0% found this document useful (0 votes)
41 views39 pages

Lab 4

This document covers GPIO (General Purpose Input/Output) programming on the NUCLEO F401RE board, detailing the structure of GPIO ports and pins, their operating modes, and the necessary registers for programming. It includes exercises for blinking an LED and controlling it with a button, providing step-by-step instructions for configuring the microcontroller. Additionally, it lists relevant documentation for further reference.

Uploaded by

ngdlong1809
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)
41 views39 pages

Lab 4

This document covers GPIO (General Purpose Input/Output) programming on the NUCLEO F401RE board, detailing the structure of GPIO ports and pins, their operating modes, and the necessary registers for programming. It includes exercises for blinking an LED and controlling it with a button, providing step-by-step instructions for configuring the microcontroller. Additionally, it lists relevant documentation for further reference.

Uploaded by

ngdlong1809
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/ 39

Lab 4: General purpose

input/output programming
(continued)

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

Configure pin I/O as input with pull-up resistant.


When having no specific signal at input, default logic level is 1
OPERATING MODES OF GPIO
 INPUT pull-down

Configure pin I/O as input with pull-down resistant.


When having no specific signal at input, default logic level is 0
OPERATING MODES OF GPIO
 Output open-drain

Open-drain output has only two states: low and high impedance.
If a high level needs to be output, an external pull-up resistor is required.
When the input signal is high, the output pin is pulled low to ground; but when the
input signal is low, the output pin is in a high-impedance floating state.
OPERATING MODES OF GPIO
 Output push-pull

ush: When the input signal is low, the P-MOS is turned on, and the current flows from VDD through
to the output pin. At this time, the N-MOS is off => Output logic level is high.
ull: When the input signal is high, the N-MOS is turned on, and the current flows from the output pin
rough it to GND. At this time, the P-MOS is off => Output logic level is low.
> Output push-pull logic level output is always in two options 0 or 1
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

The Input data register is used to read the status of buttons, sensors, switches, etc.
PROGRAMING GPIO
 GPIO port output data register
PROGRAMING GPIO
 GPIO port bit set/reset register
PROGRAMING GPIO
 GPIO port bit set/reset register

The output data register and set/reset bit are used to control the status of the GPIO pin
EXERCISES
Exercise 1: 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 1: 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 1: 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 1: 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++);
}

}
EXERCISES
Exercise 2: Write a program to turn on green led on NUCLEO-F401RE board when pressing user
button and vice versa (using register)
Step 1: Look up schematic of NUCLEO-F401RE to find which pin of microcontroller connect to button.

=> Which pin of microcontroller is connected to button?


=> Which is status of that pin when pressing/not pressing button?
EXERCISES
Exercise 2: Write a program to turn on green led on NUCLEO-F401RE board when pressing user
button and vice versa (using register)
Step 1: Look up schematic of NUCLEO-F401RE to find which pin of microcontroller connect to button.

=> Pin PC13 is connected to user button.


=> When pressing button, pin PC13 has logic 0; when not pressing button, pin PC13 has logic 1 due to
external pull-up register on NUCLEO-F401RE board.
EXERCISES
Exercise 2 : Write a program to turn on green led on NUCLEO-F401RE board when pressing user
button and vice versa (using register)
Step 2: Read reference manual to find information about appropriate registers used for programming.

Configure for green led on PA5 similar as exercise 1

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)

Configure for user button on PC13

4. Enable clock to IO Port C (Using RCC AHB1ENR register)

5. Configure I/O direction mode on PC13 to input mode (Using GPIOx_MODER register)
(Don’t need to configure internal pull up on PC13 due to existing external pull on PC13 on NUCLEO-
F401RE board)
EXERCISES
Exercise 2 : Write a program to turn on green led on NUCLEO-F401RE board when pressing user
button and vice versa (using register)

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

Read status of button and turn on/turn off green led based on status of button.

6. Read status of user button (using GPIOx_IDR register)

7. If user button is pressed => turn on green led (using GPIOx_ODR register or GPIOx_BSRR
register)
If user button is not pressed => turn off green led (using GPIOx_ODR register or GPIOx_BSRR
register)
EXERCISES
Exercise 2 : Write a program to turn on green led on NUCLEO-F401RE board when pressing user
button and vice versa (using register)
Structure of program:
#include "stm32f4xx_hal.h"

#define button_pressed 0
#define button_pin 13
int main(void)
{
init_led(); //Initialize led
init_button(); //Initialize button

//Reading status of button and turn on/off led based on status of button
uint32_t read_button;
while (1)
{
read_button = (GPIOC->IDR >> button_pin) & 0x1; // using GPIOx_IDR register
if (read_status == button_pressed)
{
//Turn on green led
}
else
{
//Turn off green led
}
}
}
EXERCISES
Exercise 2 : Write a program to turn on green led on NUCLEO-F401RE board when pressing user
button and vice versa (using register)

Structure of program:

void init_led()
{
// 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)

void init_button()
{
//4. Enable clock to IO Port C (Using RCC AHB1ENR register)
// 5. Configure I/O direction mode on PC13 to input mode (Using GPIOx_MODER register)
}
EXERCISES
Exercise 3 : Write a program to turn on a led when pressing a button and vice versa
(students make the circuit themselves on the breadboard)

Component required:

-1 button
- 1 led
- 2 resistor (220 ohm)
- 1 breadboard
EXERCISES
Exercise 3 : Write a program to turn on a led when pressing a button and vice versa
(students make the circuit themselves on the breadboard)

Schematic:
EXERCISES
Exercise 3 : Write a program to turn on a led when pressing a button and vice versa
(students make the circuit themselves on the breadboard) (expand to using 3 led and 3
button)
Circuit:
EXERCISES
Exercise 3 : Write a program to turn on a led when pressing a button and vice versa
(students make the circuit themselves on the breadboard) (expand to using 3 led and 3
button)
Circuit: #include "stm32f4xx_hal.h"

#define button_pressed 0
#define button1 5
#define button2 6
#define button3 7

int main(void)
{
init_led(); //Initialize led
init_button(); //Initialize button

//Reading status of button and turn on/off led based on status of button
uint32_t read_button1, read_button2, read_button3;
while (1)
{
read_button1 = (GPIOC->IDR >> button1) & 0x1; // using GPIOx_IDR register
if (read_button1 == button_pressed)
{
//Turn on red led
}
else
{
//Turn off red led
}

//Code for reading button2 to control green led


//Code for reading button3 to control yellow led

}
}
EXERCISES
Exercise 3 : Write a program to turn on a led when pressing a button and vice versa
(students make the circuit themselves on the breadboard) (expand to using 3 led and 3
button)
Circuit:

void init_led()
{
//Enable clock to port A
//Enable clock to port B
//Configure PA12 as output, PA12 connected with red led
//Configure PA11 as output, PA11 connected with green led
//Configure PB12 as output, PB12 connected wih yellow led
}

void init_button()
{
//Enable clock to port A
//Configure PA5, PA6, PA7 as input
}
DOCUMENTATIONS
- NUCLEO-F401RE_Schematic
- UM1724: User manual for STM32 Nucleo – 64 boards
- STM32F401RE datasheet
- RM0368: Reference manual for STM32F401RE

You might also like