STM Logic 1.8 GPIO Applications
STM Logic 1.8 GPIO Applications
GPIO Applications
27/02/2014
Agenda
3V-5V interfacing
STM32F3 Electrical Characteristics
I/O Device Categories
GPIO Interfacing to External Devices
Noise margins
Important characteristics are:
VIHmin min value input recognized as a 1
VOHmin min value of output generated as a 1
VILmax max value of input recognized as a 0
VOLmax max value of output generated as a 0
Values outside the given range are not allowed.
Level Shifting
5V to 3V
All 5V families have an output voltage
swing that is large enough to drive 3V
reliably. Outputs may be as high as
3.5V for many TTL output stages, to
the full 5V for many CMOS outputs.
Therefore, as far as switching levels
are concerned, there are no problems
in interfacing from 5V to a 3V system.
3V to 5V
All 3V logic families deliver practically
the full output voltage swing of 3V, so
they can drive TTL switching levels
without problems.
However, a 3V system cannot reliably
drive a 5V one that has CMOS input
levels, even when using pull-up
resistors.
7
10
https://www.sparkfun.com/products/8745
http://www.adafruit.com/datasheets/BSS138.pdf
11
3V side
5V side
http://www.adafruit.com/products/395
http://www.ti.com/lit/ds/symlink/txb0108.pdf
12
parasitic
diode
13
http://www.siongboon.com/projects/2006-06-19_switch/
4N28
15
STM32F3
I/O structure
Free I/O
5V tolerant I/O
PIN
PA0
PA1
PA2
PA3
PA4
PA5
PA6
PA7
PA8
PA9
PA10
PA11
PA12
PA13
PA14
PA15
Str
TTa
TTa
TTa
TTa
TTa
TTa
TTa
TTa
FT
FTf
FTf
FT
FT
FT
FTf
FTf
PIN
PB0
PB1
PB2
PB3
PB4
PB5
PB6
PB7
PB8
PB9
PB10
PB11
PB12
PB13
PB14
PB15
Str
TTa
TTa
TTa
FT
FT
FT
FTf
FTf
FTf
FTf
TTa
TTa
TTa
TTa
TTa
TTa
PIN
PC0
PC1
PC2
PC3
PC4
PC5
PC6
PC7
PC8
PC9
PC10
PC11
PC12
PC13
PC14
PC15
Str
TTa
TTa
TTa
TTa
TTa
TTa
FT
FT
FT
FT
FT
FT
FT
TC
TC
TC
PIN
PD0
PD1
PD2
PD3
PD4
PD5
PD6
PD7
PD8
PD9
PD10
PD11
PD12
PD13
PD14
PD15
Str
FT
FT
FT
FT
FT
FT
FT
FT
TTa
TTa
TTa
TTa
TTa
TTa
TTa
TTa
PIN
PE0
PE1
PE2
PE3
PE4
PE5
PE6
PE7
PE8
PE9
PE10
PE11
PE12
PE13
PE14
PE15
Str
FT
FT
FT
FT
FT
FT
FT
TTa
TTa
TTa
TTa
TTa
TTa
TTa
TTa
TTa
PIN
PF0
PF1
PF2
PF3
PF4
PF5
PF6
PF7
PF8
PF9
PF10
PF11
PF12
PF13
PF14
PF15
Str
FT
FT
TTa
TTa
FTf
FT
FT
16
STM32F3DISCOVERY BOARD:
VSS : 0 volts
VDD: 3 volts
17
18
19
20
Output devices
Actuators, Displays
Voltage levels
Current draw
Sampling frequency
Noise
Input Examples
Sensors
light
force
sound
position
orientation
proximity
tactile
temperature
pressure
humidity
speed
acceleration
displacement
User input
keyboards
joysticks
mouse
keypad
switches
touchpad
dial
slider
22
Output Examples
Actuators
motors
solenoids
relays
heaters
lights
piezoelectric materials
(buzzers, linear actuator)
speakers
Displays
LED displays
LCD displays
CRT displays
indicator lights
indicator gauges
23
24
LED
http://www.farnell.com/datasheets/553533.pdf
74HC04
http://www.farnell.com/datasheets/1645614.pdf
25
390
To turn on one LED at a time for half a second in turn, one should output the
value $80, $40, $20, $10, $08,$04,$02, and $01 and stay for half a second in
each value.
26
26
// PC[7:0] configuration
GPIOC->MODER
= GPIOC->MODER
GPIOC->OTYPER = GPIOC->OTYPER
GPIOC->OSPEEDR = GPIOC->OSPEEDR
GPIOC->PUPDR
= GPIOC->PUPDR
&
&
&
&
while (1) {
for (i = 0; i < 16; i++) {
GPIOC->ODR = GPIOC->ODR & 0xFFFFFF00 | led_tab[i];
msdelay(500);
}
}
}
void delaybyms(unsigned int j) {
unsigned int k,l;
for(k=0;k<j;k++)
for(l=0;l<1427;l++);
}
gpio_test.c
27
http://homepage.cem.itesm.mx/carbajal/Microcontrollers/RESOURCES/STM32F3DISCOVERY/stm32f3discovery_sch/MB1035.pdf
28
390
VF = 1.8V
IF = 2mA
TDSL1150
Time multiplexing
technique is often used
to drive multiple
displays in order to save
I/O pins.
One parallel port is used
to drive the segment
pattern and the other
port turns on one
display at a time. Each
display is turned on and
then off many times
within a second. The
persistence of vision
make us feel that all
displays are turned on
simultaneously.
STM32
30
30
31
31
32
33
33
34
34
35
35
36
36
37
37
38
39
39
40
41
41
Driving a step motor involves applying a series of voltages to the coils of the motor.
A subset of coils is energized at a time to cause the motor to rotate one step. The
pattern of coils energized must be followed exactly for the motor to work correctly.
A microcontroller can easily time the duration that the coil is energized, and
control the speed of the stepper motor in a precise manner.
The circuit in the figure below shows how the transistors are used to switch the
current to each of the four coils of the stepper motor.
The diodes in figure below are called fly back diodes and are used to protect the
transistors from reverse bias.
The transistor loads are the windings in the stepper motor. The windings are
inductors, storing energy in a magnetic field.
When the current is cut off, the inductor dispenses its stored energy in the form of
an electric current.
This current attempts to flow through the transistor, reversely biasing its collectoremitter pair. The diodes are placed to prevent this current from going through the
transistors.
42
43
44
45
46
//
//
//
//
47
Hardware
Debouncing Techniques
VDD
Set
Reset
Set
Reset
R
(a) Set-reset latch
SR latches
Non-inverting CMOS
gates
Integrating debouncer
VDD
4050
Vout
H
Threshold level
L
Switch closed
48
48
49
GPIOC->IDR
GPIOC->MODER
keypad_dir = (keypad_dir &0xFFFF0000) | 0x00005500; // Configure lower bits[3:0] as input, bits[7:4] as output
Adapted from The HCS12/9S12: An Introduction to Software and Hardware Interfacing, HUANG (2010)
51
Example: Write a C program to read a character from the keypad shown in the figure
above.
This program will perform keypad scanning, debouncing, and ASCII code lookup.
Solution:
void delaybyms (unsigned int); // prototype
char get_key (void) {
RCC->AHBENR |= 1 << 19; // Enable GPIOC clock
GPIOC->MODER |= 0x5500; // configure PC[7:4] for output and PC[3:0] for input
while (1) {
GPIOC->ODR = 0xE0;
56
57
58
59
59
HD44780 Commands (1 of 3)
60
HD44780 Commands (2 of 3)
61
HD44780 Commands (3 of 3)
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
40
41
42
43
44
45
46
47
48
49
4A
4B
4C
4D
4E
4F
44
45
46
47
00
01
02
03
04
05
06
07
40
41
42
43
62
One can treat the LCD kit as an I/O device and use an I/O port and several
other I/O pins as control signals.
The interface can be 4 bits or 8 bits.
To read or write the LCD successfully, one must satisfy the timing
requirements of the LCD. The timing diagrams for read and write are
shown in the next two figures.
63
65
65
#define
#define
#define
#define
#define
lcdPort
lcdE
lcdRW
lcdRS
lcdCtl
GPIOD->ODR
0x40
0x20
0x10
GPIOC->ODR
//
//
//
//
//
66
//
//
//
//
//
//
//
//
67
//
//
//
//
//
//
//
//
68
69
8-bit Initialization
70
LCD Initialization
The function to configure LCD sends four commands to the LCD kit
void initlcd(void) {
// configure lcdPort port (GPIOD) as output
GPIOD->MODER
|= 0x00005555;
// configure LCD control pins (PC6, PC5, & PC4) as outputs
GPIOC->MODER
|= 0x00001500;
delayby100ms(5); // wait for LCD to become ready
cmd2lcd (0x38); // set 8-bit data, 2-line display, 5x8 font
cmd2lcd (0x0F); // turn on display, cursor, blinking
cmd2lcd (0x06); // move cursor right
cmd2lcd (0x01); // clear screen, move cursor to home
delayby1ms (2); // wait until "clear display" command is complete
}
71
72
4-bit Initialization
73