PIC - IO Ports

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 43

I/O Interfacing

1
PIC18F452/4520 I/O Ports
 MCU includes five I/O ports
 PORTA, PORTB, PORTC, PORTD, PORTE
 Ports are multiplexed
 Can be set up to perform various functions
 Each I/O port is associated with several SFRs
 PORT
 Functions as a latch or a buffer
 TRIS
 Data direction register
 Logic 0 sets up the pin as an output
 Logic 1 sets up the pin as an input
 LAT
 Output latch similar to PORT
2
Ports in PIC 18 Family

3
PIC18F452/4520 I/O Ports
PORTA: Multiple Functions
 Digital I/O: RA6-RA0
 Analog Input: AN0-AN4
 V REF+ : A/D Reference Plus V
 V REF- : A/D Reference Minus V
 TOCK1: Timer0 Ext. Clock
 SS: SPI Slave Select Input
 LVDIN: Low V Detect Input

4
5
6
7
8
PIC18F452/4520 I/O Ports
 PORTB

9
Alternate functions of port pins

10
11
12
I/O Example
 Write instructions to set up pins RB7-RB4 of
PORTB as inputs and pins RB3-RB0 as outputs

Opcode Operands Comments


MOVLW 0xF0 ;Load B’11110000’ into WREG
MOVWF TRISB ;Set PORTB TRIS Reg

13
Interfacing Output Peripherals
 Commonly used output peripherals in embedded systems
 LEDs

 Seven-Segment Displays

 LCDs

 Two ways of connecting LEDs to I/O ports


 Common Cathode

 LED cathodes are grounded


 Logic 1 from the I/O port turns on the LEDs
 Current is supplied by the I/O port called current sourcing
 Common Anode
 LED anodes are connected to the power supply
 Logic 0 from the I/O port turns on the LEDs
 Current is received by the chip called current sinking

14
Interfacing Output Peripherals

Common Cathode Common Anode


15
Seven-Segment Display
 Seven-segment Displays
 Used to display BCD digits
 0 thru 9
 A group of 7 LEDs physically
mounted in the shape of the
number eight
 Plus a decimal point
 Each LED is called a segment
 ‘a’ through ‘g’
 Two types
 Common anode
 Common cathode
16
Seven-Segment Display
 Common Anode
 All anodes are connected together to a power supply
 Cathodes are connected to data lines
 Logic 0 turns on a segment
 Example: To display the digit 1
 All segments except b and c should be off
 11111001 = F9H
Common Anode

17
Seven-Segment Display
 Common Cathode
 All cathodes are connected together to ground
 Anodes are connected to data lines
 Logic 1 turns on a segment
 Example: To display digit 1
 All segments except b and c should be off
 00000110 = 06H

18
 Interfacing Seven-Segment Display to PORTB
 Common Anode
 Table Look-Up

19
Illustrative Program
 Problem Statement
 Interface two common-anode seven-segment
displays to PORTD and PORTC of the PIC18F
 Write instructions to implement an up-counter,
counting from 00 to 59
 Display the count on the two seven-segment
displays

20
Interfacing Input Peripherals
 Commonly used input peripherals
 DIP switches, push-button keys, keyboards, and A/D converters
 DIP switch
 One side of the switch is tied high
 To a power supply through a resistor called a pull-up resistor
 The other side is grounded
 The logic level changes when the position is switched
 Push-button key
 Same as the DIP switch except that contact is momentary

21
DIP switch

22
Interfacing Dip Switches

23
Reading from an I/O Port
 Read input switches on PORTB (RB7-RB4)
 RB0 set HI (1)
 Switches Open = LOW (0)
 Switches Closed = HIGH (1)
 Display on PORTC
Opcode Operands Comments
MOVLW 0xF0 ;Load B’11110000’ into WREG

MOVWF TRISB ;Set PORTB TRIS Reg

CLRF TRISC ;Set PORTC as Output

