0% found this document useful (0 votes)
13 views20 pages

INF2004 WK2PREP GPIO Principles

The document outlines the general principles of General Purpose Input/Output (GPIO) including its necessity for connecting computers to peripherals and its operation in both hardware and software. It explains the configuration of GPIO pins as inputs or outputs, the role of buses in data transfer, and when to utilize GPIO versus specialized hardware. Additionally, it covers practical applications such as driving LEDs and connecting switches, while highlighting considerations for effective use.

Uploaded by

Andrew Koh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views20 pages

INF2004 WK2PREP GPIO Principles

The document outlines the general principles of General Purpose Input/Output (GPIO) including its necessity for connecting computers to peripherals and its operation in both hardware and software. It explains the configuration of GPIO pins as inputs or outputs, the role of buses in data transfer, and when to utilize GPIO versus specialized hardware. Additionally, it covers practical applications such as driving LEDs and connecting switches, while highlighting considerations for effective use.

Uploaded by

Andrew Koh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

s

Title: GPIO principle


e s s or I an M cL oughlin
Lecturer: Prof
Date: 2020-06
SINGAPORE
INSTITUTE OF
TECHNOLOGY

General principles
of GPIO

Professor Ian McLoughlin Thanks to


ICT Cluster Dr Muhamad Fauzi
Singapore Institute of Technology A/P Yar Kar Peo
SINGAPORE
INSTITUTE OF
TECHNOLOGY

Contents

● Why do we need GPIO and what is it used for?


● How does it work (hardware)?
● Input types and options
● Output types and options
● How does it work in software?
● When to use/when not to use GPIO
SINGAPORE
INSTITUTE OF
TECHNOLOGY

Why do we need GPIO and what is it used for?


Any time you want to connect a computer
to a peripheral, to the 'outside world', or
to another device you need I/O.

General purpose I/O pins (GPIO) can be used


under software control to connect to almost any
digital device – assuming you can write a
program to control them appropriately.
Image from: http://embedded-lab.com/blog/arduino-controlled-plant-watering-system/
SINGAPORE
INSTITUTE OF
TECHNOLOGY

Here is an example Arduino project


(automated plant watering system)
which contains a lot of I/O.

Can you identify which peripheral devices


are inputs and which are outputs?

This is the kind of system you are going to


be able to build and program.
SINGAPORE
INSTITUTE OF
TECHNOLOGY

Device 1
Microcontrollers and system-on-chip CPUs
GPIO
typically contain built-in I/O hardware. So do many
'smart' peripherals. GPIO

Microcontroller

Device 2
EIA232
When you connect them, try to use built-in I/O SPI
SPI
hardware to 'talk' to it, if possible. But if your I2C
peripheral uses a different protocol, connect it to USART

Device 3
GPIO pins, and write a program to control them. USB I2C

Sometimes you need pins to input or output a


simple signal (high/low voltage), or a particular
sequence (e.g. waveform) – then you need GPIO.
SINGAPORE
INSTITUTE OF
TECHNOLOGY

How does it work (hardware)?


Everything that a computer
does can be described by three
CPU operations:
Data bus
Address bus ● CPU reads in data from somewhere
Control bus
(any module connected to bus)
● CPU does something with the data
I/O
module ● CPU writes data to somewhere

Memory
SINGAPORE
INSTITUTE OF
TECHNOLOGY

All internal modules are connected by buses:

● Data bus – a bundle of 'wires' (e.g. 32 of them in a 32-bit


computer) that hold binary data. Modules connected to the
data bus can read what is on the bus, or write to it*

● Address bus – another bundle of 'wires', which indicates


which module is supposed to read from/write to the bus

● Control bus – tells a module to read or to write

Every bus needs a controller, usually it's the CPU.


It uses the address & control buses to 'tell' other
modules what to do.
SINGAPORE
INSTITUTE OF
TECHNOLOGY

A bit more about buses – it's all about timing


● When the CPU wants to read something, it outputs the
address it wants to read from to the address bus [short wait].
● Then it sets the control bus to indicate that a module should
output a binary word onto the data bus [longer wait].
● The module which is selected by the
address bus wakes up, finds the value
and outputs it to the data bus.
● The CPU reads the data bus then [short wait]
reset the control bus.
SINGAPORE
INSTITUTE OF
TECHNOLOGY

● Read the description of how a parallel bus works in my


Computer Systems book, section 6.1:
Interfacing using a bus
● You can access the e-book for free through the SIT library:
https://singaporetech.primo.exlibrisgroup.com/permalink/65SIT_INST/1i4buil/alma991000618027804056

(skip the bit about DMA)


SINGAPORE
INSTITUTE OF
TECHNOLOGY

More about buses – and about addresses

● Just like your home address allows a letter to be delivered to you, or


