Microprocessor Engineering - Sheffield Hallam University

Download Report

Transcript Microprocessor Engineering - Sheffield Hallam University

Digital I/O

A group of I/O pins is called a PORT






A port is where data enters/leaves the system.
Digital I/O pins are usually grouped into 8,16 or 32-bit
ports.
A digital I/O port is normally accessed as a single entity
through a register.
The direction (i.e. input or output) of the digital I/O pins
must be configured before they are used.
It is possible in most systems to have a mixture of inputs
and outputs on a port.
Some microcontrollers have instructions that access
individual bits of a port rather than the whole port.
3-1
LPC23XX Functional Block Diagram
The LPC2364/66/67/68
family
The LPC2368 has 70
digital I/O pins , most
pins have alternate
functions. The default
on reset is digital I/O
3-2
I/O pins and on-chip peripherals



Most I/O pins on microcontrollers have multiple
functions.
The microcontroller must be configured to select the
correct function.
The PINSEL registers control the functions of the device
pins.
TRACECLK
11
Function
select bits
(from the
PINSEL
register)
TXD1 PWM1[1]
10
01
GP I/O P2[0]
00
PIN CONNECT BLOCK
Physical I/O pin
3-3
Simple Digital I/O applications

Applications of General purpose I/O
 Driving
simple digital output devices such as LEDs, or
other indicators.
 Controlling off-chip devices.
 Sensing digital input levels, detecting signal
transitions.
 Bringing the part out of Power Down modes.
 Digital inputs can be configured to generate interrupts
(the subject of a later lecture).
3-4
LPC2368 General purpose I/O

The LPC2368 has five 32-bit Ports (numbered 0 to 4)

Not all 32 bits are implemented in each port.
See lpc23xx_um.pdf onBlackboard site
LQFP100
Package
TFBGA100
Package
3-5
LPC2368 General Purpose I/O





The PORTS are accessed through specific registers.
(These registers are memory locations that are defined
in the header file LPC23XX.h)
All the ports are accessible through FIO registers - these
uses a fast internal bus operation.
Each Port has its own set of FIO registers to setup,
control and access the port.
To maintain backwards compatibility with previous
devices PORT0 and PORT1 are also accessible by an
alternate set of registers - using a slower internal bus.
We should use always use the FIO registers not the
slower ones!
3-6
The FIO Registers





Allow direction control of individual bits.
An entire port value can be read or written in
one instruction.
Bit-level set and clear registers allow a single
instruction set or clear of any number of bits in
one port.
Mask registers allow treating sets of port bits as
a group, leaving other bits unchanged
Remember all I/O pins default to input after
reset.
3-7
Fast IO registers for PORT Access
Generic
Name
Description
Access
Reset
value
Port Register name
and address
FIODIR
Port Direction control register. This register individually
controls the direction of each port pin.
R/W
0x0
FIO0DIR - 0x3FFF C000
FIO1DIR - 0x3FFF C020
FIO2DIR - 0x3FFF C040
etc.
FIOPIN
Fast Port Pin value register. The current state of digital
port pins can be read from this register, regardless of the
pin direction or alternate function selection. Values written
to this register will be output on those port pins
configured as outputs (this is not strictly true and actually
depends on another register).
R/W
0x0
FIO0PIN - 0x3FFF C014
FIO1PIN - 0x3FFF C034
FIO2PIN - 0x3FFFC054
etc.
There are three additional registers that also influence access and
control of the port (FIOSET,FIOCLR and FIOMASK) but the above two
registers are all we need at present.
3-8
FIO Registers and Port DIRECTION

Setting port direction to OUTPUT
 Set bits in FIOxDIR register ( 1 for output, 0 for input)
 Examples:



FIO2DIR = 0x000000FF; //make PORT2 bits 0 to 7 output
FIO2DIR = 0x00000040; // make PORT2 bit 6 an output.
Note this also makes bits 0-4 and 8-31 inputs (the default
anyway)
If we want to only affect specific bits we can use masking:

FIO2DIR |= 0x00000040; // only change bit 6 using OR
FIO2DIR |= (1 << 6); // simpler?
3-9
FIO Registers and Port DIRECTION

Setting port direction to INPUT
 PORTS default to input on reset.
 Set Direction using FIOxDIR (No harm in explicitly
setting FIOxDIR)
 Examples:

FIO2DIR = 0x00000000;

FIO2DIR &= 0xFFFFFB00;

FIO2DIR &= ~0x00000400;
FIO2DIR &= ~(1<<10);

// make ALL bits inputs (the
default after a reset)
// make bit 10 an input
leave rest unchanged!
// ?
//
3-10
Polling input pins


Remember: All 32 bits are read on input
Isolate bits using masking e.g. for bit 10
 Wait



do
{
x = FIO2PIN;
} while((x & (1<<10)) == 0); //is bit 0?
while( (FIO2PIN & (1<<10)) == 0 ) //is bit 0?
while( (FIO2PIN & (1<<10)) != 0 ) //is bit 1?
 test


for a bit to change
bit
if (FIO2PIN & (1<<10) == 0)
if (FIO2PIN & (1<<10) != 0)
3-11
Using ports in our programs
1.
In the 'C' program include the special system header
file for the LPC2368 processor which defines ALL the
special register locations:
2.
3.
4.
5.
#include <LPC23xx.H>
Configure the direction for the I/O pins before they are
used (this is usually done just once at the beginning of
the program).
Use the relevant FIOxPIN register to perform the I/O.
Note the position in relation to the "="
Output FIO2PIN = expr ; // on LHS of =
Input
var = FIO2PIN;
// on RHS of =
Note: For input we usually need a variable to store the inputted value.
3-12
Program fragment
#include <LPC23xx.H>
// LPC23xx definitions
int main (void)
{
Port2Init();
while(1)
{
FIO2PIN |= 0x000000FF;
Delay();
FIO2PIN |= ~0x000000FF;
Delay();
}
}
void Port2Init(void) {
PINSEL10 = 0;
// Disable ETM interface, enable LEDs
FIO2DIR = 0x000000FF;
// P2.0..7 defined as Outputs
FIO2MASK = 0x00000000;
// Unmask all port bits
}
//Delay function not shown.
3-13