Microprocessors and Microcontrollers
I/O Ports
DC Electrical Characteristics
EE3954 by Maarten Uijt de Haag, Tim Bambeck I/O Part I.1
PORTB
PORTA
PORTE PORTD
PORTC
PORTC
PORTD
PORTD Multiplexing of I/O Pins
Pin Diagram from Datasheet
I/O Part I.2
I/O Part I.3
Typical I/O Port
I/O Part I.4
Typical I/O Port
Buffers
I/O Part I.5
Typical I/O Port Flip-Flops
I/O Part I.6
Typical I/O Port
Logic Gates
I/O Part I.7
Typical I/O Port
MOS Transistors (Output Drivers)
I/O Part I.8
Overview
DC Electrical Characteristics I/O Pin Voltage
• Levels: – – – –
TTL Schmitt Trigger CMOS Open drain
Current
• Levels: – – – –
Sink current Source current Fan-in Fan-out I/O Part I.9
Input/Output Ports Most PORT pins can be defined as either input or output via the TRIS registers.
PORTA, PORTB, PORTC, PORTD, PORTE
TRISA, TRISB, TRISC, TRISD, TRISE
To read/write data from/to inputs/outputs
Data Direction Registers Determines if a pin is an input (“1”) or an output (“0”) I/O Part I.10
Special Function Registers
PORTs are in Bank 0
TRISs are in Bank 1
STATUS Bank 0: RP1 = 0; RP0 = 0 Bank 1: RP1 = 0; RP0 = 1
I/O Part I.11
I/O Ports
PORTA equ TRISA equ org nop
0x05 0x05
Example – Part 1
0x000
; Select Bank 1 to access the data direction registers (TRIS) bcf STATUS, RP1 bsf STATUS, RP0 ; Set pins 1, 2, 4, and 7 as input ; Set pins 0, 3, 5, and 6 as output movlw B’1001 0110’ movwf TRISA ; Select Bank 0 to access the PORT itself bcf STATUS, RP0 NOTE: 1 looks like an ‘I’ for input 0 looks like an ‘O’ for output
I/O Part I.12
I/O Ports
Example – Part 2 ; Select Bank 0 to access the PORT itself bcf STATUS, RP0 ; Check the input on 2 and call the subroutine SUB_1 if its is ‘1’ btfsc PORTA, 2 call SUB_1 … … … ; Set the output on pin 5 to “0” bcf PORTA, 5 … … ; Set the output on pin 0 to “1” bsf PORTA, 0
NOTE: 1 looks like an ‘I’ for input; 0 looks like an ‘O’ for output
I/O Part I.13
I/O Port
Equivalent Statements PORTA is at Data Memory address 0x05 and TRISA is at Data Memory address 0x85 7 least significant bits of 0x05 PORTA equ TRISA equ
0x05 0x05
00 0000101 -> 0x05 01 0000101 -> 0x85
I/O Part I.14
Typical I/O Port Pin
PORT Writes: movwf PORTX movwf TRISX
I/O Part I.15
Typical I/O Port Pin PORT Reads: movf PORTX,W btfss PORTX,2
I/O Part I.16
Typical I/O Port
I/O Part I.17
Transistor p-channel: p-channel transistor
“1”
=
Off
“0”
=
On
Source Gate
Drain
P
I/O Part I.18
Transistor n-channel: n-channel transistor
“1”
=
On
“0”
=
Off
Drain Gate
N
Source
I/O Part I.19
Totem Pole Configuration VDD S
V1
V1 = VDD=‘1’; V2 = VDD=‘1’
VDD
P
G D
V1 = VSS=‘0’; V2 = VSS=‘0’
VDD
Off
I/O Pin
On
“0” (VSS)
“1” (VDD)
D
V2 G
On
N
Off
S
VSS
VSS
VSS
I/O Part I.20
Interpretation Example Writing to TRIS - Output
“0”
“0” “1”
Here: VSS = “0” VDD = “1” I/O Part I.21
Interpretation Example Writing to a PORT Pin - Output “0”
“1”
“1” “0” “1”
“0”
“0”
“1”
“1”
“1”
Here: VSS = “0” VDD = “1” I/O Part I.22
Schmitt Trigger Schmitt Trigger buffer:
Regular input buffer:
Vin Vin
Vin
Vout
Switching threshold
Vin
Vout
Positive-going Threshold (4v) Negative-going Threshold (1v)
t
t
Vout
Vout
False switching occurs
t
Jitter free operation I/O Part I.23t
I/O Port
DC Electrical Characteristics Output:
Input:
(Minimum high-level output voltage)
VOH
VOL
(Minimum high-level input voltage)
VIH VIL
(Maximum low-level input voltage)
(Maximum low-level output voltage)
Increasing voltage
See, for example, Datasheet Page 155/156 (157/158 in PDF)
I/O Part I.24
I/O Port
Noise Margin 5.0 v ->
VDD Guaranteed HIGH-level output
4.3 v (min) -> 2.0 v ->
VOH VIH
Recognizable HIGH-level output
Noise Margin Uncertain region (neither HIGH nor LOW)
0.75v -> 0.6v (max) -> 0v ->
VIL VOL VSS = 0
Noise Margin
Recognizable LOW-level output Guaranteed LOW-level output I/O Part I.25
TTL Voltage Level PIC16F877
• VOL = 0.6V max • VOH = (VDD- 0.7) V min = 4.3V min
( values from page 156 in Data Sheets )
• VIL = VSS -> 0.15 VDD = 0V -> 0.75V • VIH = 2.0 -> VDD = 2.0V –> 5.0V (values from page 155 in Data Sheets )
I/O Part I.26
I/O Ports
Sink and Source Currents Sink current is the current going into the microcontroller
Microcontroller
I
Source current is the current coming out of the microcontroller
Microcontroller
I
See, for example, Datasheet Page 149 (151 in PDF)
I/O Part I.27
I/O Ports
Sink and Source Currents – PIC16F877
Any pin = 25 mA. Port A=6 pins, B=8 pins, E=3 pins for a total of 17 pins. We would expect: 17 pins * 25 mA = 425 mA. ( Note: combined Port A,B,E is 200 mA max.) I/O Part I.28
I/O Ports
Sink and Source Currents – PIC16F877
RD1
If RD1 = “0” = VOL = 0V: No voltage differential between (A) and (B), thus LED is OFF.
(A) R
If RD1 = “1” = VOH = 5V: 5V
PIC 16F877
R (B)
1.4V
NOTE: Voltage drop over the diode is ~1.4V
Ohm’s Law: V = I*R Voltage over resistor: 5 – 1.4 = 3.6V I = V/R = 3.6/R < 25mA Thus, R > 3.6/25·10-3 = 144Ω PR= 3.6*25·10-3 = 0.09W I/O Part I.29
I/O Ports
Sink and Source Currents – PIC16F877 With VOH and VOL RD1
VOH = 4.3V & VOL = 0.6V IOH = 25mA & IOL = 25mA
(A) R
PIC 16F877
If RD1 = “0” = VOL = 0.6V: Not enough voltage differential between (A) and (B), thus LED is OFF. If RD1 = “1” = VOH = 4.3V:
(B)
4.3V R 1.4V
NOTE: Voltage drop over the diode is ~1.4V
Ohm’s Law: V = I * R Voltage over resistor: 4.3 – 1.4 = 2.9V I = V/R = 2.9/R < 25mA Thus, R > 2.9/25·10-3 = 116Ω I/O Part I.30
Diodes I
I
Current only flows through the diode in one direction VDD Works:
VSS Does not Work:
VSS
VDD
I/O Part I.31
Laboratory #2 7-segment display
RD7
Microcontroller RD6 RD5 RD4 RD3 RD2 RD1
RD0
I/O Part I.32
Fan-out Fan-out The number of inputs (in the form of devices and gates) that can be driven from a single device or gate output (or pin) without damaging the IC
Low
I1
I2 I
I3
High
I1
I2
I3
I
I/O Part I.33
Fan-out Example
RD1
VOH = 5 V 470
470 470 470 470
PIC 16F877
Vdiode = 1.4V
Is this going to work??? Current through one of the LEDs = (5-1.4)/470 = 7.66mA So, the total current that must be sourced by the pin is: 5*7.66mA = 38.3mA So, 38.3mA > 25mA and therefore this is not going to work!!! I/O Part I.34
Interpretation Example Writing to TRIS - Input
“1”
“1”
“0” “1”
“open”
“open”
“0”
Here: VSS = “0” VDD = “1” I/O Part I.35
I/O Ports
High Impedance
I/O Part I.36
I/O Ports
High Impedance V2 = VDD
I/O Pin – RA4 (PORTA Pin 4)
V2
N
I/O Pin = VSS= “0”
V2 = VSS
I/O Pin = ∞Ω
NMOS
VSS Open Drain Output
On
VSS
Off
VSS
It looks as if there is a very high impedance between the pin and ground
I/O Part I.37
I/O Ports
Pull-Up Resistor
VDD
VDD
VDD
5V
0V On
V2
Off
N VSS VSS
V2 = VDD
VSS V2 = VSS I/O Part I.38
I/O Ports PIC16F877 • See Datasheet Table 1-2 Page 8 & 9 (PDF pages: 10 & 11)
• See Datasheet Pages 30 through 36 For: (PDF pages: 32-38)
PORTA, B, C, D, E Functions. Registers Associated with Each Port. I/O Part I.39
PORT A
From Datasheet
PINS 0,1,2, and 3 are also used as a 10-bit Analog-to-Digital Converter (ADC) (Sections 21,22,23, and 24 of RM) (Section 11 of DS) SFR ADCON1 (see page 29 of DS) must be used to configure PORTA as either a regular PORT or an ADC. From PIC16F877 Datasheet
I/O Part I.40
PORT A - Configuration ADCON1 @ 0x1F in Bank 1
RED Circled is POR Condition.
Blue Circled is Example on Next page.
I/O Part I.41
PORT A
Configure as all digital I/O BCF BCF CLRF
STATUS, RP0 STATUS, RP1 PORTA
BSF MOVLW MOVWF MOVLW
STATUS, RP0 B’0000 0110’ ADCON1 B’1100 1111’
MOVWF
TRISA
; ; Bank0 ; Initialize PORTA by ; clearing output ; data latches ; Select Bank 1 ; Configure all pins ; as digital inputs ; Value used to ; initialize data ; direction ; Set RA as inputs ; RA as outputs ; TRISAare always ; read as ’0’. I/O Part I.42
PORT A
From Datasheet
I/O Part I.43
PORT B
I/O Part I.44
PORTB
I/O Part I.45