your IP address identifies your computer on a network, CPUs use
addresses to specify where data is read from or written to.
● The CPU uses the address bus to tell
all connected modules what is the
address it wants to read data from
or write data to.
SINGAPORE Top of memory
INSTITUTE OF 0xFFFF FFFF
TECHNOLOGY
● The memory map is different
for every type & variant of CPU,
Zoomed
in part but there are some common
RAM
0x28 of memory things.
map
0x24
● Everything a microcontroller
0x20 Full memory
map does involves reading from or
0x1C
writing to a location in this
0x18 Int req vector
0x14
map.
0x10 Peripherals ● Remember the address bus
0xC
0x8 S/W int vector FLASH
0x4 Undef. vector
0 Reset vector Internal
0 1 2 3 registers
0x0000 0000
byte
Bottom of memory
SINGAPORE
INSTITUTE OF
TECHNOLOGY

The CPU can read/write the I/O module


● Different addresses correspond
to registers controlling various
actions (see later).
CPU
Data bus
Address bus
● A read from an input register,
Control bus gives the state of input pins.
● A write to an output register,
I/O changes the state of output pins.
Memory module
SINGAPORE
INSTITUTE OF
TECHNOLOGY

GPIO pin direction can be set as input or output


● Input pins – a single bit of information per pin. If connected to a low
voltage, it reads as a 0, if connected to a high voltage, as a 1.
● A “port” is a bundle of pins. Often there are 8, so they can return a
byte (sometimes 16 or 32). For example:
+3.3 volts
Bit 0 = 0 If all pins on I/O port A are set
Bit 1 = 1 as inputs, reading the port
Bit 2 = 1
I/O Bit 3 = 0 will give the value:
Port A Bit 4 = 1
Bit 5 = 0 0001 0110 binary =
Bit 6 = 0 0x16 hexadecimal =
Bit 7 = 0
22 decimal
GND
SINGAPORE
INSTITUTE OF
TECHNOLOGY

● Output pins can also be arranged in a bundle, or a port.


● Each pin can either output logic 0 or logic 1 (usually meaning the
output is 0V or 3.3V)

If all pins on I/O port A


are set as active high
outputs, and connected
Bit 0 = 0 to LEDs, writing 0xAA to
Bit 1 = 1
Bit 2 = 0 the port will make the
I/O Bit 3 = 1 LEDs light up as shown.
Port A Bit 4 = 0
Bit 5 = 1
Bit 6 = 0
Bit 7 = 1
SINGAPORE
INSTITUTE OF
TECHNOLOGY

● Most microcontrollers have multiplexed pins. Each pin can be set to have
several functions. To use a pin for GPIO, first set its mode.
● Input pins can be configured with an internal resistor (or use an external one).
● Output pins can often be configured to have high/low drive strength.
● Finally, a single port can have input and output pins (along with pins that are
disabled or used for their alternative function). For example:

Bit 0 = input
Bit 1 = input
Bit 2 = output
I/O alt. Bit 3 = used for alternative function
Port A X Bit 4 = disabled
Bit 5 = output
Bit 6 = output
Bit 7 = input
SINGAPORE
INSTITUTE OF
TECHNOLOGY

Putting it all together – how does it work in software?


Imagine you connect something to a GPIO pin and want to use it in your program;

1. Find the I/O port register addresses (from the datasheet)


2. Configure the I/O port by writing to the configuration registers
GPIO/alternative function: Set pins/port for GPIO use or alternative use
Port enable register: Individual pins are enabled / disabled
Direction register: Pins are set as input / output
Pull-up resistor register: Enable / disable pull up
Drive strength register: Set o/p drive strength

3. Read or write as needed from the data register


Port data register: Read from/write to this
SINGAPORE
INSTITUTE OF
TECHNOLOGY

When to use/when not to use GPIO


● You can use GPI to connect up single inputs or outputs, you can also use it
to implement some protocol like a CAN bus, SPI bus or parallel port...
● But why would you? You can get a hardware chip that does the same thing
for very low cost.
● Often you need a driver chip anyway to convert the electrical signal that
your microcontroller uses to be compatible with the thing it is connected to.
● If you microcontroller has specialised hardware
for a protocol, better to use it – it will use less
CPU time, probably has a buffer (for more
efficient access), is less susceptible to timing
issues and can probably use interrupts.
SINGAPORE
INSTITUTE OF
TECHNOLOGY

So that's covered the general principles of GPIO – true for most types of
microcontroller. What we haven't looked at so far is;
1. How to use GPIO in a high level language like 'C'.

We use GPIO most commonly for driving LEDs as indicator lights (outputs),
and for connecting switches (inputs). On the switches there are a couple of
things we will note later;
1. How to debounce inputs.
2. How to scan a matrix of switches/buttons.
SINGAPORE
INSTITUTE OF
TECHNOLOGY

What we have covered in this segment

● Why do we need GPIO and what is it used for?


● How they work in terms of hardware
● We saw input types and options, output types and options
● We briefly considered how they work in software
● We discussed when to use/when not to use GPIO

You might also like