U3 - U4.1 - IOports
U3 - U4.1 - IOports
GPIO Ports
Unit - IV
Embedded C Program on Digital Interface
Referefence:
Muhammad Ali Mazidi, Sarmad Naimi, SepehrNaimi, “The stm32f103 arm
microcontroller and embedded systems: Using assembly and c", 1st Edition,
MicroDigitalEd, 2020
I/O unit in ARM
RAM Timers
PROGRAM
ROM
Program
Bus Bus
CPU
Interrupt Other
OSC I/O Port
Unit Peripherals
I/O
PINS
2
GPIO pins
PORTA (PAn) – PA0 – PA15
PORTB (PBn) – PB0 – PB15
PORTC (PCn) – PC0 – PC15
PORTD – PD0 & PD1
3
GPIO pins in STM32 Board
4
GPIO Registers
Only Ports A – D available in STM32F103C6T6
5
GPIO Registers
Register Name Size Role of the Register
Configuration registers 32 Each port bit of the GPIO Ports, can be individually
(GPIOx_CRL & GPIOx_CRH) configured by software in several modes
Data register (GPIOx_IDR) 32 The Input Data register (GPIOx_IDR) captures the data
present on the I/O pin at every APB2 clock cycle
Data register (GPIOx_ODR) 32 Data send as output on the I/O pin. It is possible to use
the output driver in Push-Pull mode or Open-Drain
mode
Bit Set/Reset Register 32 The purpose of the GPIOx_BSRR and GPIOx_BRR
(GPIOx_BSRR) registers is to allow atomic read/modify accesses to any
Bit Reset Register 16 of the GPIO registers.
(GPIOx_BRR)
Bit locking register 32 The locking mechanism allows the IO configuration to
(GPIOx_LCKR) be frozen. When the LOCK sequence has been applied
on a port bit, it is no longer possible to modify the value
of the port bit until the next reset.
6
CRL and CRH (Configuration Registers)
Output (MODE>00)
CNFx bits Configuration MODEx Direction Max speed
bits
00 General purpose output push-pull
00 Input
01 General purpose output Open-drain
01 10 MHz
10 Alternate function output Push-pull 10 Output 2 MHz
11 Alternate function output Open-drain 11 50 MHz
Input (MODE=00)
GPIOx->ODR: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
GPIOx->IDR: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Px15 Px14 Px13 Px12 Px11 Px10 Px9 Px8 Px7 Px6 Px5 Px4 Px3 Px2 Px1 Px0
9
GPIO Register
Port Bit Set / Reset Register (GPIOx_BSRR) (x=A..C)
Address offset: 0x10 and Reset value: 0x0000 0000
10
GPIO Register
Port Bit Reset Register (GPIOx_BRR) (x=A..C)
Address offset: 0x14 and Reset value: 0x0000 0000
11
Clearing pins
BRR (Bit Reset Register)
Examples:
GPIOB->BRR = 1<<5; //make PB5 low
GPIOA->BRR = (1<<3)|(1<<5); // make PA3 and PA5 low
12
Setting & Clearing Pins
BSRR (Bit Set/Reset Register)
Examples:
GPIOC->BSRR = (1<<5); //make PC5 high
GPIOB->BSRR = (1<<5)|(1<<19); /*makes PB5 high and
PB3 low */
13
Toggle Port A
#include "stm32f10x.h“
void delay_ms(uint16_t t) {
volatile unsigned long l = 0;
for(uint16_t i = 0; i < t; i++)
{ for(l = 0; l < 6000; l++); }
}
int main() {
RCC->APB2ENR |= 0xFC; //Enable the clocks for GPIO ports
int main()
{
RCC->APB2ENR |= 0xFC; //Enable GPIO ports clocks
while(1)
{
GPIOC->ODR ^= (1<<13); //toggle PC13
delay_ms(1000);
}
}
15
7-segment
16
Example: Display 3 on the 7-segment
A 7-segment is connected to PA0-PA7. Display 3 on
the 7-segment.
CRL: 3 3 3 3 3 3 3 3
ODR: 0 1 0 0 1 1 1 1 CC
STM32F10X 0
#include <stm32f10x.h> 5 1
8 6
int main() { PORTA
while(1) {
}
}
17
Example
A switch is connected to pin PB10 and an LED to pin PC13. Write a
program to get the status of SW and send it to the LED.
PB10 –Input & PC13 - Output VCC STM32F103
18
Internal Pull-up/Pull-down resistor
bit n of ODR
In CRH:CRL Pin n
0x4 = input bit n of IDR of port
(no pull-up/down)
0x8 = input with
pull-up/down CRH:CRL = 0x8
ODR = 1
Using internal pull-up
MCU MCU
VCC
VCC
Px.n Px.n
19
Example
A switch is connected to pin PB10 and an LED to pin PC13. STM32F103
21