Lab 3
Lab 3
input/output programming
(continued)
- 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
STM32F401RE:
• 6 PORT: A, B, C, D, E, H
• 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
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.
Step 2: Read reference manual to find information about appropriate registers used for programming.
2. Configure I/O direction mode on PA5 to general output mode (Using GPIOx_MODER 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)
#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.
2. Configure I/O direction mode on PA5 to general output mode (Using GPIOx_MODER 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.
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
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_status;
while (1)
{
read_status = // 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)
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)
}
DOCUMENTATIONS
- NUCLEO-F401RE_Schematic
- UM1724: User manual for STM32 Nucleo – 64 boards
- STM32F401RE datasheet
- RM0368: Reference manual for STM32F401RE