BSF PORTB,0 ;Set RB0 High

MOVF PORTB,W ;Read PORTB


24
Internal Pull-Up Resistor
 Turning off the internal FET
provides a pull-up resistor
 Bit7 (RBPU) in the INTCON2
register enables or disables the
pull-up resistor
 Instruction to Enable Pull Up
Resistors:
BCF INTCON2,7

25
Interfacing Push-Button Keys
 When a key is pressed (or released), mechanical metal contact
bounces momentarily and can be read as multiple inputs
 Key debounce
 Eliminating reading of one contact as multiple inputs
 Hardware or Software

26
Key Debounce Techniques
 Hardware technique  Software technique
 Two NAND gates  Wait for 10 to 20 ms
 S-R latch after detection of a
 The output of the latch is switch closure
a pulse without a bounce
 If the reading is still the
same it is accepted

27
Interfacing a Matrix Keypad
 Problem statement
 Interface a 4 x 4 Hex keypad to PORTB
 Write a program to recognize a key pressed and
encode the key in its binary value
 Display binary code on PORTC

28
Interfacing a Matrix Keypad
 Hardware (PIC18 Simulator)
 4 x 4 matrix keypad organized in the row and
column format
 Four columns are connected to the lower half of
PORTB (RB0-RB3)
 Four rows are connected to upper half of PORTB
(RB4-RB7)
 When a key is pressed, it makes a contact with
the corresponding row and column

29
Interfacing a Matrix Keypad
 PIC18 Simulator Keypad Matrix

1 2 3 C

4 5 6 D

7 8 9 E
A 0 B F

30
Interfacing LCD
 Problem statement
 Interface a 2-line x 20 character LCD module
with the built-in HD44780 controller to I/O ports
of the PIC18 microcontroller.
 Explain the control signals necessary to read
from and write to the LCD.
 Write a program to display ASCII characters.

36
Interfacing LCD
 Hardware
 20 x 2-line LCD display
 Two lines with 20 characters per line
 LCD has a display Data RAM
 Stores data in 8-bit character code
 Each register in Data RAM has its own address
 Corresponds to its position on the line
 Line 1 is 00H to 13H
 Line 2 is 40H to 53H

37
Interfacing LCD

38
Interfacing LCD
 Driver HD44780
 8-bit data bus (RD7-RD0)
 Three control signals
 RS – Register Select (RA3)
 R/W – Read/Write (RA2)

E – Enable (RA1)
 Three power connections
 Power, ground, and variable resistor to control brightness

39
Interfacing LCD
 Can be interfaced either in 8-bit mode or 4-bit mode
 In 8-bit mode, all eight data lines are connected
 In 4-bit mode, only four data lines are connected
 Two transfers per character (or instruction) are needed
 Driver has two 8-bit internal registers
– Instruction Register (IR) to write instructions to set up LCD
 Data Register (DR) to write data (ASCII characters)

40
Interfacing LCD
 LCD Operation
 When the MPU writes an instruction to IR or data to DR,
the controller:
 Sets DB7 high indicating that the controller is busy
 Sets DB7 low after the completion of the operation
 The MPU should always check whether DB7 is low
before sending an instruction or a data byte

41
Interfacing LCD
 Writing to or Reading from LCD
 The MPU:
 Asserts RS low to select IR
 Asserts RS high to select DR
 Reads from LCD by asserting the R/W signal high
 Writes into LCD by asserting the R/W signal low
 Asserts the E signal high and then low (toggles) to latch a data
byte or an instruction

42
Interfacing LCD
 Timing diagram: writing to LCD

43
44
45
46
47
Interfacing LCD
 Software
 To write into the LCD
 Send the initial instructions to set up the LCD
 4-bit or 8-bit mode
 Continue to check DB7 until it goes low
 Write instructions to IR to set up LCD parameters
 Number of display lines and cursor status
 Write data to display a message

48

You might also like