Beruflich Dokumente
Kultur Dokumente
Textbook Sections:
Chapter 2, Section 2.7 (switches/LEDs)
Chapter 4, Sections 4.1, 4.2 (I/O ports and programming)
STM32F407 Reference Manual, Chapter 8 (general-purpose IOs)
STM32F407VG
Microcontroller
ARM CortexTM-M
processor
Input
ports
PPB
Internal
peripherals
Advanced
High-perf
Bus
Instructions
Flash ROM
ICode bus
DCode bus
Output
ports
Data
RAM
RISC machine
Pipelining effectively provides single cycle operation for many
instructions
Thumb-2 configuration employs both 16 and 32 bit instructions
Bard, Gerstlauer, Valvano,Yerraballi
Cortex-M4 processor
ST Board
Debugger
Program memory
Data memory
I/O devices
ST Microelectronics
STM32F40x
microcontroller
Slaves
Cortex-M4
memory map
Program code,
Constant data
One-time
programmable
CPU
System bus
Interface
Data link
Device
Tristate Gate
G=0
A
G
Output enabled
(Y = A)
A
G
G=1
Output disabled
(Y = high impedance)
A
+3.3V
+3.3V
+3.3V
T3
74HC244
8
8
G
A
T5
T6
T4
Y
T1
T7
G
T2
T8
Bard, Gerstlauer,
Valvano, Yerraballi
Open Collector
A
Vcc
B 74LS05
20k
Vcc
74HC05
8k
B
A
Q
n-type
Q
1
Q2
4.5k
A
Low
High
Q2
B
off
HiZ
active Low
+3.3V
TM4C
Input port
+3.3V
TM4C
10k
s
Input port
0.0V
Pressed
+3.3V
TM4C
10k
s
Input port
3.3V
Not pressed
Input port
10k
Positive logic
Negative logic
Negative Logic s
pressed, 0V, false
not pressed, 3.3V, true
TM4C
Positive Logic t
pressed, 3.3V, true
not pressed, 0V, false
+3.3V
t
TM4C
3.3V
Input port
10k
Pressed
+3.3V
t
TM4C
0V
Input port
10k
Not pressed
Bard, Gerstlauer,
Valvano, Yerraballi
Switch debouncing
A switch must be debounced to multiple contacts caused by
+V
1
10K
Pull-up
Resistors
+5v
Pressed
Key
LED Interfacing
anode
cathode
anode (+)
cathode (-)
big voltage connects to big pin
Bard, Gerstlauer,
Valvano, Yerraballi
LED Configuration
Current
2
+ a
(mA) 1
voltage
Out
LM3S
or
TM4C
high
R
1mA
LED
Out
+3.3V
R
LM3S
or
TM4C
1mA
LED
low
Bard, Gerstlauer,
Valvano, Yerraballi
LED interfaces
Positive logic, low current
high
Out
R
2mA
TM4C
LED
3.3 V
TM4C
R
2mA
LED
Out
low
LED Interfacing
R = (3V 1.5)/0.001
= 1.5 kOhm
Out
LM3S
or
TM4C
high
R
1mA
R = (5.0-2-0.5)/0.01
= 220 Ohm
LM3S
or
TM4C
LED
Out
+5V
R
7406
+5V
high
0.5V
10mA
LED
f
Common anode or
Common cathode
Data[6:0]
b
g
c
e
c
e
d
Select digit
Push buttons
User (blue button) PA0
Reset (black button) NRST pin
LED3
LED4
LED5
LED6
expansion buses
Each port is configured and accessed via one or more registers
Each register is assigned a unique memory address
CPU reads/writes data via data registers
Mode Register (or data direction register) for each port determines
GPIOA_ODR
Bus
GPIOA_IDR
n
Input/Output Port
GPIOA_MODER
Bard, Gerstlauer,
Valvano, Yerraballi
STM32F4xx microcontroller
General-Purpose I/O (GPIO) ports
Up to 144 GPIO pins, individually configurable
IDR
BSRR
ODR
Alt. Function
PIN
x = AI
31
7 6
Pin 3
5 4
Pin 2
3 2
Pin 1
Pin value captured in IDR every bus clock (through Schmitt trigger)
11 Analog mode:
Disable output buffer, input Schmitt trigger, pull resistors
(so as not to alter the analog voltage on the pin)
1 0
Pin 0
x = AI
GPIOx_IDR
Data input through the 16 pins
Read-only
GPIOx_ODR
Write data to be output to the 16 pins
Read last value written to ODR
Read/write (for read-modify-write operations)
C examples:
GPIOA->ODR = 0x45;
N = GPIOA->IDR;
C examples:
GPIOA->BSRRL = (1 << 5); //set bit 4 of GPIOA
GPIOA->BSRRH = (1 << 5); //reset bit 5 of GPIOA
GPIOx_AFRL
(low pins 0..7)
GPIOx_AFRH
(high pins 8..15)
GPIOx_PUPDR pull-up/down
00 no pull-up/pull-down (reset state)
01 pull-up
10 pull-down
GPIOA
GPIOB
GPIOC
GPIOD
GPIOE
=
=
=
=
=
0x4002
0x4002
0x4002
0x4002
0x4002
0000
0400
0800
0C00
1000
GPIOF
GPIOG
GPIOH
GPIOI
=
=
=
=
0x4002
0x4002
0x4002
0x4002
1400
1800
1C00
2000
To set bits
The or operation to set bits 3-0 of GPIOD_MODER, to select analog mode for
pins PD1 and PD0.
( The other 28 bits of GPIOD_MODER are to remain constant.)
Friendly software modifies just the bits that need to be.
GPIOD_MODER |= 0x0F; // PD1,PD0 analog
Assembly:
LDR R0,=GPIOD_MODER
LDR R1,[R0]
; read previous value
ORR R1,R1,#0x0F ; set bits 0-3
STR R1,[R0]
; update
c7 c6 c5 c4 c3 c2 c1 c0
0
c7 c6 c5 c4 1
value of R1
0x0F constant
result of the ORR
To clear bits
The AND or BIC operations to clear bits 3-0 of GPIOD_MODER to select input
mode for pins PD1 and PD0. (Without altering other bits of GPIOD_MODER.)
Friendly software modifies just the bits that need to be.
GPIOD_MODER &= ~0x0F; // PD1,PD0 output
Assembly:
LDR
LDR
BIC
STR
R0,=GPIOD_MODER
R1,[R0]
; read previous value
R1,R1,#0x0F ; clear bits 3-0
R1,[R0]
; update
c7 c6 c5 c4 c3 c2 c1 c0
value of R1
c7 c6 c5 c4 0
To toggle bits
The exclusive or operation can also be used to toggle bits.
GPIOD_ODR ^= 0x80; /* toggle PD7 */
Assembly:
LDR
LDR
EOR
STR
R0,=GPIOD_ODR
R1,[R0]
; read port D
R1,R1,#0x80 ; toggle state of pin PD7
R1,[R0]
; update
b7 b6 b5 b4 b3 b2 b1 b0
value of R1
0x80 constant
~b7 b6 b5 b4 b3 b2 b1 b0
R0,=GPIOD
R1,#0x0080
R1,[R0,#BSSRH]
R1,#0x0400
R1,[R0,#BSSRL]
;
;
;
;
;
Alternative: write concurrently to BSSRH and BSSRL (as one 32-bit register)
LDR R0,=GPIOD
; GPIOD base address
MOV R1,#0x0400
; select PD10 in BSSRL
MOVT R1,#0x0080
; select PD7 in BSSRH
STR R1,[R0,#BSSR]
; PD10=0 and PD7=1
Switch Interfacing
Not
pressed
+3.3V
Pressed
10k
Open
LM3S or
TM4C
Input port
+3.3V
t
LM3S or
TM4C
Input port
10k
Closed
Negative logic
Positive logic
1 0
a6 0
0
0
0
0
0
0
0
0
0
0
value of R1
0x40 constant
result of the AND