PIC Microcontroller

An Introduction to Embedded Systems
Aug 2008
Lecture 3
Digital Input/Output and the 16F84A
The aims of this lecture are to explore:
• why we need parallel input/output;
• how simple logic circuits can be developed to give a flexible interface
between the microcontroller data bus and the outside world;
• how external devices can be connected to the parallel port;
• the parallel input/output available on the PIC 16F84A.
For further
information
and
background,
read
Pages 45-58.
The copyright to all diagrams is held by Microchip
Technology, or T. Wilmshurst, unless otherwise stated
Why Digital Input/Output?
Almost any embedded system needs to transfer digital data between its CPU and the
outside world. This transfer falls into a number of categories, which can be summarised
as:
Direct user interface, including switches, keypads, light emitting diodes (leds) and
displays;
Input measurement information, from external sensors, possibly being acquired through
an analog to digital converter;
Output control information, for example to motors or other actuators;
Bulk data transfer to or from other systems or sub-systems, moving in serial or parallel
form, for example sending serial data to an external memory.
How can we provide the required interface between the microcontroller core and the
outside world? More precisely, how do we get the data onto or off the data bus at the
right moment?
Parallel Input/Output 2
We could apply a circuit like this for output.
Here a pulse on the Port Select line captures data on the bus at that instant, and
transfers it to the external pin.
Two lines of
Data Bus
Read/Write
D
Q
External Pin
Port Select
Flip-flop latches data bus value onto
external pin, when memory location
is selected, AND Write is active
high whenever
port address is
selected
D
Q
External Pin
Parallel Input/Output 2
Or we could apply a circuit like this for input.
Here a pulse on the Port Select line transfers data
on the external pin at that instant to the data bus.
Two lines of
Data Bus
Read/Write
Port Select
External Pin
buffer transfers logic value on external pin
onto data bus line, when memory location
is selected, AND Read is active
External Pin
A Bi-Directional
Port Pin Driver
Circuit
Read/Write
Read Port
Or, we could combine both
circuits into one multifunction circuit, like this. You
don’t need to grasp all the
detail of this circuit, although
it’s neat if you can.
There is now an extra flipflop, labelled “Direction”.
The state of this decides in
which direction data will
flow.
Data Bus
(bit n)
Input Buffer
I/O Pin
"Data"
Port Select
Write
Port
D
Q
Output Buffer
holdsbitbit
holds
output value
88 of
of these
these
flip-flops
form
flip-flops
form
the "Data" SFR
"Direction"
Write
DDR
Direction Select
D
Q
determineswhether
whetherport
port
determines
bit
is
input
or
output
bit is input or output
Alternate Input
Function
The two flip-flops shown can
each form one bit in an SFR,
which can be controlled from
the CPU.
"Data" SFR
A group of these bits, each
driven by a circuit like this, is
called a “port”
"Direction" SFR
(bit n of an
8-bit port)
8 of these flip-flops form
the "Data Direction" SFR
buffer, enabled
when pin is output
Parallel Input/Output Ports 3
The 16F84A pins RB3:RB0
Holds output data
From Option Register
Decoded address lines
Value held determines data
direction
0 on this line enables output buffer
Output buffer
SFRs for the 16F84A Parallel Ports
The Port SFRs can be seen in the memory map,
the detail repeated here.
SFR named PORTX holds the input/output data
for the port, ie it holds all the “Data Latch” bits
for that port.
The SFR named TRISX holds all the “TRIS
Latch” bits for that port. The bits can be set
independently, so one can be input while another
output. They cannot be both at the same time.
Ports A and B can easily be found on
the pin connection diagram. Port A is
only 5 bits, while Port B is 8.
Note that some pins have several
functions, as indicated on the diagram.
Port A, Bit 2
Port A, Bit 3
RA2
RA3
1
18
*Port A, Bit 4 RA4/T0CKI
Reset
Ground
**Port B, Bit 0
RA1
RA0
Port A, Bit 1
Port A, Bit 0
OSC1/CLKIN
Oscillator connections
OSC2/CLKOUT
VDD
Supply voltage
MCLR
VSS
RB0/INT
RB7
Port B, Bit 7
Port B, Bit 1
RB1
RB6
Port B, Bit 6
Port B, Bit 2
RB2
RB5
Port B, Bit 5
Port B, Bit 3
RB3
RB4
Port B, Bit 4
*also Counter/Timer clock input
**also external Interrupt input
9
10
Port Input Characteristics
When designing with microcontroller digital I/O one needs to have an understanding of
their electrical characteristics.
16F84A Input Characteristics
The input of a logic gate or port pin requires the voltage to be below a certain maximum
in order to be recognised as a logic 0, or above a certain minimum to be recognised as a
logic 1.
Minimum Input High Voltage, VIH
Maximum Input Low Voltage, VIL
Input Leakage Current, IIL
2.4V
0.8V
+1mA
(TTL buffer inputs)
(TTL buffer inputs)
PIC 16F84A Port Input Characteristics (5V power supply)
Simple Digital Interfacing – connecting to switches
V
V
S
V
S
S
R
Vi
Vi
Vi
R
Light Emitting Diodes (leds) - Review
High Efficiency Red
Yellow
http://www.kingbright.com.tw/
Modelling a Logic Gate Output
To connect the port bit output, we need an understanding of how its
internal circuit behaves electrically. We can model it as shown.
VLH
VLL
switch is in this
position when
output is at Logic 1
R S(high)
VL
R S(low)
VS
R S(high)
R S(low)
switch is in this
position when
output is at Logic 0
a) Generalised Model
internal Logic 1 voltage
is the supply voltage
internal Logic 0
voltage is ground
b) Model of CMOS Logic Gate Output
VL
Driving LEDs from Logic Gates (and hence Port Bit Outputs)
VS
ID
R
current flows out of the gate
and lights led when output
is at logic 1
R
current flows into gate
and lights led when
output is at logic 0
VD
ID
VD
a) Gate Output Sourcing
Current to LED
b) Gate Output Sinking
Current from LED
Data for the 16F84A (when powered from 5V) shows an output resistances
of approximately 130W when at logic high, and 36W when at logic 0.
Example. The circuit alongside, where the logic gate
represents a 16F84A port pin output, is to be used to light
either a red led (gate output high, gate is sourcing
current), or a green led (gate output low, gate is sinking
current). To balance light intensity levels the red led
requires approximately 10mA of drive current, while the
green requires approximately 14mA. At these currents the
leds both have 1.8V of forward voltage. Calculate values
for R1 and R2. The supply voltage is 5V.
V
S
R1
(green)
V
D1
R2
(red)
V
D2
End of Lecture Note